代码拉取完成,页面将自动刷新
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);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。