zoukankan      html  css  js  c++  java
  • java 数据传输的加密和解密

        
        public static String CEncrypt(String content, String iv, String key) throws Exception {
            byte[] raw = key.getBytes("utf-8");
            SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec(iv.getBytes("utf-8"));
            cipher.init(1, secretKeySpec, ips);
            byte[] encrypted = cipher.doFinal(content.getBytes("utf-8"));
            return Base64.getEncoder().encodeToString(encrypted);
        }
        
        public static String CDecrypt(String content, String iv, String key){
            try {
                byte[] raw = key.getBytes("utf-8");
                SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES");
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                IvParameterSpec ips = new IvParameterSpec(iv.getBytes("utf-8"));
                cipher.init(2, secretKeySpec, ips);
                byte[] cbcBytes = Base64.getDecoder().decode(content);
                byte[] original = cipher.doFinal(cbcBytes);
                return new String(original, Charset.forName("utf-8"));
            } catch (Exception var9) {
                var9.printStackTrace();
                return null;
            }
        }
  • 相关阅读:
    HTML DOM 事件
    js实现键盘数字输入
    js实现键盘数字输入
    onbeforeunload事件兼容性操作
    onbeforeunload事件兼容性操作
    window.event对象详尽解析
    git简单使用教程
    PHP中奖概率写法
    PHP替代session的方法
    nginx实现负载均衡
  • 原文地址:https://www.cnblogs.com/liangblog/p/13572098.html
Copyright © 2011-2022 走看看