| public class AESUtil { |
| |
| public static final String algorithm = "AES"; |
| |
| |
| |
| |
| |
| |
| public static final String transformation = "AES/CBC/PKCS5Padding"; |
| public static final String key = "1234567812345678"; |
| |
| |
| |
| |
| |
| |
| |
| public static String encryptByAES(String original) throws Exception { |
| |
| Cipher cipher = Cipher.getInstance(transformation); |
| |
| SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), algorithm); |
| |
| |
| IvParameterSpec iv = new IvParameterSpec(key.getBytes()); |
| cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv); |
| |
| |
| byte[] bytes = cipher.doFinal(original.getBytes()); |
| |
| return encryptBASE64(bytes); |
| } |
| |
| |
| |
| |
| |
| |
| |
| public static String decryptByAES(String encrypted) throws Exception { |
| |
| Cipher cipher = Cipher.getInstance(transformation); |
| |
| SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), algorithm); |
| |
| |
| IvParameterSpec iv = new IvParameterSpec(key.getBytes()); |
| cipher.init(Cipher.DECRYPT_MODE, keySpec, iv); |
| |
| |
| |
| byte[] bytes = cipher.doFinal(decryBASE64(encrypted)); |
| |
| return new String(bytes); |
| } |
| |
| |
| |
| |
| |
| |
| |
| public static byte[] decryBASE64(String key) throws Exception{ |
| return (new BASE64Decoder()).decodeBuffer(key); |
| } |
| |
| |
| |
| |
| |
| |
| |
| public static String encryptBASE64(byte[] key) throws Exception{ |
| return (new BASE64Encoder()).encode(key); |
| } |
| |
| |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)