您的位置 首页 java

JAVA 实现AES加密解密

1 使用CBC+PKCS5Padding+base64+utf-8 做加密解密,代码如下

 /**
 *加密操作
 * @param content  待加密内容
 * @param encodingFormat 编码方式
 * @param sKey 密码
 * @param ivParameter 偏移量
 * @return
 * @throws  Exception 
 */public String encrypt(String content, String encodingFormat, String sKey, String ivParameter) {
try {
Cipher cipher = Cipher. getInstance ("AES/CBC/PKCS5Padding");
 byte [] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(content.get Bytes (encodingFormat));
return Base64Utils.encodeToString(encrypted);
}catch (Exception e){
log.error("data {} encrypt on error {}",content,e);
}
return null;
}

/**
 *解密操作
 * @param content 待解密内容
 * @param encodingFormat 编码方式
 * @param sKey 密码
 * @param ivParameter 偏移量
 * @return
 * @throws Exception
 */public String decrypt(String content, String encodingFormat, String sKey, String ivParameter) {
try {
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

byte[] encrypted1 = Base64Utils. decode FromString(content);
byte[] original = cipher.doFinal(encrypted1);
 String  originalString = new String(original, encodingFormat);
return originalString;
} catch (Exception ex) {
log.error("data {} decrypt on error {}",content,e);
}
return null;
}  

2 使用CBC+no padding+hex+utf-8 做加密解密,代码如下

注意:此方式加密或者解密内容字节长度必须为16的整数倍,否则会报错

 /**
 *加密操作
 * @param content  待加密内容
 * @param encodingFormat 编码方式
 * @param sKey 密码
 * @param ivParameter 偏移量
 * @return
 * @throws Exception
 */public String encrypt(String content, String encodingFormat, String sKey, String ivParameter) {
try {
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/nopadding");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
while (content.getBytes(encodingFormat).length % 16 != 0){
content+=" ";
}
byte[] encrypted = cipher.doFinal(content.getBytes(encodingFormat));
return Hex.encodeHexString(encrypted).toUpperCase(Locale.ROOT);
}catch (Exception e){
log.error("data {} decrtpy on error {}",content,e);
}
return null;
}

/**
 *解密操作
 * @param content 待解密内容
 * @param encodingFormat 编码方式
 * @param sKey 密码
 * @param ivParameter 偏移量
 * @return
 * @throws Exception
 */public String decrypt(String content, String encodingFormat, String sKey, String ivParameter) {
try {
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/nopadding");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] bytes = Hex.decodeHex(content);
byte[] original = cipher.doFinal(bytes);
String originalString = new String(original, encodingFormat);
return originalString;
} catch (Exception ex) {
log.error("data {} decrtpy on error {}",content,ex);
}
return null;
}  

文章来源:智云一二三科技

文章标题:JAVA 实现AES加密解密

文章地址:https://www.zhihuclub.com/184754.shtml

关于作者: 智云科技

热门文章

发表回复

您的电子邮箱地址不会被公开。

网站地图