Chinaunix首页 | 论坛 | 博客
  • 博客访问: 165181
  • 博文数量: 56
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 593
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-18 09:59
文章分类

全部博文(56)

文章存档

2019年(1)

2018年(26)

2016年(1)

2015年(6)

2014年(22)

我的朋友

分类: Java

2018-06-29 14:11:49


一些关键的业务主键id,  通过post/get请求提交时, 需要进行加密/解密,
避免手动修改参数后提交, 提高安全性

包含:
  • 对id进行加密
  • 对加了密的id进行解密
  • 通用加密方法 -- 对字符串进行加密
  • 通用解密方法 -- 对字符串进行解密
  • Base64加密
  • Base64解密

点击(此处)折叠或打开

  1. import org.apache.commons.codec.binary.Base64;
  2. import org.apache.commons.lang.StringUtils;

  3. import javax.crypto.Cipher;
  4. import javax.crypto.spec.SecretKeySpec;
  5. import java.io.UnsupportedEncodingException;

  6. /**
  7.  * 加密解密工具类
  8.  * 采用AES加密算法
  9.  */
  10. public class EncryptionUtil {
  11.     /**
  12.      * 密钥
  13.      */
  14.     private static final String KEY = "xxx";
  15.     /**
  16.      * 密钥算法
  17.      */
  18.     private static final String ALGORITHM = "AES";
  19.     /**
  20.      * 加解密算法/工作模式/填充方式
  21.      */
  22.     private static final String ALGORITHM_STR = "AES/ECB/PKCS5Padding";

  23.     /**
  24.      * 对id进行加密
  25.      * @param bizId 业务id
  26.      * @return 密文
  27.      */
  28.     public static String encryptBizId(Integer bizId) {
  29.         if (bizId == null || bizId == 0) {
  30.             return null;
  31.         }

  32.         return encryptData(bizId.toString());
  33.     }

  34.     /**
  35.      * 对加了密的id进行解密
  36.      * @param ciphertext 密文
  37.      * @return 业务id
  38.      */
  39.     public static Integer decryptBizId(String ciphertext) {
  40.         if (ciphertext == null || ciphertext.length() == 0) {
  41.             return null;
  42.         }
  43.         String plaintext = decryptData(ciphertext);
  44.         if (plaintext == null) {
  45.             return null;
  46.         }

  47.         return Integer.valueOf(plaintext);
  48.     }

  49.     /**
  50.      * 通用加密方法 -- 对字符串进行加密
  51.      * @param data 明文
  52.      * @return 密文
  53.      */
  54.     public static String encryptData(String data) {
  55.         if (StringUtils.isEmpty(data)) {
  56.             return null;
  57.         }

  58.         try {
  59.             // SecretKeySpec类是KeySpec接口的实现类,用于构建秘密密钥规范
  60.             SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
  61.             // 创建密码器
  62.             Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
  63.             cipher.init(Cipher.ENCRYPT_MODE, keySpec);
  64.             byte[] encrypted = cipher.doFinal(data.getBytes());

  65.             return byte2hex(encrypted).toLowerCase();
  66.         } catch (Exception e) {
  67.             LogUtil.error("EncryptionUtil#encryptData#data:" + data, e);
  68.             return null;
  69.         }
  70.     }

  71.     /**
  72.      * 通用解密方法 -- 对字符串进行解密
  73.      * @param data 密文
  74.      * @return 明文
  75.      */
  76.     public static String decryptData(String data) {
  77.         if (StringUtils.isEmpty(data)) {
  78.             return null;
  79.         }

  80.         try {
  81.             // SecretKeySpec类是KeySpec接口的实现类,用于构建秘密密钥规范
  82.             SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
  83.             // 创建密码器
  84.             Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
  85.             cipher.init(Cipher.DECRYPT_MODE, keySpec);

  86.             return new String(cipher.doFinal(hex2byte(data)));
  87.         } catch (Exception e) {
  88.             LogUtil.debug("EncryptionUtil#decryptData#data" + data);
  89.             return null;
  90.         }

  91.     }

  92.     /**
  93.      * byte数组
  94.      * @param strhex hex字符串
  95.      * @return
  96.      */
  97.     private static byte[] hex2byte(String strhex) {
  98.         if (strhex == null) {
  99.             return null;
  100.         }
  101.         int l = strhex.length();
  102.         if (l % 2 == 1) {
  103.             return null;
  104.         }
  105.         byte[] b = new byte[l / 2];
  106.         for (int i = 0; i != l / 2; i++) {
  107.             b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),
  108.                     16);
  109.         }
  110.         return b;
  111.     }

  112.     /**
  113.      * byte数组 转 hex字符串
  114.      * @param b byte数组
  115.      * @return
  116.      */
  117.     private static String byte2hex(byte[] b) {
  118.         String hs = "";
  119.         String stmp = "";
  120.         for (int n = 0; n < b.length; n++) {
  121.             stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
  122.             if (stmp.length() == 1) {
  123.                 hs = hs + "0" + stmp;
  124.             } else {
  125.                 hs = hs + stmp;
  126.             }
  127.         }
  128.         return hs.toUpperCase();
  129.     }

  130.     public static void main(String[] args) throws Exception {
  131.         long lStart = System.currentTimeMillis();
  132.         String enString = encryptBizId(1);

  133.         long lUseTime = System.currentTimeMillis() - lStart;
  134.         System.out.println("加密后的字串是:" + enString);
  135.         System.out.println("加密耗时:" + lUseTime + "毫秒");
  136.         // 解密
  137.         lStart = System.currentTimeMillis();
  138.         Integer deString = EncryptionUtil.decryptBizId("84d692dcc66e5477d80151b7394227fa");
  139.         System.out.println("解密后的字串是:" + deString);
  140.         lUseTime = System.currentTimeMillis() - lStart;
  141.         System.out.println("解密耗时:" + lUseTime + "毫秒");

  142.         System.out.println(encryptBizId(391));
  143.     }


  144.     /**
  145.      * 加密
  146.      * @return
  147.      * @throws Exception
  148.      */
  149.     public static String encryptInBase64(String data) {
  150.         if(StringUtils.isEmpty(data)) {
  151.             return null;
  152.         }
  153.         try {
  154.             return getBASE64(encryptData(data));
  155.         } catch (Exception e) {
  156.             LogUtil.error("", e);
  157.             return null;
  158.         }
  159.     }

  160.     /**
  161.      * 解密
  162.      * @return
  163.      * @throws Exception
  164.      */
  165.     public static String desEncryptInBase64(String data) throws Exception {
  166.         if(StringUtils.isEmpty(data)) {
  167.             return null;
  168.         }
  169.         try {
  170.             return decryptData(getFromBASE64(data));
  171.         } catch (Exception e) {
  172.             LogUtil.error("", e);
  173.             return null;
  174.         }
  175.     }

  176.     /**
  177.      * 解析base64获取字符串
  178.      * @param s
  179.      * @return
  180.      */
  181.     public static String getFromBASE64(String s) {
  182.         if (s == null) return null;

  183.         try {
  184.             return new String(Base64.decodeBase64(s.getBytes("utf-8")), "utf-8");
  185.         } catch (UnsupportedEncodingException e) {
  186.             return null;
  187.         }
  188.     }

  189.     /**
  190.      * 获取字符串的base64
  191.      * @param s
  192.      * @return
  193.      */
  194.     public static String getBASE64(String s) {
  195.         if (s == null) return null;
  196.         try {
  197.             return new String(Base64.encodeBase64(s.getBytes("utf-8")), "utf-8");
  198.         } catch (Exception e) {
  199.             return null;
  200.         }
  201.     }

  202. }

阅读(913) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~