首页 > 编程语言 > 详细

java 数据传输的加密和解密

时间:2020-08-27 17:02:03      阅读:182      评论:0      收藏:0      [点我收藏+]
    
    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;
        }
    }

 

java 数据传输的加密和解密

原文:https://www.cnblogs.com/liangblog/p/13572098.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!