我在Java中面临解密问题。以下是遇到的错误:
javax.crypto.BadPaddingException: Blocktype mismatch: -127
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:311)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:255)
使用以下代码在.Net中加密文本:
public string EncryptString( string inputString, int dwKeySize, string xmlString )
{
RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider( dwKeySize );
rsaCryptoServiceProvider.FromXmlString( xmlString );
int keySize = dwKeySize / 8;
byte[] bytes = Encoding.UTF32.GetBytes( inputString );
int maxLength = keySize - 42;
int dataLength = bytes.Length;
int iterations = dataLength / maxLength;
StringBuilder stringBuilder = new StringBuilder();
for( int i = 0; i <= iterations; i++ )
{
byte[] tempBytes = new byte[ ( dataLength - maxLength * i > maxLength ) ? maxLength : dataLength - maxLength * i ];
Buffer.BlockCopy( bytes, maxLength * i, tempBytes, 0, tempBytes.Length );
byte[] encryptedBytes = rsaCryptoServiceProvider.Encrypt( tempBytes, true );
Array.Reverse( encryptedBytes );
stringBuilder.Append( Convert.ToBase64String( encryptedBytes ) );
}
return stringBuilder.ToString();
}
解密代码是JAVA:
PrivateKey privKey = readPrivateKey(); // reads the private key
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] encryptedBytes = Base64.decodeBase64(encryptedText.getBytes("UTF-32"));
encryptedBytes = reverse(b); // reverse the bytes
byte[] decrypted = cipher.doFinal(encryptedBytes);
return new String(decrypted);
我在这里遗漏了什么吗?如何进行双向加密/解密?
答案 0 :(得分:1)
当您使用PKSC v1.5时,您的C#应用程序正在使用OAEP填充PKSC v2尝试将您的C#代码更改为:
ptedBytes = rsaCryptoServiceProvider.Encrypt( tempBytes, false );
答案 1 :(得分:0)