Solve the problem of AES decryption failure in Linux

Source: Internet
Author: User
Solve the problem of AES decryption failure in Linux. description: encryption and decryption on windows are normal, while encryption on linux is normal. the following exception occurs during decryption: javax. crypto. badPaddingException: Givenfinalblocknotproperlypaddedatcom. s... description:
In windows, encryption and decryption are normal, while in linux, the following exception occurs: javax. crypto. badPaddingException: Given final block not properly padded at com. sun. crypto. provider. sunJCE_f. B (DashoA13 *..) at com. sun. crypto. provider. sunJCE_f. B (DashoA13 *..) at com. sun. crypto. provider. AESCipher. engineDoFinal (DashoA13 *..) at javax. crypto. cipher. doFinal (DashoA13 *..) at chb. test. crypto. AESUtils. crypt (AESUtils. java: 386) at chb. test. crypto. AESUtils. AE SDecrypt (AESUtils. java: 254) at chb. test. crypto. AESUtils. main (AESUtils. java: 40) www.2cto.com solution: After checking, locate the method for generating the KEY, as shown below: [java] public static SecretKey getKey (String strKey) {try {KeyGenerator _ generator = KeyGenerator. getInstance ("AES"); _ generator. init (128, new SecureRandom (strKey. getBytes (); return _ generator. generateKey ();} catch (Exception e) {throw new RuntimeException ("An Exception occurred during key initialization") ;}} Changed to the following method. solution: [java] public static SecretKey getKey (String strKey) {try {KeyGenerator _ generator = KeyGenerator. getInstance ("AES"); SecureRandom secureRandom = SecureRandom. getInstance ("SHA1PRNG"); secureRandom. setSeed (strKey. getBytes (); _ generator. init (128, secureRandom); return _ generator. generateKey ();} catch (Exception e) {www.2cto.com throw new RuntimeException ("initialization key Exception ");} Cause analysis: Is SecureRandom fully implemented? Operating system itself? Acetylene ?? B, unless? Users in? After using the getInstance method? The setSeed method is used. The key generated every time on windows is the same, but it is different on solaris or some linux systems.
Author: hbcui1984
Related Article

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.