javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with pad

今天在做AES加密时,项目中出现javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher异常是AES加密解密的报错异常,在向方法传输参数进行解密的过程中会报错,这个错误的字面意思就是java 使用AES解密报这个异常,字面理解很容易,就是解密的字符串的数组必须是16的倍数。

AESUtils 工具类:

package ctd.util.codec;

import com.fasterxml.jackson.databind.ObjectMapper;
import ctd.util.JSONUtils;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;

public class AESUtils {

    private static final String KEY_ALGORITHM = "AES";
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    public static byte[] initSecretKey() throws Exception {
        KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
        kg.init(128);
        SecretKey secretKey = kg.generateKey();

        return secretKey.getEncoded();
    }

    public static String generatorSecretKey() throws Exception {
        return DigestUtils.md5Hex(initSecretKey()).substring(8, 24);
    }

    public static Key toKey(byte[] key) {
        return new SecretKeySpec(key, KEY_ALGORITHM);
    }

    public static String encrypt(String data, String key) throws Exception {
        return Base64.encodeBase64String(encrypt(data.getBytes(), toKey(key.getBytes()), DEFAULT_CIPHER_ALGORITHM));
    }

    public static String decrypt(String data, String key) throws Exception {
        return new String(decrypt(Base64.decodeBase64(data.getBytes()), toKey(key.getBytes()), DEFAULT_CIPHER_ALGORITHM));
    }

    public static byte[] encrypt(byte[] data, Key key) throws Exception {
        return encrypt(data, key, DEFAULT_CIPHER_ALGORITHM);
    }

    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        return encrypt(data, toKey(key), DEFAULT_CIPHER_ALGORITHM);
    }

    public static byte[] encrypt(byte[] data, Key key, String cipherAlgorithm) throws Exception {
        Cipher cipher = Cipher.getInstance(cipherAlgorithm);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(data);
    }

    public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        return decrypt(data, key, DEFAULT_CIPHER_ALGORITHM);
    }

    public static byte[] decrypt(byte[] data, Key key) throws Exception {
        return decrypt(data, key, DEFAULT_CIPHER_ALGORITHM);
    }

    public static byte[] decrypt(byte[] data, byte[] key, String cipherAlgorithm) throws Exception {
        Key k = toKey(key);
        return decrypt(data, k, cipherAlgorithm);
    }

    public static byte[] decrypt(byte[] data, Key key, String cipherAlgorithm) throws Exception {
        Cipher cipher = Cipher.getInstance(cipherAlgorithm);
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(data);
    }

    private static String showByteArray(byte[] data) {
        if (null == data) {
            return null;
        }
        StringBuilder sb = new StringBuilder("{");
        for (byte b : data) {
            sb.append(b).append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append("}");
        return sb.toString();
    }


    public static void main(String[] args) throws Exception {
        String key = generatorSecretKey();
        System.out.println("key: " + key);
        String[] original = new String[]{"hello 我是中文 world!"};
        try {
            String encrypt = encrypt(new ObjectMapper().writeValueAsString(original), key);
            System.out.println("encrypt: " + encrypt);
            String decrypt = decrypt(encrypt, key);
            System.out.println("decrypt: " + decrypt);
            System.out.println("original: " + JSONUtils.parse(decrypt.getBytes(), String[].class));
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
}

调用:

                        String enc = AESUtils.decrypt(IOUtils.toString(request.getInputStream()), aes128_encrypt_key);

报错原因:

一步步的调试,我们可以看到哦,我们能够看到参数的传递到后台我们的参数中是否包含处参数以外的其他字符,否则就会报错。

解决方案:

将传递的参数的其他字符想办法去除。

你可能感兴趣的:(加密解密,JAVA,加密解密)