zoukankan      html  css  js  c++  java
  • 后台对Json数据加密、解密

    1、工具类

    package com.abc.er.util;
    import org.apache.commons.codec.binary.Base64;
    import javax.crypto.*;
    import javax.crypto.spec.SecretKeySpec;
    import java.security.SecureRandom;
    
    public class AesUtil {
        public static void main(String[] args) throws Exception {
            // aes + base  -->  aes + base
            String content =   "{"mac":"123","idfa":"123","clientIp":"456",insertTime":"test"}";
            System.out.println("加密内容:" + content);
            String key = "123abc";
            System.out.println("加密密钥和解密密钥:" + key);
            String encrypt = aesEncrypt(content, key);
            System.out.println("加密后:" +encrypt);
            String decrypt = aesDecrypt(encrypt, key);
            System.out.println("解密后:" + decrypt);
        }
    
        /**
         * 编码
         * @param bstr
         * @return String
         */
        public static String Base64encode(byte[] bstr) {
            return Base64.encodeBase64String(bstr);
        }
    
        /**
         * 解码
         * @param str
         * @return string
         */
        public static byte[] Base64decode(String str) {
            return Base64.decodeBase64(str);
        }
    
        /*
         * AES加密
         * @param content 待加密的内容
         * @param encryptKey 加密密钥
         * @return 加密后的byte[]
         * @throws Exception
         */
        public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            /*防止linux下 随机生成key*/
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(encryptKey.getBytes());
            kgen.init(128, random);
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
    
            return cipher.doFinal(content.getBytes("UTF-8"));
        }
    
        /**
         * AES加密为base 64 code
         * @param content 待加密的内容
         * @param encryptKey 加密密钥
         * @return 加密后的base 64 code
         * @throws Exception
         */
        public static String aesEncrypt(String content, String encryptKey) throws Exception {
            return Base64encode(aesEncryptToBytes(content, encryptKey));
        }
    
        /**
         * AES解密
         * @param encryptBytes 待解密的byte[]
         * @param decryptKey 解密密钥
         * @return 解密后的String
         * @throws Exception
         */
        public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) {
            byte[] decryptBytes = new byte[0];
            try {
                KeyGenerator kgen = KeyGenerator.getInstance("AES");
                /*防止linux下 随机生成key*/
                SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
                random.setSeed(decryptKey.getBytes());
                kgen.init(128, random);
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
                decryptBytes = cipher.doFinal(encryptBytes);
                return new String(decryptBytes,"UTF-8");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return decryptKey;
    
        }
    
        /**
         * 将base 64 code AES解密
         * @param encryptStr 待解密的base 64 code
         * @param decryptKey 解密密钥
         * @return 解密后的string
         * @throws Exception
         */
        public static String aesDecrypt(String encryptStr, String decryptKey){
            return aesDecryptByBytes(Base64decode(encryptStr), decryptKey);
        }
    }

    2、controller中调用

    //加密stg为需要加密的方法
    String strd = AesUtil.aesEncrypt(stg, "123abc");
    //解密
    String strd = AesUtil.aesDecrypt(stg, "123abc");
  • 相关阅读:
    APIO2019游记
    ZJOI2019赛季回顾
    「HNOI 2019」白兔之舞
    LOJ #6539 奇妙数论题
    BZOJ4314 倍数?倍数!
    伯努利数学习笔记&&Luogu P3711 仓鼠的数学题
    BZOJ 5093[Lydsy1711月赛]图的价值 线性做法
    AtCoder Grand Contest 030题解
    Codeforces Round #542 (Div. 1) 题解
    Codeforces Round #541 (Div. 2)题解
  • 原文地址:https://www.cnblogs.com/yanchaohui/p/11005037.html
Copyright © 2011-2022 走看看