4c2c6c905f8bacb2d36ce2496f957dfff49e9d3f.svn-base 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.yihu.platform.utils;
  2. import java.io.IOException;
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.security.SecureRandom;
  6. import javax.crypto.Cipher;
  7. import javax.crypto.SecretKey;
  8. import javax.crypto.SecretKeyFactory;
  9. import javax.crypto.spec.DESKeySpec;
  10. import sun.misc.BASE64Decoder;
  11. import sun.misc.BASE64Encoder;
  12. /**
  13. * ClassName: DesUtil
  14. * @Description: 字符串加密 、解密 类
  15. * @author houzq
  16. * @Company www.yihu.com
  17. * @date 2017-8-31 下午03:12:54
  18. */
  19. public class DesUtil {
  20. private final static String DES = "DES";
  21. private final static String key = "jkzl_!@#$%_";
  22. /**
  23. * Description 根据键值进行加密
  24. *
  25. * @param data
  26. * @param key
  27. * 加密键byte数组
  28. * @return
  29. * @throws Exception
  30. */
  31. public static String encrypt(String data, String key) throws Exception {
  32. byte[] bt = encrypt(data.getBytes(), key.getBytes());
  33. String strs = new BASE64Encoder().encode(bt);
  34. return strs;
  35. }
  36. public static String encrypt(String data) throws Exception {
  37. byte[] bt = encrypt(data.getBytes(), key.getBytes());
  38. String strs = new BASE64Encoder().encode(bt);
  39. return strs;
  40. }
  41. /**
  42. * Description 根据键值进行解密
  43. *
  44. * @param data
  45. * @param key
  46. * 加密键byte数组
  47. * @return
  48. * @throws IOException
  49. * @throws Exception
  50. */
  51. public static String decrypt(String data, String key) throws IOException, Exception {
  52. if (data == null)
  53. return null;
  54. BASE64Decoder decoder = new BASE64Decoder();
  55. byte[] buf = decoder.decodeBuffer(data);
  56. byte[] bt = decrypt(buf, key.getBytes());
  57. return new String(bt);
  58. }
  59. public static String decrypt(String data) throws IOException, Exception {
  60. if (data == null)
  61. return null;
  62. BASE64Decoder decoder = new BASE64Decoder();
  63. byte[] buf = decoder.decodeBuffer(data);
  64. byte[] bt = decrypt(buf, key.getBytes());
  65. return new String(bt);
  66. }
  67. /**
  68. * Description 根据键值进行加密
  69. *
  70. * @param data
  71. * @param key
  72. * 加密键byte数组
  73. * @return
  74. * @throws Exception
  75. */
  76. private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
  77. // 生成一个可信任的随机数源
  78. SecureRandom sr = new SecureRandom();
  79. // 从原始密钥数据创建DESKeySpec对象
  80. DESKeySpec dks = new DESKeySpec(key);
  81. // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
  82. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
  83. SecretKey securekey = keyFactory.generateSecret(dks);
  84. // Cipher对象实际完成加密操作
  85. Cipher cipher = Cipher.getInstance(DES);
  86. // 用密钥初始化Cipher对象
  87. cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
  88. return cipher.doFinal(data);
  89. }
  90. /**
  91. * Description 根据键值进行解密
  92. *
  93. * @param data
  94. * @param key
  95. * 加密键byte数组
  96. * @return
  97. * @throws Exception
  98. */
  99. private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
  100. // 生成一个可信任的随机数源
  101. SecureRandom sr = new SecureRandom();
  102. // 从原始密钥数据创建DESKeySpec对象
  103. DESKeySpec dks = new DESKeySpec(key);
  104. // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
  105. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
  106. SecretKey securekey = keyFactory.generateSecret(dks);
  107. // Cipher对象实际完成解密操作
  108. Cipher cipher = Cipher.getInstance(DES);
  109. // 用密钥初始化Cipher对象
  110. cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
  111. return cipher.doFinal(data);
  112. }
  113. /**
  114. * MD5工具
  115. *
  116. * @param string
  117. * @return
  118. */
  119. public static String md5(String string) {
  120. MessageDigest md = null;
  121. try {
  122. md = MessageDigest.getInstance("md5");
  123. md.update(string.getBytes());
  124. byte[] md5Bytes = md.digest();
  125. return bytes2Hex(md5Bytes);
  126. } catch (NoSuchAlgorithmException e) {
  127. e.printStackTrace();
  128. }
  129. return null;
  130. }
  131. private static String bytes2Hex(byte[] byteArray) {
  132. StringBuffer strBuf = new StringBuffer();
  133. for (int i = 0; i < byteArray.length; i++) {
  134. if (byteArray[i] >= 0 && byteArray[i] < 16) {
  135. strBuf.append("0");
  136. }
  137. strBuf.append(Integer.toHexString(byteArray[i] & 0xFF));
  138. }
  139. return strBuf.toString();
  140. }
  141. /**
  142. * BASE64 编码
  143. * @param s
  144. * @return
  145. */
  146. public static String getBASE64(String s) {
  147. if (s == null)
  148. return null;
  149. return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
  150. }
  151. // 将 BASE64 编码的字符串 s 进行解码
  152. public static String getFromBASE64(String s) {
  153. if (s == null)
  154. return null;
  155. BASE64Decoder decoder = new BASE64Decoder();
  156. try {
  157. byte[] b = decoder.decodeBuffer(s);
  158. return new String(b);
  159. } catch (Exception e) {
  160. return null;
  161. }
  162. }
  163. }