DesUtil.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package jnpf.util;
  2. import cn.hutool.core.codec.Base64;
  3. import cn.hutool.core.util.HexUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import cn.hutool.crypto.SecureUtil;
  6. import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
  7. import cn.hutool.crypto.symmetric.SymmetricCrypto;
  8. import jnpf.constant.GlobalConst;
  9. import jnpf.properties.SecurityProperties;
  10. import javax.crypto.Cipher;
  11. import javax.crypto.KeyGenerator;
  12. import javax.crypto.Mac;
  13. import javax.crypto.SecretKey;
  14. import javax.crypto.spec.SecretKeySpec;
  15. import java.nio.charset.StandardCharsets;
  16. import java.security.MessageDigest;
  17. import java.security.SecureRandom;
  18. /**
  19. *
  20. * @author JNPF开发平台组
  21. * @version V3.1.0
  22. * @copyright 引迈信息技术有限公司
  23. * @date 2021/3/16 10:51
  24. */
  25. public class DesUtil {
  26. private static final String MD5 = "MD5";
  27. private static final String SHA1 = "SHA1";
  28. private static final String HMAC_MD_5 = "HmacMD5";
  29. private static final String HMAC_SHA_1 = "HmacSHA1";
  30. private static final String DES = "DES";
  31. private static final String AES = "AES";
  32. /**编码格式;默认使用uft-8*/
  33. private static String charset = "utf-8";
  34. /**DES*/
  35. private static int keysizeDES = 0;
  36. /**AES*/
  37. private static int keysizeAES = 128;
  38. /**
  39. * 使用MessageDigest进行单向加密(无密码)
  40. * @param res 被加密的文本
  41. * @param algorithm 加密算法名称
  42. * @return
  43. */
  44. private static 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 static 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 static String keyGeneratorEs(String res, String algorithm, String key, int keysize, boolean isEncode){
  90. try {
  91. SecretKeySpec sks = null;
  92. KeyGenerator kg = KeyGenerator.getInstance(algorithm);
  93. if (key==null) {
  94. kg.init(keysize);
  95. }else {
  96. SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
  97. random.setSeed(key.getBytes());
  98. if (keysize == 0) {
  99. kg.init(random);
  100. } else {
  101. kg.init(keysize, random);
  102. }
  103. }
  104. SecretKey sk = kg.generateKey();
  105. sks = new SecretKeySpec(sk.getEncoded(), algorithm);
  106. Cipher cipher = Cipher.getInstance(algorithm);
  107. if (isEncode) {
  108. cipher.init(Cipher.ENCRYPT_MODE, sks);
  109. byte[] resBytes = charset==null?res.getBytes():res.getBytes(charset);
  110. return parseByte2HexStr(cipher.doFinal(resBytes));
  111. }else {
  112. cipher.init(Cipher.DECRYPT_MODE, sks);
  113. return new String(cipher.doFinal(parseHexStr2Byte(res)));
  114. }
  115. } catch (Exception e) {
  116. e.printStackTrace();
  117. }
  118. return null;
  119. }
  120. private static String base64(byte[] res){
  121. return Base64.encode(res);
  122. }
  123. /**将二进制转换成16进制 */
  124. public static String parseByte2HexStr(byte[] buf) {
  125. StringBuffer sb = new StringBuffer();
  126. for (int i = 0; i < buf.length; i++) {
  127. String hex = Integer.toHexString(buf[i] & 0xFF);
  128. if (hex.length() == 1) {
  129. hex = '0' + hex;
  130. }
  131. sb.append(hex.toUpperCase());
  132. }
  133. return sb.toString();
  134. }
  135. /**将16进制转换为二进制*/
  136. public static byte[] parseHexStr2Byte(String hexStr) {
  137. if (hexStr.length() < 1) {
  138. return null;
  139. }
  140. byte[] result = new byte[hexStr.length()/2];
  141. for (int i = 0;i< hexStr.length()/2; i++) {
  142. int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
  143. int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
  144. result[i] = (byte) (high * 16 + low);
  145. }
  146. return result;
  147. }
  148. /**
  149. * md5加密算法进行加密(不可逆)
  150. * @param res 需要加密的原文
  151. * @return
  152. */
  153. public static String md5(String res) {
  154. return messageDigest(res, MD5);
  155. }
  156. /**
  157. * md5加密算法进行加密(不可逆)
  158. * @param res 需要加密的原文
  159. * @param key 秘钥
  160. * @return
  161. */
  162. public static String md5(String res, String key) {
  163. return keyGeneratorMac(res, HMAC_MD_5, key);
  164. }
  165. /**
  166. * 使用SHA1加密算法进行加密(不可逆)
  167. * @param res 需要加密的原文
  168. * @return
  169. */
  170. public static String sha1(String res) {
  171. return messageDigest(res, SHA1);
  172. }
  173. /**
  174. * 使用SHA1加密算法进行加密(不可逆)
  175. * @param res 需要加密的原文
  176. * @param key 秘钥
  177. * @return
  178. */
  179. public static String sha1(String res, String key) {
  180. return keyGeneratorMac(res, HMAC_SHA_1, key);
  181. }
  182. /**
  183. * 使用DES加密算法进行加密(可逆)
  184. * @param res 需要加密的原文
  185. * @return
  186. */
  187. public static String desEncode(String res) {
  188. return keyGeneratorEs(res, DES, SecurityProperties.getInstance().getSecurityKey(), keysizeDES, true);
  189. }
  190. /**
  191. * 对使用DES加密算法的密文进行解密(可逆)
  192. * @param res 需要解密的密文
  193. * @return
  194. */
  195. public static String desDecode(String res) {
  196. return keyGeneratorEs(res, DES, SecurityProperties.getInstance().getSecurityKey(), keysizeDES, false);
  197. }
  198. /**
  199. * 使用AES加密算法经行加密(可逆)
  200. * @param res 需要加密的密文
  201. * @return
  202. */
  203. public static String aesEncode(String res) {
  204. return keyGeneratorEs(res, AES, SecurityProperties.getInstance().getSecurityKey(), keysizeAES, true);
  205. }
  206. /**
  207. * 对使用AES加密算法的密文进行解密
  208. * @param res 需要解密的密文
  209. * @return
  210. */
  211. public static String aesDecode(String res) {
  212. return keyGeneratorEs(res, AES, SecurityProperties.getInstance().getSecurityKey(), keysizeAES, false);
  213. }
  214. /**
  215. * 使用Base64进行加密
  216. * @param res 密文
  217. * @return
  218. */
  219. public static String base64Encode(String res) {
  220. return Base64.encode(res.getBytes());
  221. }
  222. /**
  223. * 使用Base64进行解密
  224. * @param res
  225. * @return
  226. */
  227. public static String base64Decode(String res) {
  228. return new String(Base64.decode(res));
  229. }
  230. /**
  231. * aes des 加密解密
  232. * 加密 输出hexstr
  233. * 解密 输入hexstr
  234. * @param res
  235. * @return
  236. */
  237. public static String aesOrDecode(String res,boolean isEncode,boolean isAes){
  238. byte[] bytes;
  239. if(isEncode){
  240. bytes = StrUtil.bytes(res, GlobalConst.DEFAULT_CHARSET);
  241. }else{
  242. bytes = HexUtil.decodeHex(res);
  243. }
  244. bytes = aesOrDecode(bytes, isEncode, isAes, SecurityProperties.getInstance().getSecurityKey());
  245. if(isEncode){
  246. return HexUtil.encodeHexStr(bytes);
  247. }else{
  248. return StrUtil.str(bytes, GlobalConst.DEFAULT_CHARSET);
  249. }
  250. }
  251. /**
  252. * aes des 加密解密
  253. * @param res
  254. * @return
  255. */
  256. public static byte[] aesOrDecode(byte[] res, boolean isEncode, boolean isAes, String key){
  257. //KEY 16/24/32,
  258. SymmetricAlgorithm symmetricAlgorithm = isAes ? SymmetricAlgorithm.AES : SymmetricAlgorithm.DES;
  259. byte[] keyBytes = SecureUtil.generateKey(symmetricAlgorithm.getValue(), key.getBytes(GlobalConst.DEFAULT_CHARSET)).getEncoded();
  260. SymmetricCrypto symmetricCrypto = new SymmetricCrypto(symmetricAlgorithm, keyBytes);
  261. if(isEncode){
  262. return symmetricCrypto.encrypt(res);
  263. }else{
  264. return symmetricCrypto.decrypt(res);
  265. }
  266. }
  267. }