AES加密解密
转自:https://blog.csdn.net/qq_28205153/article/details/55798628
转自: https://www.zhihu.com/question/20874499
转自:https://blog.csdn.net/l18848956739/article/details/83184243
package com.zhhs.app.utils; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; public class AesUtil { /** * 密钥 (需要前端和后端保持一致)十六位作为密钥 */ private static final String KEY = "SwScqcJqO1NIKPwB"; /** * 密钥偏移量 (需要前端和后端保持一致)十六位作为密钥偏移量 */ private static final String IV = "0FVo7C4JQrU8wuR4"; /** * base 64 decode * * @param base64Code 待解码的base 64 code * @return 解码后的byte[] */ public static byte[] base64Decode(String base64Code) { return StringUtils.isEmpty(base64Code) ? null : Base64.decodeBase64(base64Code.getBytes()); } /** * AES解密 * * @param encryptBytes 待解密的byte[] * @return 解密后的String * @throws Exception */ public static String aesDecryptByBytes(byte[] encryptBytes) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] temp = IV.getBytes(StandardCharsets.US_ASCII); IvParameterSpec iv = new IvParameterSpec(temp); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY.getBytes(StandardCharsets.US_ASCII), "AES"), iv); byte[] decryptBytes = cipher.doFinal(encryptBytes); return new String(decryptBytes); } /** * 将base 64 code AES解密 * * @param encryptStr 待解密的base 64 code * @return 解密后的string * @throws Exception */ public static String aesDecrypt(String encryptStr) throws Exception { return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr)); } //加密 @SuppressWarnings("unused") public static String aesEncrypt(String str) { try { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec iv = new IvParameterSpec(IV.getBytes(StandardCharsets.US_ASCII)); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY.getBytes(StandardCharsets.US_ASCII), "AES"), iv); byte[] encryptBytes = cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)); return new String(Base64.encodeBase64(encryptBytes), StandardCharsets.US_ASCII); } catch (Exception e) { e.printStackTrace(); return ""; } } /** * 生成一个可选长度的随机数列 * * @param */ public static String getRandomCode(int length) { String[] arr = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}; String str = ""; for (int i = 0; i < length; i++) { int r = (int) (Math.random() * 61); str += arr[r]; } return str; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类