java 实现 AES 加解密

版权声明:欢迎大家转载,指正。 https://blog.csdn.net/yin__ren/article/details/81778276

1. 原理:

AES加密


2. java 代码实现

注: ENCODE_RULES 为自己定义的值,如:private static final String ENCODE_RULES = "liu";

加密

/**
     * 加密
     * 1. 构造密钥生成器
     * 2. 根据 encodeRules 规则初始化密钥生成器
     * 3. 产生密钥
     * 4. 创建初始化密码器
     * 5. 内容加密
     * 6. 返回字符串
     *
     * @param content
     * @return
     */
    public static String aesEncode(String content) {
        try {
            //1. 构造密钥生成器,指定为 AES 算法,不区别大小写
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            //2. 根据 encodeRules 规则初始化密钥生成器,生成一个128位的随机源,根据传入的字节数组
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(ENCODE_RULES.getBytes());
            keyGenerator.init(128, random);
            //3. 产生原始对称密钥
            SecretKey originalKey = keyGenerator.generateKey();
            //4. 获得原始对称密钥的字节数组
            byte[] raw = originalKey.getEncoded();
            //5. 根据字节数组生成AES密钥
            SecretKey key = new SecretKeySpec(raw, "AES");
            //6. 根据指定算法AES自成密码器
            Cipher cipher = Cipher.getInstance("AES");
            //7. 初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY
            cipher.init(Cipher.ENCRYPT_MODE, key);
            //8. 获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合,中文就会解码为乱码
            byte[] byteEncode = content.getBytes("utf-8");
            //9. 根据密码器的初始化方式--加密:将数据加密
            byte[] byteAES = cipher.doFinal(byteEncode);
            //10. 将加密后的数据转换为字符串
            String aesEncode = new String(new BASE64Encoder().encode(byteAES));
            //11. 将字符串返回
            return aesEncode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        //如果有错就返回 null
        return null;
    }

解密

/**
     * 解密
     * 1. 同加密 1-4 步
     * 2. 将加密后的字符串反纺成 byte[] 数组
     * 3. 将加密内容解密
     *
     * @param content
     * @return
     */
    public static String aesDecode(String content) {
        try {
            //1. 构造密钥生成器,指定为AES算法,不区分大小写
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            //2.根据ecnodeRules规则初始化密钥生成器
            //生成一个128位的随机源,根据传入的字节数组
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(ENCODE_RULES.getBytes());
            keyGenerator.init(128, random);
            //3.产生原始对称密钥
            SecretKey originalKey = keyGenerator.generateKey();
            //4.获得原始对称密钥的字节数组
            byte[] raw = originalKey.getEncoded();
            //5.根据字节数组生成AES密钥
            SecretKey key = new SecretKeySpec(raw, "AES");
            //6.根据指定算法AES自成密码器
            Cipher cipher = Cipher.getInstance("AES");
            //7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY
            cipher.init(Cipher.DECRYPT_MODE, key);
            //8.将加密并编码后的内容解码成字节数组
            byte[] byteContent = new BASE64Decoder().decodeBuffer(content);
            /*
             * 解密
             */
            byte[] byteDecode = cipher.doFinal(byteContent);
            String aesDecode = new String(byteDecode, "utf-8");
            return aesDecode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        //如果有错就返回 null
        return null;
    }

猜你喜欢

转载自blog.csdn.net/yin__ren/article/details/81778276