DESUtil.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package com.jkcredit.identity.util;
  2. import org.apache.commons.logging.Log;
  3. import org.apache.commons.logging.LogFactory;
  4. import sun.misc.BASE64Encoder;
  5. import javax.crypto.Cipher;
  6. import javax.crypto.SecretKey;
  7. import javax.crypto.SecretKeyFactory;
  8. import javax.crypto.spec.DESKeySpec;
  9. import java.io.UnsupportedEncodingException;
  10. import java.security.SecureRandom;
  11. /**
  12. * @description:
  13. * @author: sunzhaoning
  14. * @create: 2019-05-21 10:48
  15. * @version: V1.0
  16. **/
  17. public class DESUtil {
  18. private static final Log QUERY_LOGGER = LogFactory.getLog("QUERY_LOGGER");
  19. /**
  20. * @param data
  21. * @return
  22. * @throws Exception
  23. * @Method: encrypt
  24. * @Description: 加密数据
  25. * @date 2016年7月26日
  26. */
  27. public static String encrypt(String data, String password) { //对string进行BASE64Encoder转换
  28. byte[] bt = encryptByKey(data.getBytes(), password);
  29. BASE64Encoder base64en = new BASE64Encoder();
  30. bt = base64en.encode(bt).getBytes();
  31. try {
  32. return new String(bt,"UTF-8");
  33. } catch (UnsupportedEncodingException e) {
  34. e.printStackTrace();
  35. }
  36. return null;
  37. }
  38. /**
  39. * @param data
  40. * @return
  41. * @throws Exception
  42. * @Method: encrypt
  43. * @Description: 解密数据
  44. * @date 2016年7月26日
  45. */
  46. public static String decrypt(String data, String password){
  47. String result = "";
  48. try {
  49. sun.misc.BASE64Decoder base64en = new sun.misc.BASE64Decoder();
  50. byte[] bt = decrypt(base64en.decodeBuffer(data), password);
  51. result = new String(bt,"UTF-8");
  52. } catch (Exception e) {
  53. QUERY_LOGGER.info("decrypt Exception:" + e);
  54. }
  55. return result;
  56. }
  57. /**
  58. * 加密
  59. *
  60. * @param datasource byte[]
  61. * @param key String
  62. * @return byte[]
  63. */
  64. private static byte[] encryptByKey(byte[] datasource, String key) {
  65. try {
  66. SecureRandom random = new SecureRandom();
  67. DESKeySpec desKey = new DESKeySpec(key.getBytes());
  68. //创建一个密匙工厂,然后用它把DESKeySpec转换成
  69. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  70. SecretKey securekey = keyFactory.generateSecret(desKey);
  71. //Cipher对象实际完成加密操作
  72. Cipher cipher = Cipher.getInstance("DES");
  73. //用密匙初始化Cipher对象
  74. cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
  75. //现在,获取数据并加密
  76. //正式执行加密操作
  77. return cipher.doFinal(datasource);
  78. } catch (Throwable e) {
  79. e.printStackTrace();
  80. }
  81. return null;
  82. }
  83. /**
  84. * 解密
  85. *
  86. * @param src byte[]
  87. * @param key String
  88. * @return byte[]
  89. * @throws Exception
  90. */
  91. private static byte[] decrypt(byte[] src, String key) throws Exception {
  92. // DES算法要求有一个可信任的随机数源
  93. SecureRandom random = new SecureRandom();
  94. // 创建一个DESKeySpec对象
  95. DESKeySpec desKey = new DESKeySpec(key.getBytes());
  96. // 创建一个密匙工厂
  97. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  98. // 将DESKeySpec对象转换成SecretKey对象
  99. SecretKey securekey = keyFactory.generateSecret(desKey);
  100. // Cipher对象实际完成解密操作
  101. Cipher cipher = Cipher.getInstance("DES");
  102. // 用密匙初始化Cipher对象
  103. cipher.init(Cipher.DECRYPT_MODE, securekey, random);
  104. // 真正开始解密操作
  105. return cipher.doFinal(src);
  106. }
  107. }