2 Star 3 Fork 1

Ben / commons

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
AES.java 3.34 KB
一键复制 编辑 原始数据 按行查看 历史
Ben 提交于 2023-06-15 13:51 . 规范代码
package com.stark.commons.lang.security;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* 对称算法 AES 加解密。
* @author Ben
* @since 1.0.0
* @version 1.0.0
*/
public class AES {
private static final String DEFAULT_CHARSET = "UTF-8";
private static final String IV_PARAMETER = "0102030405060708";
/**
* AES 加密。
* @param input 待加密字符串。
* @param charsetName 字符串编码,如果 input 包含中文,必须为 UTF-8 或 GBK 。
* @param key 由字母和数字组成的长度为 16 位的秘钥。
* @return 加密后的字符串。
*/
public static String encrypt(String input, String charsetName, String key) throws Exception {
if (key == null) {
throw new IllegalArgumentException("秘钥不能为空");
}
if (key.length() != 16) {
throw new IllegalArgumentException("秘钥长度不是16位");
}
byte[] raw = key.getBytes(charsetName);
SecretKeySpec sks = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); // "算法/模式/补码方式"
IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(charsetName)); // 使用CBC模式,需要一个向量 iv ,可增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, sks, iv);
byte[] encrypted = cipher.doFinal(input.getBytes(charsetName));
return Base64.encodeBase64String(encrypted); // 此处使用 base64 做转码功能,同时能起到 2 次加密的作用
}
/**
* AES 加密。
* @param input 待加密字符串,默认 UTF-8 编码。
* @param key 由字母和数字组成的长度为 16 位的秘钥。
* @return 加密后的字符串。
*/
public static String encrypt(String input, String key) throws Exception {
return encrypt(input, DEFAULT_CHARSET, key);
}
/**
* AES 解密。
* @param input AES 加密的字符串。
* @param charsetName 字符串编码,如果 input 包含中文,必须为 UTF-8 或 GBK 。
* @param key 由字母和数字组成的长度为 16 位的秘钥。
* @return 解密后的字符串。
*/
public static String decrypt(String input, String charsetName, String key) throws Exception {
if (key == null) {
throw new IllegalArgumentException("秘钥不能为空");
}
if (key.length() != 16) {
throw new IllegalArgumentException("秘钥长度不是16位");
}
byte[] raw = key.getBytes(charsetName);
SecretKeySpec sks = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(charsetName));
cipher.init(Cipher.DECRYPT_MODE, sks, iv);
byte[] encrypted = Base64.decodeBase64(input);
byte[] original = cipher.doFinal(encrypted);
return new String(original, charsetName);
}
/**
* AES 解密。
* @param input AES 加密的字符串,默认 UTF-8 编码。
* @param key 由字母和数字组成的长度为 16 位的秘钥。
* @return 解密后的字符串。
*/
public static String decrypt(String input, String key) throws Exception {
return decrypt(input, DEFAULT_CHARSET, key);
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/jarvis-lib/commons.git
git@gitee.com:jarvis-lib/commons.git
jarvis-lib
commons
commons
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891