EncryptUtils.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package com.ruoyi.common.utils;
  2. import cn.hutool.core.codec.Base64;
  3. import cn.hutool.core.util.ArrayUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import cn.hutool.crypto.SecureUtil;
  6. import cn.hutool.crypto.SmUtil;
  7. import cn.hutool.crypto.asymmetric.KeyType;
  8. import cn.hutool.crypto.asymmetric.RSA;
  9. import cn.hutool.crypto.asymmetric.SM2;
  10. import java.nio.charset.StandardCharsets;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. /**
  14. * 安全相关工具类
  15. *
  16. * @author 老马
  17. */
  18. public class EncryptUtils {
  19. /**
  20. * 公钥
  21. */
  22. public static final String PUBLIC_KEY = "publicKey";
  23. /**
  24. * 私钥
  25. */
  26. public static final String PRIVATE_KEY = "privateKey";
  27. /**
  28. * Base64加密
  29. *
  30. * @param data 待加密数据
  31. * @return 加密后字符串
  32. */
  33. public static String encryptByBase64(String data) {
  34. return Base64.encode(data, StandardCharsets.UTF_8);
  35. }
  36. /**
  37. * Base64解密
  38. *
  39. * @param data 待解密数据
  40. * @return 解密后字符串
  41. */
  42. public static String decryptByBase64(String data) {
  43. return Base64.decodeStr(data, StandardCharsets.UTF_8);
  44. }
  45. /**
  46. * AES加密
  47. *
  48. * @param data 待解密数据
  49. * @param password 秘钥字符串
  50. * @return 加密后字符串, 采用Base64编码
  51. */
  52. public static String encryptByAes(String data, String password) {
  53. if (StrUtil.isBlank(password)) {
  54. throw new IllegalArgumentException("AES需要传入秘钥信息");
  55. }
  56. // aes算法的秘钥要求是16位、24位、32位
  57. int[] array = {16, 24, 32};
  58. if (!ArrayUtil.contains(array, password.length())) {
  59. throw new IllegalArgumentException("AES秘钥长度要求为16位、24位、32位");
  60. }
  61. return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).encryptBase64(data, StandardCharsets.UTF_8);
  62. }
  63. /**
  64. * AES加密
  65. *
  66. * @param data 待解密数据
  67. * @param password 秘钥字符串
  68. * @return 加密后字符串, 采用Hex编码
  69. */
  70. public static String encryptByAesHex(String data, String password) {
  71. if (StrUtil.isBlank(password)) {
  72. throw new IllegalArgumentException("AES需要传入秘钥信息");
  73. }
  74. // aes算法的秘钥要求是16位、24位、32位
  75. int[] array = {16, 24, 32};
  76. if (!ArrayUtil.contains(array, password.length())) {
  77. throw new IllegalArgumentException("AES秘钥长度要求为16位、24位、32位");
  78. }
  79. return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).encryptHex(data, StandardCharsets.UTF_8);
  80. }
  81. /**
  82. * AES解密
  83. *
  84. * @param data 待解密数据
  85. * @param password 秘钥字符串
  86. * @return 解密后字符串
  87. */
  88. public static String decryptByAes(String data, String password) {
  89. if (StrUtil.isBlank(password)) {
  90. throw new IllegalArgumentException("AES需要传入秘钥信息");
  91. }
  92. // aes算法的秘钥要求是16位、24位、32位
  93. int[] array = {16, 24, 32};
  94. if (!ArrayUtil.contains(array, password.length())) {
  95. throw new IllegalArgumentException("AES秘钥长度要求为16位、24位、32位");
  96. }
  97. return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).decryptStr(data, StandardCharsets.UTF_8);
  98. }
  99. /**
  100. * sm4加密
  101. *
  102. * @param data 待加密数据
  103. * @param password 秘钥字符串
  104. * @return 加密后字符串, 采用Base64编码
  105. */
  106. public static String encryptBySm4(String data, String password) {
  107. if (StrUtil.isBlank(password)) {
  108. throw new IllegalArgumentException("SM4需要传入秘钥信息");
  109. }
  110. // sm4算法的秘钥要求是16位长度
  111. int sm4PasswordLength = 16;
  112. if (sm4PasswordLength != password.length()) {
  113. throw new IllegalArgumentException("SM4秘钥长度要求为16位");
  114. }
  115. return SmUtil.sm4(password.getBytes(StandardCharsets.UTF_8)).encryptBase64(data, StandardCharsets.UTF_8);
  116. }
  117. /**
  118. * sm4加密
  119. *
  120. * @param data 待加密数据
  121. * @param password 秘钥字符串
  122. * @return 加密后字符串, 采用Base64编码
  123. */
  124. public static String encryptBySm4Hex(String data, String password) {
  125. if (StrUtil.isBlank(password)) {
  126. throw new IllegalArgumentException("SM4需要传入秘钥信息");
  127. }
  128. // sm4算法的秘钥要求是16位长度
  129. int sm4PasswordLength = 16;
  130. if (sm4PasswordLength != password.length()) {
  131. throw new IllegalArgumentException("SM4秘钥长度要求为16位");
  132. }
  133. return SmUtil.sm4(password.getBytes(StandardCharsets.UTF_8)).encryptHex(data, StandardCharsets.UTF_8);
  134. }
  135. /**
  136. * sm4解密
  137. *
  138. * @param data 待解密数据
  139. * @param password 秘钥字符串
  140. * @return 解密后字符串
  141. */
  142. public static String decryptBySm4(String data, String password) {
  143. if (StrUtil.isBlank(password)) {
  144. throw new IllegalArgumentException("SM4需要传入秘钥信息");
  145. }
  146. // sm4算法的秘钥要求是16位长度
  147. int sm4PasswordLength = 16;
  148. if (sm4PasswordLength != password.length()) {
  149. throw new IllegalArgumentException("SM4秘钥长度要求为16位");
  150. }
  151. return SmUtil.sm4(password.getBytes(StandardCharsets.UTF_8)).decryptStr(data, StandardCharsets.UTF_8);
  152. }
  153. /**
  154. * 产生sm2加解密需要的公钥和私钥
  155. *
  156. * @return 公私钥Map
  157. */
  158. public static Map<String, String> generateSm2Key() {
  159. Map<String, String> keyMap = new HashMap<>(2);
  160. SM2 sm2 = SmUtil.sm2();
  161. keyMap.put(PRIVATE_KEY, sm2.getPrivateKeyBase64());
  162. keyMap.put(PUBLIC_KEY, sm2.getPublicKeyBase64());
  163. return keyMap;
  164. }
  165. /**
  166. * sm2公钥加密
  167. *
  168. * @param data 待加密数据
  169. * @param publicKey 公钥
  170. * @return 加密后字符串, 采用Base64编码
  171. */
  172. public static String encryptBySm2(String data, String publicKey) {
  173. if (StrUtil.isBlank(publicKey)) {
  174. throw new IllegalArgumentException("SM2需要传入公钥进行加密");
  175. }
  176. SM2 sm2 = SmUtil.sm2(null, publicKey);
  177. return sm2.encryptBase64(data, StandardCharsets.UTF_8, KeyType.PublicKey);
  178. }
  179. /**
  180. * sm2公钥加密
  181. *
  182. * @param data 待加密数据
  183. * @param publicKey 公钥
  184. * @return 加密后字符串, 采用Hex编码
  185. */
  186. public static String encryptBySm2Hex(String data, String publicKey) {
  187. if (StrUtil.isBlank(publicKey)) {
  188. throw new IllegalArgumentException("SM2需要传入公钥进行加密");
  189. }
  190. SM2 sm2 = SmUtil.sm2(null, publicKey);
  191. return sm2.encryptHex(data, StandardCharsets.UTF_8, KeyType.PublicKey);
  192. }
  193. /**
  194. * sm2私钥解密
  195. *
  196. * @param data 待加密数据
  197. * @param privateKey 私钥
  198. * @return 解密后字符串
  199. */
  200. public static String decryptBySm2(String data, String privateKey) {
  201. if (StrUtil.isBlank(privateKey)) {
  202. throw new IllegalArgumentException("SM2需要传入私钥进行解密");
  203. }
  204. SM2 sm2 = SmUtil.sm2(privateKey, null);
  205. return sm2.decryptStr(data, KeyType.PrivateKey, StandardCharsets.UTF_8);
  206. }
  207. /**
  208. * 产生RSA加解密需要的公钥和私钥
  209. *
  210. * @return 公私钥Map
  211. */
  212. public static Map<String, String> generateRsaKey() {
  213. Map<String, String> keyMap = new HashMap<>(2);
  214. RSA rsa = SecureUtil.rsa();
  215. keyMap.put(PRIVATE_KEY, rsa.getPrivateKeyBase64());
  216. keyMap.put(PUBLIC_KEY, rsa.getPublicKeyBase64());
  217. return keyMap;
  218. }
  219. /**
  220. * rsa公钥加密
  221. *
  222. * @param data 待加密数据
  223. * @param publicKey 公钥
  224. * @return 加密后字符串, 采用Base64编码
  225. */
  226. public static String encryptByRsa(String data, String publicKey) {
  227. if (StrUtil.isBlank(publicKey)) {
  228. throw new IllegalArgumentException("RSA需要传入公钥进行加密");
  229. }
  230. RSA rsa = SecureUtil.rsa(null, publicKey);
  231. return rsa.encryptBase64(data, StandardCharsets.UTF_8, KeyType.PublicKey);
  232. }
  233. /**
  234. * rsa公钥加密
  235. *
  236. * @param data 待加密数据
  237. * @param publicKey 公钥
  238. * @return 加密后字符串, 采用Hex编码
  239. */
  240. public static String encryptByRsaHex(String data, String publicKey) {
  241. if (StrUtil.isBlank(publicKey)) {
  242. throw new IllegalArgumentException("RSA需要传入公钥进行加密");
  243. }
  244. RSA rsa = SecureUtil.rsa(null, publicKey);
  245. return rsa.encryptHex(data, StandardCharsets.UTF_8, KeyType.PublicKey);
  246. }
  247. /**
  248. * rsa私钥解密
  249. *
  250. * @param data 待加密数据
  251. * @param privateKey 私钥
  252. * @return 解密后字符串
  253. */
  254. public static String decryptByRsa(String data, String privateKey) {
  255. if (StrUtil.isBlank(privateKey)) {
  256. throw new IllegalArgumentException("RSA需要传入私钥进行解密");
  257. }
  258. RSA rsa = SecureUtil.rsa(privateKey, null);
  259. return rsa.decryptStr(data, KeyType.PrivateKey, StandardCharsets.UTF_8);
  260. }
  261. /**
  262. * md5加密
  263. *
  264. * @param data 待加密数据
  265. * @return 加密后字符串, 采用Hex编码
  266. */
  267. public static String encryptByMd5(String data) {
  268. return SecureUtil.md5(data);
  269. }
  270. /**
  271. * sha256加密
  272. *
  273. * @param data 待加密数据
  274. * @return 加密后字符串, 采用Hex编码
  275. */
  276. public static String encryptBySha256(String data) {
  277. return SecureUtil.sha256(data);
  278. }
  279. /**
  280. * sm3加密
  281. *
  282. * @param data 待加密数据
  283. * @return 加密后字符串, 采用Hex编码
  284. */
  285. public static String encryptBySm3(String data) {
  286. return SmUtil.sm3(data);
  287. }
  288. }