AesUtil.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package com.ruoyi.common.utils;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.security.crypto.password.PasswordEncoder;
  6. import org.springframework.util.Base64Utils;
  7. import javax.crypto.Cipher;
  8. import javax.crypto.spec.IvParameterSpec;
  9. import javax.crypto.spec.SecretKeySpec;
  10. import static com.ruoyi.common.constant.CommonConstants.LOGIN_PASSWORD_AES;
  11. /**
  12. * @Author: tjf AES加密工具类
  13. * @Date: 2024/4/16 10:09
  14. * @Describe:
  15. */
  16. public class AesUtil {
  17. /**
  18. * 日志相关
  19. */
  20. private static final Logger LOGGER = LoggerFactory.getLogger(AesUtil.class);
  21. /**
  22. * 编码
  23. */
  24. private static final String ENCODING = "UTF-8";
  25. /**
  26. * 算法定义
  27. */
  28. private static final String AES_ALGORITHM = "AES";
  29. /**
  30. * 指定填充方式
  31. */
  32. private static final String CIPHER_PADDING = "AES/ECB/PKCS5Padding";
  33. private static final String CIPHER_CBC_PADDING = "AES/CBC/PKCS5Padding";
  34. /**
  35. * 偏移量(CBC中使用,增强加密算法强度)
  36. */
  37. private static final String IV_SEED = "1234567812345678";
  38. /**
  39. * AES加密
  40. *
  41. * @param content 待加密内容
  42. * @param aesKey 密码
  43. * @return
  44. */
  45. public static String encrypt(String content, String aesKey) {
  46. if (StringUtils.isBlank(content)) {
  47. LOGGER.info("AES encrypt: the content is null!");
  48. return null;
  49. }
  50. //判断秘钥是否为16位
  51. if (StringUtils.isNotBlank(aesKey) && aesKey.length() == 16) {
  52. try {
  53. //对密码进行编码
  54. byte[] bytes = aesKey.getBytes(ENCODING);
  55. //设置加密算法,生成秘钥
  56. SecretKeySpec skeySpec = new SecretKeySpec(bytes, AES_ALGORITHM);
  57. // "算法/模式/补码方式"
  58. Cipher cipher = Cipher.getInstance(CIPHER_PADDING);
  59. //选择加密
  60. cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  61. //根据待加密内容生成字节数组
  62. byte[] encrypted = cipher.doFinal(content.getBytes(ENCODING));
  63. //返回base64字符串
  64. return Base64Utils.encodeToString(encrypted);
  65. } catch (Exception e) {
  66. LOGGER.info("AES encrypt exception:" + e.getMessage());
  67. throw new RuntimeException(e);
  68. }
  69. } else {
  70. LOGGER.info("AES encrypt: the aesKey is null or error!");
  71. return null;
  72. }
  73. }
  74. /**
  75. * 解密
  76. *
  77. * @param content 待解密内容
  78. * @param aesKey 密码
  79. * @return
  80. */
  81. public static String decrypt(String content, String aesKey) {
  82. if (StringUtils.isBlank(content)) {
  83. LOGGER.info("AES decrypt: the content is null!");
  84. return null;
  85. }
  86. //判断秘钥是否为16位
  87. if (StringUtils.isNotBlank(aesKey) && aesKey.length() == 16) {
  88. try {
  89. //对密码进行编码
  90. byte[] bytes = aesKey.getBytes(ENCODING);
  91. //设置解密算法,生成秘钥
  92. SecretKeySpec skeySpec = new SecretKeySpec(bytes, AES_ALGORITHM);
  93. // "算法/模式/补码方式"
  94. Cipher cipher = Cipher.getInstance(CIPHER_PADDING);
  95. //选择解密
  96. cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  97. //先进行Base64解码
  98. byte[] decodeBase64 = Base64Utils.decodeFromString(content);
  99. //根据待解密内容进行解密
  100. byte[] decrypted = cipher.doFinal(decodeBase64);
  101. //将字节数组转成字符串
  102. return new String(decrypted, ENCODING);
  103. } catch (Exception e) {
  104. LOGGER.info("AES decrypt exception:" + e.getMessage());
  105. throw new RuntimeException(e);
  106. }
  107. } else {
  108. LOGGER.info("AES decrypt: the aesKey is null or error!");
  109. return null;
  110. }
  111. }
  112. /**
  113. * AES_CBC加密
  114. *
  115. * @param content 待加密内容
  116. * @param aesKey 密码
  117. * @return
  118. */
  119. public static String encryptCBC(String content, String aesKey) {
  120. if (StringUtils.isBlank(content)) {
  121. LOGGER.info("AES_CBC encrypt: the content is null!");
  122. return null;
  123. }
  124. //判断秘钥是否为16位
  125. if (StringUtils.isNotBlank(aesKey) && aesKey.length() == 16) {
  126. try {
  127. //对密码进行编码
  128. byte[] bytes = aesKey.getBytes(ENCODING);
  129. //设置加密算法,生成秘钥
  130. SecretKeySpec skeySpec = new SecretKeySpec(bytes, AES_ALGORITHM);
  131. // "算法/模式/补码方式"
  132. Cipher cipher = Cipher.getInstance(CIPHER_CBC_PADDING);
  133. //偏移
  134. IvParameterSpec iv = new IvParameterSpec(IV_SEED.getBytes(ENCODING));
  135. //选择加密
  136. cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
  137. //根据待加密内容生成字节数组
  138. byte[] encrypted = cipher.doFinal(content.getBytes(ENCODING));
  139. //返回base64字符串
  140. return Base64Utils.encodeToString(encrypted);
  141. } catch (Exception e) {
  142. LOGGER.info("AES_CBC encrypt exception:" + e.getMessage());
  143. throw new RuntimeException(e);
  144. }
  145. } else {
  146. LOGGER.info("AES_CBC encrypt: the aesKey is null or error!");
  147. return null;
  148. }
  149. }
  150. /**
  151. * AES_CBC解密
  152. *
  153. * @param content 待解密内容
  154. * @param aesKey 密码
  155. * @return
  156. */
  157. public static String decryptCBC(String content, String aesKey) {
  158. if (StringUtils.isBlank(content)) {
  159. LOGGER.info("AES_CBC decrypt: the content is null!");
  160. return null;
  161. }
  162. //判断秘钥是否为16位
  163. if (StringUtils.isNotBlank(aesKey) && aesKey.length() == 16) {
  164. try {
  165. //对密码进行编码
  166. byte[] bytes = aesKey.getBytes(ENCODING);
  167. //设置解密算法,生成秘钥
  168. SecretKeySpec skeySpec = new SecretKeySpec(bytes, AES_ALGORITHM);
  169. //偏移
  170. IvParameterSpec iv = new IvParameterSpec(IV_SEED.getBytes(ENCODING));
  171. // "算法/模式/补码方式"
  172. Cipher cipher = Cipher.getInstance(CIPHER_CBC_PADDING);
  173. //选择解密
  174. cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
  175. //先进行Base64解码
  176. byte[] decodeBase64 = Base64Utils.decodeFromString(content);
  177. //根据待解密内容进行解密
  178. byte[] decrypted = cipher.doFinal(decodeBase64);
  179. //将字节数组转成字符串
  180. return new String(decrypted, ENCODING);
  181. } catch (Exception e) {
  182. LOGGER.info("AES_CBC decrypt exception:" + e.getMessage());
  183. throw new RuntimeException(e);
  184. }
  185. } else {
  186. LOGGER.info("AES_CBC decrypt: the aesKey is null or error!");
  187. return null;
  188. }
  189. }
  190. }