Hi everyone,
today I had some time taking problems with the RSA implementation on Android. I was trying to do the following:
Encrypt a log file on my Android device with a RSA public key, copy the device to the server and on the server decrypt the log file with my RSA private key using a small Java-App.
So for testing I first encrypted and decrypted on my Android device using the generated key pair, then I encrypted and decrypted a String in my Java App using the same key pair. Worked nicely.
Then I tried to copy the encrypted file from the Android device and decrypt in on the server. Didn’t work. But why? I used the same key pair all the time, but always got an Exception:
javax.crypto.BadPaddingException: Blocktype mismatch: 0
Solution:
Afters some hours of trying, confirming that the key pairs are the same, using different options of saving the public key in the Android filesystem, etc. I finally found a post stating that Android is using the Bouncycastle Security provider. Bouncycastle’s default RSA implementation is: “RSA/None/NoPadding”, whereas Sun’s default security provider implementation is “RSA/None/PKCS1Padding”. So, no decryption possible when just using
Cipher.getInstance("RSA")
because of the different paddings used.
So for a solution I downloaded the bouncycastle Jar, added bounycastle as a Security provider to my Java App, and now use
Cipher.getInstance("RSA", "BC");
It works fine now.