RSA Encryption: Difference between Java and Android

前端 未结 3 651
眼角桃花
眼角桃花 2020-12-04 15:57

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         


        
相关标签:
3条回答
  • 2020-12-04 16:18

    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.

    0 讨论(0)
  • 2020-12-04 16:25

    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.

    0 讨论(0)
  • 2020-12-04 16:30

    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();
    
    0 讨论(0)
提交回复
热议问题