У меня проблемы с расшифровкой в 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);
Я что-то здесь упускаю?Как я могу сделать двустороннее шифрование / дешифрование?