Home > Back-end >  AES decryption frustrated BadPaddingException: Given the final block not properly padded
AES decryption frustrated BadPaddingException: Given the final block not properly padded

Time:05-20

Background: there is a demand now is to write a interface receive encrypted information from other systems, need to decrypt the reprocessing
Encryption algorithm is known:
AES/the ECB/pkcs5padding/128 - bit data blocks/password: 12345678901234567890123456789012/base64 output/utf8 character set
Test on online website:

This configuration can be normal decryption

Then I start trying to Java implementation
Key code is as follows:
 
Private static String key="12345678901234567890123456789012";
Private static String mode="AES/the ECB/PKCS5Padding";
Public static String decodeMsg (String MSG) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException, InvalidKeySpecException {
Cipher Cipher=Cipher. GetInstance (mode);
Cipher. The init (cipher DECRYPT_MODE, getSecretKey (key));//initialize
Byte [] msgbyte=base64 (MSG);
Result=cipher byte []. DoFinal (msgbyte);
Return a new String (result);//decryption
}
Public static byte [] base64 (String STR) {
Return Base64. GetDecoder (). The decode (STR);
}
Private static SecretKeySpec getSecretKey (final String password) {
//returns the algorithm to generate the key generator KeyGenerator object
KeyGenerator kg=null;
Try {
Kg=KeyGenerator. GetInstance (AES, "");
Byte [] keyBytes=password. GetBytes ();
SecureRandom SecureRandom=SecureRandom. GetInstance (" SHA1PRNG ");
SecureRandom. SetSeed (keyBytes);
//AES for key length of 128
Kg. The init (128, secureRandom);
A key generated//
SecretKey SecretKey=kg. GenerateKey ();
Return new SecretKeySpec (secretKey. GetEncoded (), "AES");//converted to AES private key
{} the catch (NoSuchAlgorithmException ex)
//Logger. GetLogger (AESUtil. Class. GetName ()). The log (Level. SEVERE, null, ex);
}
return null;
}


Test has been an exception is thrown when
Javax.mail. Crypto. BadPaddingException: Given the final block not properly padded

I first make AES, for not familiar with this, where is can't see the problem is,

CodePudding user response:

The building Lord, how to solve this problem I also appear the same mistake, consult!
  • Related