PasswordUtils.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package com.fdkankan.common.util;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.SecretKey;
  4. import javax.crypto.SecretKeyFactory;
  5. import javax.crypto.spec.PBEKeySpec;
  6. import javax.crypto.spec.PBEParameterSpec;
  7. import java.security.Key;
  8. import java.security.SecureRandom;
  9. public class PasswordUtils {
  10. public static char[] arr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
  11. /**
  12. * JAVA6支持以下任意一种算法 PBEWITHMD5ANDDES PBEWITHMD5ANDTRIPLEDES
  13. * PBEWITHSHAANDDESEDE PBEWITHSHA1ANDRC2_40 PBKDF2WITHHMACSHA1
  14. * */
  15. /**
  16. * 定义使用的算法为:PBEWITHMD5andDES算法
  17. */
  18. public static final String ALGORITHM = "PBEWithMD5AndDES";//加密算法
  19. public static final String Salt = "63293188";//密钥
  20. /**
  21. * 定义迭代次数为1000次
  22. */
  23. private static final int ITERATIONCOUNT = 1000;
  24. /**
  25. * 获取加密算法中使用的盐值,解密中使用的盐值必须与加密中使用的相同才能完成操作. 盐长度必须为8字节
  26. *
  27. * @return byte[] 盐值
  28. */
  29. public static byte[] getSalt() throws Exception {
  30. // 实例化安全随机数
  31. SecureRandom random = new SecureRandom();
  32. // 产出盐
  33. return random.generateSeed(8);
  34. }
  35. public static byte[] getStaticSalt() {
  36. // 产出盐
  37. return Salt.getBytes();
  38. }
  39. /**
  40. * 根据PBE密码生成一把密钥
  41. *
  42. * @param password 生成密钥时所使用的密码
  43. * @return Key PBE算法密钥
  44. */
  45. private static Key getPBEKey(String password) {
  46. // 实例化使用的算法
  47. SecretKeyFactory keyFactory;
  48. SecretKey secretKey = null;
  49. try {
  50. keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
  51. // 设置PBE密钥参数
  52. PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
  53. // 生成密钥
  54. secretKey = keyFactory.generateSecret(keySpec);
  55. } catch (Exception e) {
  56. // TODO Auto-generated catch block
  57. e.printStackTrace();
  58. }
  59. return secretKey;
  60. }
  61. /**
  62. * 加密明文字符串
  63. *
  64. * @param plaintext 待加密的明文字符串
  65. * @param password 生成密钥时所使用的密码
  66. * @param salt 盐值
  67. * @return 加密后的密文字符串
  68. * @throws Exception
  69. */
  70. public static String encrypt(String plaintext, String password, byte[] salt) {
  71. Key key = getPBEKey(password);
  72. byte[] encipheredData = null;
  73. PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, ITERATIONCOUNT);
  74. try {
  75. Cipher cipher = Cipher.getInstance(ALGORITHM);
  76. cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);
  77. encipheredData = cipher.doFinal(plaintext.getBytes());
  78. } catch (Exception e) {
  79. }
  80. return bytesToHexString(encipheredData);
  81. }
  82. /**
  83. * 解密密文字符串
  84. *
  85. * @param ciphertext 待解密的密文字符串
  86. * @param password 生成密钥时所使用的密码(如需解密,该参数需要与加密时使用的一致)
  87. * @param salt 盐值(如需解密,该参数需要与加密时使用的一致)
  88. * @return 解密后的明文字符串
  89. * @throws Exception
  90. */
  91. public static String decrypt(String ciphertext, String password, byte[] salt) {
  92. Key key = getPBEKey(password);
  93. byte[] passDec = null;
  94. PBEParameterSpec parameterSpec = new PBEParameterSpec(getStaticSalt(), ITERATIONCOUNT);
  95. try {
  96. Cipher cipher = Cipher.getInstance(ALGORITHM);
  97. cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);
  98. passDec = cipher.doFinal(hexStringToBytes(ciphertext));
  99. } catch (Exception e) {
  100. // TODO: handle exception
  101. }
  102. return new String(passDec);
  103. }
  104. /**
  105. * 将字节数组转换为十六进制字符串
  106. *
  107. * @param src 字节数组
  108. * @return
  109. */
  110. public static String bytesToHexString(byte[] src) {
  111. StringBuilder stringBuilder = new StringBuilder("");
  112. if (src == null || src.length <= 0) {
  113. return null;
  114. }
  115. for (int i = 0; i < src.length; i++) {
  116. int v = src[i] & 0xFF;
  117. String hv = Integer.toHexString(v);
  118. if (hv.length() < 2) {
  119. stringBuilder.append(0);
  120. }
  121. stringBuilder.append(hv);
  122. }
  123. return stringBuilder.toString();
  124. }
  125. /**
  126. * 将十六进制字符串转换为字节数组
  127. *
  128. * @param hexString 十六进制字符串
  129. * @return
  130. */
  131. public static byte[] hexStringToBytes(String hexString) {
  132. if (hexString == null || hexString.equals("")) {
  133. return null;
  134. }
  135. hexString = hexString.toUpperCase();
  136. int length = hexString.length() / 2;
  137. char[] hexChars = hexString.toCharArray();
  138. byte[] d = new byte[length];
  139. for (int i = 0; i < length; i++) {
  140. int pos = i * 2;
  141. d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
  142. }
  143. return d;
  144. }
  145. private static byte charToByte(char c) {
  146. return (byte) "0123456789ABCDEF".indexOf(c);
  147. }
  148. /**
  149. * 模拟前端密码加密
  150. * @param str
  151. */
  152. public static String decycptPasswordWeb(String str){
  153. int num = 2;
  154. String front = randomWord(8);
  155. String middle = randomWord(8);
  156. String end = randomWord(8);
  157. String str1 = str.substring(0, num);
  158. String str2 = str.substring(num);
  159. return front + str2 + middle + str1 + end ;
  160. }
  161. public static String randomWord(Integer min) {
  162. String str = "";
  163. Integer range = min;
  164. // 随机产生
  165. for (int i = 0; i < range; i++) {
  166. int pos = (int) Math.round(Math.random() * (arr.length - 1));
  167. str += arr[pos];
  168. }
  169. return str;
  170. }
  171. }