Several solutions to Javax.crypto.BadPaddingException:Blocktype anomalies
Reprint please indicate the source
1. Exception Description: Recently done project in order to enhance the security of data transmission using RSA encryption. That is, the information that the Android client will transmit is encrypted with the private key through the RSA asymmetric encryption algorithm to the server side (PC side). The server side decrypts the decryption failure with the corresponding (key) public key, throwing a "javax.crypto.BadPaddingException:Blocktype" exception.
2. Exception reason: The virtual machines used by Android (Dalvik) are different from the Sun Standard JDK, where their default RSA implementations differ. That is, when using the Cipher.getinstance ("RSA") method to encrypt the Android side, the provider used is Bouncycastle security provider,bouncycastle security The default implementation of provider is the "rsa/none/nopadding" algorithm, while the server (PC) End with Cipher.getinstance ("RSA") to decrypt, using the sun's security provider, the implementation is " Rsa/none/pkcs1padding "algorithm, therefore, decryption will fail.
3. WORKAROUND: I offer three solutions here:
The first: Change the Cipher.getinstance ("RSA") method on the server (PC) side to Cipher.getinstance ("rsa/ecb/nopadding"). But one drawback of this modification is that the decrypted plaintext has a lot more space than before the encryption. (Length of Space + original number of characters = number of bits used to generate the key/8)
The second option is to change the Cipher.getinstance ("RSA") method on the Android side to Cipher.getinstance ("rsa/none/pkcs1padding"). This method decrypts the plaintext and the plaintext before the encryption is corresponding, do not appear in the first method of the phenomenon, recommended this method.
Third: Add Bouncycastle Security Provider to the JDK on the server (PC) side, and for the installation configuration and verification of the Bouncycastle JCE, see http://blog.csdn.net/ caoshichao520326/article/details/8732670, after configuring the Bouncycastle Security provider, the server (PC) side of the Cipher.getinstance ("RSA") The method is changed to Cipher.getinstance ("RSA", "BC").
Several solutions to Javax.crypto.BadPaddingException:Blocktype errors