3
votes

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

1
Take the base64 and string conversion out of the equation: look at the raw bytes you're encrypting, and the raw bytes after decryption. - Jon Skeet
But i want to transmit the data over server,so how will i transfer without converting it in base64. - Rushabh Bhatt
I didn't say you needed to transmit it without using base64. I said that you need to look at the raw bytes you're encrypting and the raw bytes you're decrypting. And ideally try the same code without needing the base64 side (e.g. write to a local file and test it that way). - Jon Skeet
Can you please give me an example code.It is difficult for me to understand what you are trying to say. - Rushabh Bhatt
I'm not talking about code necessarily. I'm talking about performing more diagnostics. You should look at the data before and after encryption, and the data before and after decryption. For a start, are you trying to decrypt the right number of bytes? How many bytes do you get back after decryption, and is that the right number? All of that can be done in a debugger. I would suggest you try to write a short but complete program which demonstrates the problem in a single application, however - ideally just in desktop Java rather than on mobile, as a starting point. - Jon Skeet

1 Answers

4
votes

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.