java 使用AES解密报这个异常,字面理解很容易,就是解密的字符串的数组必须是16的倍数
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:922)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:833)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
at com.symmetric.aes.TestAES.testDecrpyt(TestAES.java:200)
at com.symmetric.aes.TestAES.main(TestAES.java:48)
1.使用的代码:
package com.symmetric.aes;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
public class TestAES2 {
private static final String enType_AES = "AES";
private static final String pathStr = "D://aes.key";
private static final String testStr = "AES tips you shoutld try again";
public static void main(String[] args) {
SecretKey genSecretKey = testGenerateKey();
String string = genSecretKey.toString();
// byte[] testEncryptBytes = testEncryptBytes(testStr, genSecretKey);
// String testDecode = testDecrptBytes(testEncryptBytes, genSecretKey);
// System.out.println(testDecode); //直接操作数组,加密解密正常
String testEncrypt = testEncrypt(testStr, genSecretKey);
String testDecode2 = testDecrpt(testEncrypt, genSecretKey);
System.out.println(testDecode2);
* 生成密钥
* 通过传递SecureRandom对象进行初始化
* 不指定种子或密钥
* @return
public static SecretKey testGenerateKey(){
SecretKey genSecretKey = null;
try {
KeyGenerator kGenerator = KeyGenerator.getInstance(enType_AES);
SecureRandom sRandom = new SecureRandom();
kGenerator.init(sRandom);//不使用种子,每次生成的都不同
genSecretKey = kGenerator.generateKey();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return genSecretKey;
* @param str
* @param genSecretKey
* @return
public static byte[] testEncryptBytes(String str,SecretKey genSecretKey){
byte encrypt [] = null;
try {
Cipher cipher = Cipher.getInstance(enType_AES);
cipher.init(Cipher.ENCRYPT_MODE, genSecretKey);//加密模式,密钥
//cipher.update(str.getBytes());
encrypt = cipher.doFinal(str.getBytes());
} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
return encrypt;
public static String testEncrypt(String str,SecretKey genSecretKey){
byte[] testEncryptBytes = testEncryptBytes(str, genSecretKey);
System.out.println(Arrays.toString(testEncryptBytes));
System.out.println("加密后的数组长度"+testEncryptBytes.length);
return (new String(testEncryptBytes));
* @return
public static String testDecrptBytes(byte[] bytes,SecretKey genSecretKey){
String decoderStr = null;
try {
Cipher cipher = Cipher.getInstance(enType_AES);
cipher.init(Cipher.DECRYPT_MODE, genSecretKey);//解密模式
//cipher.update(str.getBytes());
byte[] doFinal = cipher.doFinal(bytes);
decoderStr = new String(doFinal);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
return decoderStr;