AES 128位加解密
一.AES 简介
高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一.
二. EncryptionUtil 工具类
package cn.gdmcmc.iovs.hamcmd5.aes;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* @Description:AES128 加密
* @Author:lighter
* @Date:2019/9/9 10:51
* @Version 1.0
*/
public class EncryptionUtil {
// 加密
public static String Encrypt(String sSrc, String sKey,String cKey) throws Exception {
if (sKey == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
//获得utf-8编码数组
byte[] raw = sKey.getBytes("utf-8")
//获取AES密钥
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
//获取加密对象
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"
//获取AES向量
IvParameterSpec iv = new IvParameterSpec(cKey.getBytes());
//加密对象初始化
cipher.init(Cipher.ENCRYPT_MODE, skeySpec,iv);
//加密
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
//转码
String str =new BASE64Encoder().encode(encrypted);
//替换,之前没加,加密后的数据老是错误
str=str.replaceAll("\r\n", "");
str=str.replaceAll("\n", "");
return str;
}
// 解密
public static String Decrypt(String sSrc, String sKey,String cKey) throws Exception {
try {
// 判断Key是否正确
if (sKey == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
//获得utf-8编码数组
byte[] raw = sKey.getBytes("utf-8");
//获取AES 密钥
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
//获取AES 加密对象
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//获取AES 向量
IvParameterSpec iv = new IvParameterSpec(cKey.getBytes());
//加密对象初始化
cipher.init(Cipher.DECRYPT_MODE, skeySpec,iv);
//转码
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);
try {
//解密
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original,"utf-8");
return originalString;
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
}
三.AesMain
package cn.gdmcmc.iovs.hamcmd5.aes;
import com.alibaba.fastjson.JSON;
import java.util.HashMap;
import java.util.Map;
/**
* @Description:
* @Author:lighter
* @Date:2019/9/9 10:56
* @Version 1.0
*/
public class AesMain {
public static void main(String[] args) throws Exception {
/*
* 此处使用AES-128-ECB加密模式,key需要为16位。
*/
//AES 密钥 16位
String sKey = "****************";
//AES 向量
String cKey = "****************";
//被加密对象
Map<String,String> map = new HashMap<>();
map.put("OperatorSecret","****************");
map.put("OperatorID","*********");
// 需要加密的字串
String cSrc = JSON.toJSONString(map);
System.out.println(cSrc);
// 加密
String enString = EncryptionUtil.Encrypt(cSrc, sKey,cKey);
System.out.println("加密后的字串是:" + enString);
// 解密
String DeString = EncryptionUtil.Decrypt(enString, sKey,cKey);
System.out.println("解密后的字串是:" + DeString);
}
}