EncryptUtil.java 8.5 KB

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