In my android application I am implementing RSA encryption decryption.
we have this app for ios as well as for android.
Whenever ios encryptes the message with my public key sends the encrypted text in base64 and on android side I decrypt it with my private key.
The problem is whenever I decrypt the data,It gives encrypted message surrounded by garbage padding
as shown bellow :
K������N�t �X�08���I�ii�z �<���C�,r|�����aKj:N�^J���c��U�X�'�r�6Y��k,o�D^�)����F���[
����tH^�f�s��test updated pub key��
"test updated pub key" is the message.
public static String RSADecrypt(final String result, Context context, PrivateKey key)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, InvalidKeySpecException
{
Log.d(TAG, "Decryption of " + result);
String decrypted = "";
try
{
byte[] b = Base64.decode(result, Base64.DEFAULT);
Cipher cipher1;
cipher1 = Cipher.getInstance(ALGO);
cipher1.init(Cipher.DECRYPT_MODE, key);
byte[] decryptedBytes = cipher1.doFinal(b);
decrypted = new String(decryptedBytes,"US-ASCII");
Log.d(TAG, "Decrypted text " + decrypted);
Toast.makeText(context, decrypted, Toast.LENGTH_LONG).show();;
}
catch (Exception e)
{
e.printStackTrace();
Log.d(TAG, "Exception in decryption");
}
return decrypted;
}
I cannot understand where is the problem, on iOs side on android side or in converting bytes to string after decryption.Plz help
Try and use "RSA/ECB/PKCS1Padding" instead of "RSA/ECB/NoPadding" or similar for the value of ALGO. The surrounding random looking characters are actually random padding bytes. Padding is an integral part of RSA encryption and should not be skipped.
Note that the KeyPairGenerator should still use "RSA", the generator is not concerned with modes of encryption and/or padding modes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With