I am using RSA to encrypt username and password on Android and decrypt them on server (tomcat 6, java 1.6). Android Encryption:
PublicKey pubKey = readPu
I suggest you use specific cipher initialization: as an example,
Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
will work on both. The exception you are getting (BadPaddingException) is occuring because the default cipher initialization padding appears to be different between the desktop JVM and the Android JVM.
Im doing RSA Encrypt in Android 2.2+ and decrypt on a tomcat 6 java 1.6 server.
I was getting this exact problem, reading all over the place and in part thanks to @Femi 's answer I came across what I needed.
The solution was to use the folowing algorithm specification for the Cipher:
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
This works doing encryption from both Android and BlackBerry smartphones. I know its been four months since the question was asked, but just in case someone else goes through this problem.
Firstly, it looks like you're initializing both ciphers with the public key. Encryption uses public key, decryption used private key. I hope that's just a typo though.
I had a lot of trouble with RSA encryption as well, much was trial and error. I suggest you try another provider. I managed to implement RSA using BouncyCastle.
Cipher wrapper = Cipher.getInstance("RSA", "BC");
wrapper.init(Cipher.ENCRYPT_MODE, publicKey);
encryptedData= wrapper.doFinal(unencryptedData);
Although, I generated my own keypair since this was a session encryption.
kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
KeyPair kp = kpg.genKeyPair();
publicKey = kp.getPublic();
privateKey = kp.getPrivate();