Java AES加密

Java AES 加密

  1. 加密
    /**
     * 
     * @description 加密
     *
     * @param content 需要加密的内容
     * @param password 加密密码
     * @return
     */
    public static byte[] encrypt(String content, String password) {
        try {
            //创建AES密钥生成器
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            //使用用户提供的随机源初始化此密钥生成器,使其具有确定的密钥长度
            keyGenerator.init(128, new SecureRandom(password.getBytes()));
            //生成一个密钥
            SecretKey secretKey = keyGenerator.generateKey();
            //编码的密钥,如果此密钥不支持编码,则返回 null
            byte[] enCodeFormat = secretKey.getEncoded();
            //根据给定的字节数组构造一个密钥
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            // 创建密码器
            Cipher cipher = Cipher.getInstance("AES");
            byte[] byteContent = content.getBytes("utf-8");
            // 初始化
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(byteContent);
            // 加密结果
            return result; 
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }
  1. 解密
    /**
     * 
     * @description 解密
     *
     * @param content 待解密内容
     * @param password 解密密钥
     * @return
     */
    public static byte[] decryptFrom(byte[] content, String password) {
        try {
            //创建AES密钥生成器
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            //使用用户提供的随机源初始化此密钥生成器,使其具有确定的密钥长度
            keyGenerator.init(128, new SecureRandom(password.getBytes()));
            //生成一个密钥
            SecretKey secretKey = keyGenerator.generateKey();
            //编码的密钥,如果此密钥不支持编码,则返回 null
            byte[] enCodeFormat = secretKey.getEncoded();
            //根据给定的字节数组构造一个密钥
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            // 创建密码器
            Cipher cipher = Cipher.getInstance("AES");
            // 初始化
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] result = cipher.doFinal(content);
            // 返回结果
            return result; 
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }
  1. 测试
public static void main(String[] args) {
    String content = "7654321 ";
    String password = "a.7?6#5@4!3^1%2";
    byte[] encryptResult = encrypt(content,pa
    System.out.println(encryptResult);
    byte[] decryptResult = decryptFrom(ecryptResult, password);
    System.out.println("解密后:" + new String(decryptResult));
}
原文地址:https://www.cnblogs.com/hedianwei/p/6139626.html