zoukankan      html  css  js  c++  java
  • 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));
    }
  • 相关阅读:
    h5 拍照上传 代码
    java jdbc 链接本地mysql数据库 报错 Access denied for user 'root'@'localhost' (using password: YES)
    react.js 中对props 的理解
    react.js 如何 设置页面div 背景图片
    关于Vue.js 和 react.js 的异同
    如何用 npm ,搭建react 项目
    如何进行vue vux版本更新
    js 继承 函数
    absolute 和 z-index妙用
    关于 white-space: pre-wrap;的灵异现象
  • 原文地址:https://www.cnblogs.com/hedianwei/p/5671356.html
Copyright © 2011-2022 走看看