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));
    }
  • 相关阅读:
    kubectl exec 执行 容器命令
    centos下通过yum安装redis-cli
    windows下 使用ip地址反查主机名的命令
    O365(世纪互联)SharePoint 之文档库使用小记
    SharePoint 2016 图文安装教程
    SharePoint 2013 激活标题字段外的Menu菜单
    SharePoint 2013 定制搜索显示模板(二)
    SharePoint 2013 定制搜索显示模板
    SharePoint 2013 网站搜索规则的使用示例
    SharePoint 2013 搜索功能,列表项目不能完全被索引
  • 原文地址:https://www.cnblogs.com/hedianwei/p/5671356.html
Copyright © 2011-2022 走看看