jiazx0107@163.com
2023-12-02 5bb3ad3944ff229baff1d8bb92caf55d816ee68b
src/main/java/com/fzzy/api/utils/AESUtils.java
@@ -29,6 +29,8 @@
     * 加解密方式
     */
    private final static String ALGORITHM  = "AES";
    private final static String ALGORITHM_ECB  = "ECB";
    private final static int KEY_SIZE_128 = 128;
    /**
     *加密模式及填充方式
@@ -81,5 +83,33 @@
        return new String(encryptData,ENCODING);
    }
    /**
     * AES加密-ECB方式
     * @param plainText
     * @param key
     * @return
     * @throws Exception
     */
    public static String encryptByEcb(String plainText, String key) throws Exception {
        if (key == null) {
            System.out.print("Key为空null");
            return null;
        }
        // 判断Key是否为16位
//        if (key.length() != 16) {
//            System.out.print("Key长度不是16位");
//            return null;
//        }
        SecretKey secretKey = new SecretKeySpec(key.getBytes(ENCODING), ALGORITHM_ECB);
        // AES加密采用pkcs5padding填充
        Cipher cipher = Cipher.getInstance(PATTERN);
        //用密匙初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        //执行加密操作
        byte[] encryptData = cipher.doFinal(plainText.getBytes(ENCODING));
        return Base64.getEncoder().encodeToString(encryptData);
    }
}