java 加密解密工具类MD5

Myth丶恋晨 2022-12-11 02:24 791阅读 0赞

package com.sh.springboottdemo2.util;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.SecureRandom;

public class EncryptUtil {
public static final String MD5 = “MD5”;
public static final String SHA1 = “SHA1”;
public static final String HmacMD5 = “HmacMD5”;
public static final String HmacSHA1 = “HmacSHA1”;
public static final String DES = “DES”;
public static final String AES = “AES”;

  1. /\*\*编码格式;默认使用uft-8\*/
  2. public String charset = "utf-8";
  3. /\*\*DES\*/
  4. public int keysizeDES = 0;
  5. /\*\*AES\*/
  6. public int keysizeAES = 128;
  7. public static EncryptUtil me;
  8. private EncryptUtil()\{
  9. //单例
  10. \}
  11. //双重锁
  12. public static EncryptUtil getInstance()\{
  13. if (me==null) \{
  14. synchronized (EncryptUtil.class) \{
  15. if(me == null)\{
  16. me = new EncryptUtil();
  17. \}
  18. \}
  19. \}
  20. return me;
  21. \}
  22. /\*\*
  23. \* 使用MessageDigest进行单向加密(无密码)
  24. \* @param res 被加密的文本
  25. \* @param algorithm 加密算法名称
  26. \* @return
  27. \*/
  28. private String messageDigest(String res,String algorithm)\{
  29. try \{
  30. MessageDigest md = MessageDigest.getInstance(algorithm);
  31. byte\[\] resBytes = charset==null?res.getBytes():res.getBytes(charset);
  32. return base64(md.digest(resBytes));
  33. \} catch (Exception e) \{
  34. e.printStackTrace();
  35. \}
  36. return null;
  37. \}
  38. /\*\*
  39. \* 使用KeyGenerator进行单向/双向加密(可设密码)
  40. \* @param res 被加密的原文
  41. \* @param algorithm 加密使用的算法名称
  42. \* @param key 加密使用的秘钥
  43. \* @return
  44. \*/
  45. private String keyGeneratorMac(String res,String algorithm,String key)\{
  46. try \{
  47. SecretKey sk = null;
  48. if (key==null) \{
  49. KeyGenerator kg = KeyGenerator.getInstance(algorithm);
  50. sk = kg.generateKey();
  51. \}else \{
  52. byte\[\] keyBytes = charset==null?key.getBytes():key.getBytes(charset);
  53. sk = new SecretKeySpec(keyBytes, algorithm);
  54. \}
  55. Mac mac = Mac.getInstance(algorithm);
  56. mac.init(sk);
  57. byte\[\] result = mac.doFinal(res.getBytes());
  58. return base64(result);
  59. \} catch (Exception e) \{
  60. e.printStackTrace();
  61. \}
  62. return null;
  63. \}
  64. /\*\*
  65. \* 使用KeyGenerator双向加密,DES/AES,注意这里转化为字符串的时候是将2进制转为16进制格式的字符串,不是直接转,因为会出错
  66. \* @param res 加密的原文
  67. \* @param algorithm 加密使用的算法名称
  68. \* @param key 加密的秘钥
  69. \* @param keysize
  70. \* @param isEncode
  71. \* @return
  72. \*/
  73. private String keyGeneratorES(String res,String algorithm,String key,int keysize,boolean isEncode)\{
  74. try \{
  75. KeyGenerator kg = KeyGenerator.getInstance(algorithm);
  76. if (keysize == 0) \{
  77. byte\[\] keyBytes = charset==null?key.getBytes():key.getBytes(charset);
  78. kg.init(new SecureRandom(keyBytes));
  79. \}else if (key==null) \{
  80. kg.init(keysize);
  81. \}else \{
  82. byte\[\] keyBytes = charset==null?key.getBytes():key.getBytes(charset);
  83. kg.init(keysize, new SecureRandom(keyBytes));
  84. \}
  85. SecretKey sk = kg.generateKey();
  86. SecretKeySpec sks = new SecretKeySpec(sk.getEncoded(), algorithm);
  87. Cipher cipher = Cipher.getInstance(algorithm);
  88. if (isEncode) \{
  89. cipher.init(Cipher.ENCRYPT\_MODE, sks);
  90. byte\[\] resBytes = charset==null?res.getBytes():res.getBytes(charset);
  91. return parseByte2HexStr(cipher.doFinal(resBytes));
  92. \}else \{
  93. cipher.init(Cipher.DECRYPT\_MODE, sks);
  94. return new String(cipher.doFinal(parseHexStr2Byte(res)));
  95. \}
  96. \} catch (Exception e) \{
  97. e.printStackTrace();
  98. \}
  99. return null;
  100. \}
  101. private String base64(byte\[\] res)\{
  102. return Base64.encode(res);
  103. \}
  104. /\*\*将二进制转换成16进制 \*/
  105. public static String parseByte2HexStr(byte buf\[\]) \{
  106. StringBuffer sb = new StringBuffer();
  107. for (int i = 0; i < buf.length; i++) \{
  108. String hex = Integer.toHexString(buf\[i\] & 0xFF);
  109. if (hex.length() == 1) \{
  110. hex = '0' + hex;
  111. \}
  112. sb.append(hex.toUpperCase());
  113. \}
  114. return sb.toString();
  115. \}
  116. /\*\*16进制转换为二进制\*/
  117. public static byte\[\] parseHexStr2Byte(String hexStr) \{
  118. if (hexStr.length() < 1)
  119. return null;
  120. byte\[\] result = new byte\[hexStr.length()/2\];
  121. for (int i = 0;i< hexStr.length()/2; i++) \{
  122. int high = Integer.parseInt(hexStr.substring(i\*2, i\*2+1), 16);
  123. int low = Integer.parseInt(hexStr.substring(i\*2+1, i\*2+2), 16);
  124. result\[i\] = (byte) (high \* 16 + low);
  125. \}
  126. return result;
  127. \}
  128. /\*\*
  129. \* md5加密算法进行加密(不可逆)
  130. \* @param res 需要加密的原文
  131. \* @return
  132. \*/
  133. public String MD5(String res) \{
  134. return messageDigest(res, MD5);
  135. \}
  136. /\*\*
  137. \* md5加密算法进行加密(不可逆)
  138. \* @param res 需要加密的原文
  139. \* @param key 秘钥
  140. \* @return
  141. \*/
  142. public String MD5(String res, String key) \{
  143. return keyGeneratorMac(res, HmacMD5, key);
  144. \}
  145. /\*\*
  146. \* 使用SHA1加密算法进行加密(不可逆)
  147. \* @param res 需要加密的原文
  148. \* @return
  149. \*/
  150. public String SHA1(String res) \{
  151. return messageDigest(res, SHA1);
  152. \}
  153. /\*\*
  154. \* 使用SHA1加密算法进行加密(不可逆)
  155. \* @param res 需要加密的原文
  156. \* @param key 秘钥
  157. \* @return
  158. \*/
  159. public String SHA1(String res, String key) \{
  160. return keyGeneratorMac(res, HmacSHA1, key);
  161. \}
  162. /\*\*
  163. \* 使用DES加密算法进行加密(可逆)
  164. \* @param res 需要加密的原文
  165. \* @param key 秘钥
  166. \* @return
  167. \*/
  168. public String DESencode(String res, String key) \{
  169. return keyGeneratorES(res, DES, key, keysizeDES, true);
  170. \}
  171. /\*\*
  172. \* 对使用DES加密算法的密文进行解密(可逆)
  173. \* @param res 需要解密的密文
  174. \* @param key 秘钥
  175. \* @return
  176. \*/
  177. public String DESdecode(String res, String key) \{
  178. return keyGeneratorES(res, DES, key, keysizeDES, false);
  179. \}
  180. /\*\*
  181. \* 使用AES加密算法经行加密(可逆)
  182. \* @param res 需要加密的密文
  183. \* @param key 秘钥
  184. \* @return
  185. \*/
  186. public String AESencode(String res, String key) \{
  187. return keyGeneratorES(res, AES, key, keysizeAES, true);
  188. \}
  189. /\*\*
  190. \* 对使用AES加密算法的密文进行解密
  191. \* @param res 需要解密的密文
  192. \* @param key 秘钥
  193. \* @return
  194. \*/
  195. public String AESdecode(String res, String key) \{
  196. return keyGeneratorES(res, AES, key, keysizeAES, false);
  197. \}
  198. /\*\*
  199. \* 使用异或进行加密
  200. \* @param res 需要加密的密文
  201. \* @param key 秘钥
  202. \* @return
  203. \*/
  204. public String XORencode(String res, String key) \{
  205. byte\[\] bs = res.getBytes();
  206. for (int i = 0; i < bs.length; i++) \{
  207. bs\[i\] = (byte) ((bs\[i\]) ^ key.hashCode());
  208. \}
  209. return parseByte2HexStr(bs);
  210. \}
  211. /\*\*
  212. \* 使用异或进行解密
  213. \* @param res 需要解密的密文
  214. \* @param key 秘钥
  215. \* @return
  216. \*/
  217. public String XORdecode(String res, String key) \{
  218. byte\[\] bs = parseHexStr2Byte(res);
  219. for (int i = 0; i < bs.length; i++) \{
  220. bs\[i\] = (byte) ((bs\[i\]) ^ key.hashCode());
  221. \}
  222. return new String(bs);
  223. \}
  224. /\*\*
  225. \* 直接使用异或(第一调用加密,第二次调用解密)
  226. \* @param res 密文
  227. \* @param key 秘钥
  228. \* @return
  229. \*/
  230. public int XOR(int res, String key) \{
  231. return res ^ key.hashCode();
  232. \}
  233. /\*\*
  234. \* 使用Base64进行加密
  235. \* @param res 密文
  236. \* @return
  237. \*/
  238. public String Base64Encode(String res) \{
  239. return Base64.encode(res.getBytes());
  240. \}
  241. /\*\*
  242. \* 使用Base64进行解密
  243. \* @param res
  244. \* @return
  245. \*/
  246. public String Base64Decode(String res) \{
  247. return new String(Base64.decode(res));
  248. \}

}

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTcyMjkyOA_size_16_color_FFFFFF_t_70

发表评论

表情:
评论列表 (有 0 条评论,791人围观)

还没有评论,来说两句吧...

相关阅读