package aa;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* AES加密demo,ECB/NoPadding
* @author 7490
* @version 20131205
*/
public class AESDemo {
private String algorithm="AES";
private String charset="UTF-8";
private int keyLength=128;
/*
* 加密算法/工作模式/填充方式
* 默认:AES/ECB/PKCS5Padding
*
*/
private String cipherAlgorithm="AES/ECB/NoPadding";
/**
* @param args
*/
public static void main(String[] args) {
try {
AESDemo demo=new AESDemo();
String content="AwFeGQAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAThSlePoks3jIMU95LkUHeRBfCnioa6x5vG0Z+QqIFnmOjh54YJGceVabIHjcqCz5vKkmeR65Kvl26jH4YuuP+CT3jvmWE199sDcPfIA+p/y8RKx9ZmcU/O7RNPz+sSrxrpZzdAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
//NoPadding时,补充加密串长度为16倍数
content=demo.gain16MultiLengthStr(content);
String secretKey="20131205104410";
System.out.println("加密前:"+content);
byte[] encryptB=demo.encryptByAES(content, secretKey);//获取密钥
String encryptStr=demo.getBASE64EncodeRes(encryptB);
System.out.println("加密后:"+encryptStr);
byte[] tmpB=demo.getBASE64DecodeRes(encryptStr);
byte[] decryptB=demo.decryptByAES(tmpB, secretKey);
String decryptStr=new String(decryptB,demo.charset);
System.out.println("解密后:"+decryptStr);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* AES加密
* @param content String,要加密的串
* @param secretKey String,加密密钥
* @return 加密后的字节数组
*/
public byte[] encryptByAES(String content,String secretKey){
try {
byte[] encodeKey=initSecretKey(secretKey);
SecretKeySpec keySpec=new SecretKeySpec(encodeKey, algorithm);//可以使用此类来根据一个字节数组构造一个 SecretKey
Cipher cipher=Cipher.getInstance(cipherAlgorithm);//Cipher完成加密
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
return cipher.doFinal(content.getBytes(charset));
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* AES解密
* @param content byte[],要解密的字节数组
* @param secretKey String,加密密钥
* @return 解密后的字节数组
*/
public byte[] decryptByAES(byte[] content,String secretKey){
try {
byte[] encodeKey=initSecretKey(secretKey);
SecretKeySpec keySpec=new SecretKeySpec(encodeKey, algorithm);//可以使用此类来根据一个字节数组构造一个 SecretKey
Cipher cipher=Cipher.getInstance(cipherAlgorithm);//Cipher完成解密
cipher.init(Cipher.DECRYPT_MODE, keySpec);
return cipher.doFinal(content);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 密钥处理,此方法需返回一个长度为16的字节数组(即:密钥)
* @param secretKey String,用于生成密钥的字符串
* @return 16字节的密钥
*/
private byte[] initSecretKey(String secretKey){
try {//该过程可替换,最终返回一个长度为16的字节数组即可
/*
KeyGenerator key=KeyGenerator.getInstance(algorithm);//KeyGenerator提供密钥生成器的功能
SecureRandom random=new SecureRandom(secretKey.getBytes(charset));//生成随机源
key.init(keyLength, random);//初始化密钥生成器,使密钥大小为128位
SecretKey secretkey=key.generateKey();//SecretKey负责保存对称密钥
byte[] encodeKey=secretkey.getEncoded();
System.out.println("密钥字节数组长度:"+encodeKey.length);
*/
String secretKey1="2013120510441000";
return secretKey1.getBytes(charset);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* base64转码
* @param source
*/
private String getBASE64EncodeRes(byte[] source){
try{
BASE64Encoder base64en=new BASE64Encoder();
String restr=base64en.encode(source);
restr=restr.replaceAll("\r", "");
restr=restr.replaceAll("\n", "");
return restr;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
/**
* base64解码
* @param source
*/
private byte[] getBASE64DecodeRes(String source){
try{
BASE64Decoder base64de=new BASE64Decoder();
byte[] resb=base64de.decodeBuffer(source);
base64de=null;
return resb;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
/**
* 将字符串长度补为16的倍数
* @param content
* @return
*/
private String gain16MultiLengthStr(String content){
try {
int length=content.length();
if(length%16==0){
return content;
}
int remainder=length%16;
for(int i=remainder;i<16;i++){
content=content+" ";
}
return content;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
java aes加密demo
最新推荐文章于 2023-06-07 15:13:18 发布