RSA Encryption: Difference between Java and Android

大兔子大兔子 提交于 2019-11-27 17:52:29
blindstuff

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.

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.

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!