Several solutions to Javax.crypto.BadPaddingException:Blocktype errors

Source: Internet
Author: User
Tags asymmetric encryption

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.