* @return public static String encrypt ( String algorithm, String encryptKey, String data ){ try { // 创建密码器 Cipher cipher = Cipher . getInstance (algorithm); // 初始化 Key key = new SecretKeySpec (encryptKey. getBytes (), AES ); cipher. init ( Cipher . ENCRYPT_MODE , key); return new String (cipher. doFinal (data. getBytes ())); } catch ( Exception e){ e. printStackTrace (); return "" ; * 将数据进行AES解密 * @param algorithm * @param encryptKey * @param data * @return public static String decrypt ( String algorithm, String encryptKey, String data ){ try { // 创建密码器 Cipher cipher = Cipher . getInstance (algorithm); // 初始化 Key key = new SecretKeySpec (encryptKey. getBytes (), AES ); cipher. init ( Cipher . DECRYPT_MODE , key); return new String (cipher. doFinal (data. getBytes ())); } catch ( Exception e){ e. printStackTrace (); return "" ; private static final String TRANSFORM_ALGORITHM = "AES/ECB/PKCS5Padding" ; private static final String ALGORITHM = "AES" ; private static final String CHARSET_NAME = "UTF-8" ; public static String ENCRYPT_KEY = "Y5MUIOM7BUWI7BQR" ; public static void main ( String [] args) throws Exception { String data = "123456" ; String str = encrypt ( ENCRYPT_KEY ,data); System . out . println (str); System . out . println ( decrypt ( ENCRYPT_KEY ,str));

使用简单数据测试,正常。

使用json对象测试,出现了标题中的错误:Input length must be multiple of 16 when decrypting with padded cipher

解密的字节数组必须是16的倍数

翻阅资料后发现

AES原理:AES是对数据按128位,也就是16个字节进行分组进行加密的,每次对一组数据加密需要运行多轮。而输入密钥的长度可以为128、192和256位,也就是16个字节、24个字节和32个字节,如果用户输入的密钥长度不是这几种长度,也会补成这几种长度。无论输入密钥是多少字节,加密还是以16字节的数据一组来进行的,密钥长度的不同仅仅影响加密运行的轮数

查看资料后,发现网上有两种解决办法:

自己写方法将字符串与byte数组之间进行转换

private static String parseByte2HexStr(byte buf[]) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < buf.length; i++) {
        String hex = Integer.toHexString(buf[i] & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        sb.append(hex.toUpperCase());
    return sb.toString();
private static byte[] parseHexStr2Byte(String hexStr) {
    if (hexStr.length() < 1){
        return null;
    byte[] result = new byte[hexStr.length()/2];
    for (int i = 0;i< hexStr.length()/2; i++) {
        int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
        int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
        result[i] = (byte) (high * 16 + low);
    return result;
  • 使用base64来对字符串进行加解密
  • public static String encrypt(String key, String value) throws Exception {
        SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(Charset.forName(CHARSET_NAME)), ALGORITHM);
        Cipher cipher = Cipher.getInstance(TRANSFORM_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, aesKey);
        byte[] encrypted = cipher.doFinal(value.getBytes(Charset.forName(CHARSET_NAME)));
        return Base64.getEncoder().encodeToString(encrypted);
    public static String decrypt(String key, String value) throws Exception {
        byte[] decode = Base64.getDecoder().decode(value.getBytes(StandardCharsets.UTF_8));
        SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(Charset.forName(CHARSET_NAME)), ALGORITHM);
        Cipher cipher = Cipher.getInstance(TRANSFORM_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, aesKey);
        return new String(cipher.doFinal(decode));
    复制代码
    分类:
    后端
    标签: