AES加密的四种模式详解
ECB模式Java实现(1)
ECB模式Java实现(2)
CBC模式Java实现(1)
CBC模式Java实现(2)
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;
public class Base64AesEncrypt {
/**
* BASE64加密
* @param base64Content 被加密的字符串
* @return
*/
public static String encryptBASE64(String base64Content) {
byte[] bt = base64Content.getBytes();
String aesContent = (new BASE64Encoder()).encodeBuffer(bt);
return aesContent;
}
/**
* AES加密
* @param aesContent 被加密的字符串
* @param aesKey 秘钥
* @return
*/
public static String encryptAES(String aesContent, String aesKey) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");// 创建AES的Key生产者
kgen.init(128, new SecureRandom(aesKey.getBytes()));// 利用用户密码作为随机数初始化出128位的key生产者
//加密没关系,SecureRandom是生成安全随机数序列,password.getBytes()是种子,只要种子相同,序列就一样,所以解密只要有password就行
SecretKey secretKey = kgen.generateKey(); //根据用户密码,生成一个密钥
byte[] enCodeFormat = secretKey.getEncoded();// 返回基本编码格式的密钥,如果此密钥不支持编码,则返回null
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 转换为AES专用密钥
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
byte[] byteContent = aesContent.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化为加密模式的密码器
byte[] result = cipher.doFinal(byteContent);// 加密
String result_hexStrResult = Base64AesEncrypt.parseByte2HexStr(result);
return result_hexStrResult;
// return result;
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**将二进制转换成16进制
* @param buf
* @return
*/
public static String parseByte2HexStr(byte[] buf) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**将16进制转换为二进制
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length()/2];
for (int i = 0;i< hexStr.length()/2; i++) {
int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
}
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
public class Base64AesDecipher {
/**
* base64解密
* @param s
* @return
*/
public static String DecipherBase64(String s){
byte[] b = null;
String result = null;
if (s != null) {
BASE64Decoder decoder = new BASE64Decoder();
try {
b = decoder.decodeBuffer(s);
result = new String(b, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 解密AES加密过的字符串
* @param content AES加密过过的内容
* @param aesKey 秘钥
* @return 明文
*/
public static byte[] decryptAES(String conts, String aesKey) {
byte[] content = Base64AesEncrypt.parseHexStr2Byte(conts);
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES"); // 创建AES的Key生产者
kgen.init(128, new SecureRandom(aesKey.getBytes()));
SecretKey secretKey = kgen.generateKey(); // 根据用户密码,生成一个密钥
byte[] enCodeFormat = secretKey.getEncoded();// 返回基本编码格式的密钥
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 转换为AES专用密钥
Cipher cipher = Cipher.getInstance("AES"); // 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key); // 初始化为解密模式的密码器
byte[] result = cipher.doFinal(content);
return result;
// return new String(result); // 明文
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
public class Test {
public static void main(String[] args) {
String content = "welcome";
String aesKey = "aeskey";
//base64加密
String en_base64 = Base64AesEncrypt.encryptBASE64(content);
System.out.println(en_base64);
//AES加密
String en_aes = Base64AesEncrypt.encryptAES(en_base64,aesKey);
System.out.println(en_aes);
//AES解密
byte[] de_aes = Base64AesDecipher.decryptAES(en_aes,aesKey);
System.out.println(new String(de_aes));
//base64解密
String de_base64 = Base64AesDecipher.DecipherBase64(new String(de_aes));
System.out.println(de_base64);
}
}
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;
public class Base64AesEncrypt {
/**
* BASE64加密
* @param base64Content 被加密的字符串
* @return
*/
public static String encryptBASE64(String base64Content) {
byte[] bt = base64Content.getBytes();
String aesContent = (new BASE64Encoder()).encodeBuffer(bt);
return aesContent;
}
/**
* AES加密
* @param aesContent 被Base64加密过的字符串
* @param key 秘钥
* @param ivParameter 偏移量
* @return
*/
public static String encryptAES(String aesContent,String key, String ivParameter) {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = key.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());//使用CBC模式,需要一个向量iv,可增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(aesContent.getBytes("utf-8"));
return new BASE64Encoder().encode(encrypted);//此处使用BASE64做转码。
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
public class Base64AesDecipher {
/**
* base64解密
* @param content 被AES解密过的字符串
* @return
*/
public static String DecipherBase64(String content){
byte[] b = null;
String result = null;
if (content != null) {
BASE64Decoder decoder = new BASE64Decoder();
try {
b = decoder.decodeBuffer(content);
result = new String(b, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* @param content 被Base64+AES加密过的字符串
* @param aesKey 秘钥
* @param ivParameter 偏移量
* @return
*/
public static String decryptAES(String content, String aesKey,String ivParameter) {
try {
byte[] raw = aesKey.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 = new BASE64Decoder().decodeBuffer(content);//先用base64解密
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original,"utf-8");
return originalString;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}
public class Test {
public static void main(String[] args) {
String content = "welcome";
String aesKey = "aeskeyaeskeyaesk";
String iv = "a9a109f44adeac5d";
//base64加密
String en_base64 = Base64AesEncrypt.encryptBASE64(content);
System.out.println(en_base64);
//AES加密
String en_aes = Base64AesEncrypt.encryptAES(en_base64,aesKey,iv);
System.out.println(en_aes);
//AES解密
String de_aes = Base64AesDecipher.decryptAES(en_aes,aesKey,iv);
System.out.println(de_aes);
//base64解密
String de_base64 = Base64AesDecipher.DecipherBase64(de_aes);
System.out.println(de_base64);
}
}