package algo;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class AES {
public static void main(String[] args) {
String content = "我热爱生活";
String pwd = "123456";
System.out.println("内容:" + content + "----------" + "密钥:" + pwd);
// AES加密
byte[] encryptrRes = encrypt(content, pwd);
System.out.println("AES加密:" + encryptrRes);
// AES加密的结果转换成String字符串
String encryptResStr = parseByteToString(encryptrRes);
// Base64加密
String encodeBase64 = Base64.encryptBase64(encryptResStr);
System.out.println("Base64加密:" + encodeBase64);
// Base64解密
String decodeBase64 = Base64.decryptBase64(encodeBase64);
System.out.println("Base64解密:" + decodeBase64);
// Base64解密的结果转换成byte类型
byte[] decryptRes = parseStringToByte(decodeBase64);
// AES解密
String decryptResStr = decrypt(decryptRes, pwd);
System.out.println("AES解密:" + decryptResStr);
// AES2加密
String connect = "外敌犯我中华者虽远必诛杀之勿忘之";
String pass = "abcdefgh12345678";
byte[] encrypt2 = encrypt2(connect, pass);
System.out.println("AES 参数16位加密:" + encrypt2);
// AES2解密
String decrypt2 = decrypt2(encrypt2, pass);
System.out.println("AES 参数16位解密:" + decrypt2);
}
private static final String ALGORITHM_TYPE = "AES";
private static final String FORMAT = "utf-8";
private static final String ALGORITHM_TYPE_2 = "AES/ECB/NoPadding";
//加密
public static byte[] encrypt(String content, String password) {
try {
if (null == content || null == password) {
return null;
}
// 返回生成指定算法的秘密密钥的 KeyGenerator 对象
KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM_TYPE);
// 初始化此密钥生成器,使其具有确定的密钥大小
// AES 要求密钥长度为 128
kgen.init(128, new SecureRandom(password.getBytes()));
// 生成一个密钥
SecretKey secretKey = kgen.generateKey();
// 返回基本编码格式的密钥,如果此密钥不支持编码,则返回 null
byte[] enCodeFormat = secretKey.getEncoded();
// 根据给定的字节数组构造一个密钥
SecretKeySpec key = new SecretKeySpec(enCodeFormat, ALGORITHM_TYPE);
// 实例化密码器
Cipher cipher = Cipher.getInstance(ALGORITHM_TYPE);
// 編碼格式
byte[] byteContent = content.getBytes(FORMAT);
// 使用密钥初始化,设置为加密模式
cipher.init(Cipher.ENCRYPT_MODE, key);
// 加密結果
byte[] result = cipher.doFinal(byteContent);
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
return null;
}
// 解密
public static String decrypt(byte[] content, String password) {
try {
if (content.length < 0 || null == password) {
return null;
}
// 返回生成指定算法的秘密密钥的 KeyGenerator 对象
KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM_TYPE);
// 初始化此密钥生成器,使其具有确定的密钥大小
// AES 要求密钥长度为 128
kgen.init(128, new SecureRandom(password.getBytes()));
// 生成一个密钥
SecretKey secretKey = kgen.generateKey();
// 返回基本编码格式的密钥,如果此密钥不支持编码,则返回 null
byte[] enCodeFormat = secretKey.getEncoded();
// 根据给定的字节数组构造一个密钥
SecretKeySpec key = new SecretKeySpec(enCodeFormat, ALGORITHM_TYPE);
// 实例化密码器
Cipher cipher = Cipher.getInstance(ALGORITHM_TYPE);
// 使用密钥初始化,设置为解密模式
cipher.init(Cipher.DECRYPT_MODE, key);
// 解密结果
byte[] result = cipher.doFinal(content);
return new String(result);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
/**
* Byte类型转String
*
* @param buf
* @return
*/
public static String parseByteToString(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();
}
/**
* String类型转Byte类型
*
* @param hexStr
* @return
*/
public static byte[] parseStringToByte(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;
}
/**
* 加密
*
* @param content
* 需要加密的内容
* @param password
* 加密密码
* @return
*/
public static byte[] encrypt2(String content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM_TYPE);
kgen.init(new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
// 返回基本编码格式的密钥,如果此密钥不支持编码,则返回 null
byte[] enCodeFormat = secretKey.getEncoded();
// 根据给定的字节数组构造一个密钥
SecretKeySpec key = new SecretKeySpec(enCodeFormat, ALGORITHM_TYPE);
Cipher cipher = Cipher.getInstance(ALGORITHM_TYPE_2);
byte[] byteContent = content.getBytes(FORMAT);
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
// 解密
public static String decrypt2(byte[] content, String password) {
try {
if (content.length < 0 || null == password) {
return null;
}
// 返回生成指定算法的秘密密钥的 KeyGenerator 对象
KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM_TYPE);
// 初始化此密钥生成器,使其具有确定的密钥大小
// AES 要求密钥长度为 128
kgen.init(128, new SecureRandom(password.getBytes()));
// 生成一个密钥
SecretKey secretKey = kgen.generateKey();
// 返回基本编码格式的密钥,如果此密钥不支持编码,则返回 null
byte[] enCodeFormat = secretKey.getEncoded();
// 根据给定的字节数组构造一个密钥
SecretKeySpec key = new SecretKeySpec(enCodeFormat, ALGORITHM_TYPE);
// 实例化密码器
Cipher cipher = Cipher.getInstance(ALGORITHM_TYPE_2);
// 使用密钥初始化,设置为解密模式
cipher.init(Cipher.DECRYPT_MODE, key);
// 解密结果
byte[] result = cipher.doFinal(content);
return new String(result);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
}
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class AES {
public static void main(String[] args) {
String content = "我热爱生活";
String pwd = "123456";
System.out.println("内容:" + content + "----------" + "密钥:" + pwd);
// AES加密
byte[] encryptrRes = encrypt(content, pwd);
System.out.println("AES加密:" + encryptrRes);
// AES加密的结果转换成String字符串
String encryptResStr = parseByteToString(encryptrRes);
// Base64加密
String encodeBase64 = Base64.encryptBase64(encryptResStr);
System.out.println("Base64加密:" + encodeBase64);
// Base64解密
String decodeBase64 = Base64.decryptBase64(encodeBase64);
System.out.println("Base64解密:" + decodeBase64);
// Base64解密的结果转换成byte类型
byte[] decryptRes = parseStringToByte(decodeBase64);
// AES解密
String decryptResStr = decrypt(decryptRes, pwd);
System.out.println("AES解密:" + decryptResStr);
// AES2加密
String connect = "外敌犯我中华者虽远必诛杀之勿忘之";
String pass = "abcdefgh12345678";
byte[] encrypt2 = encrypt2(connect, pass);
System.out.println("AES 参数16位加密:" + encrypt2);
// AES2解密
String decrypt2 = decrypt2(encrypt2, pass);
System.out.println("AES 参数16位解密:" + decrypt2);
}
private static final String ALGORITHM_TYPE = "AES";
private static final String FORMAT = "utf-8";
private static final String ALGORITHM_TYPE_2 = "AES/ECB/NoPadding";
//加密
public static byte[] encrypt(String content, String password) {
try {
if (null == content || null == password) {
return null;
}
// 返回生成指定算法的秘密密钥的 KeyGenerator 对象
KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM_TYPE);
// 初始化此密钥生成器,使其具有确定的密钥大小
// AES 要求密钥长度为 128
kgen.init(128, new SecureRandom(password.getBytes()));
// 生成一个密钥
SecretKey secretKey = kgen.generateKey();
// 返回基本编码格式的密钥,如果此密钥不支持编码,则返回 null
byte[] enCodeFormat = secretKey.getEncoded();
// 根据给定的字节数组构造一个密钥
SecretKeySpec key = new SecretKeySpec(enCodeFormat, ALGORITHM_TYPE);
// 实例化密码器
Cipher cipher = Cipher.getInstance(ALGORITHM_TYPE);
// 編碼格式
byte[] byteContent = content.getBytes(FORMAT);
// 使用密钥初始化,设置为加密模式
cipher.init(Cipher.ENCRYPT_MODE, key);
// 加密結果
byte[] result = cipher.doFinal(byteContent);
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
return null;
}
// 解密
public static String decrypt(byte[] content, String password) {
try {
if (content.length < 0 || null == password) {
return null;
}
// 返回生成指定算法的秘密密钥的 KeyGenerator 对象
KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM_TYPE);
// 初始化此密钥生成器,使其具有确定的密钥大小
// AES 要求密钥长度为 128
kgen.init(128, new SecureRandom(password.getBytes()));
// 生成一个密钥
SecretKey secretKey = kgen.generateKey();
// 返回基本编码格式的密钥,如果此密钥不支持编码,则返回 null
byte[] enCodeFormat = secretKey.getEncoded();
// 根据给定的字节数组构造一个密钥
SecretKeySpec key = new SecretKeySpec(enCodeFormat, ALGORITHM_TYPE);
// 实例化密码器
Cipher cipher = Cipher.getInstance(ALGORITHM_TYPE);
// 使用密钥初始化,设置为解密模式
cipher.init(Cipher.DECRYPT_MODE, key);
// 解密结果
byte[] result = cipher.doFinal(content);
return new String(result);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
/**
* Byte类型转String
*
* @param buf
* @return
*/
public static String parseByteToString(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();
}
/**
* String类型转Byte类型
*
* @param hexStr
* @return
*/
public static byte[] parseStringToByte(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;
}
/**
* 加密
*
* @param content
* 需要加密的内容
* @param password
* 加密密码
* @return
*/
public static byte[] encrypt2(String content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM_TYPE);
kgen.init(new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
// 返回基本编码格式的密钥,如果此密钥不支持编码,则返回 null
byte[] enCodeFormat = secretKey.getEncoded();
// 根据给定的字节数组构造一个密钥
SecretKeySpec key = new SecretKeySpec(enCodeFormat, ALGORITHM_TYPE);
Cipher cipher = Cipher.getInstance(ALGORITHM_TYPE_2);
byte[] byteContent = content.getBytes(FORMAT);
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
// 解密
public static String decrypt2(byte[] content, String password) {
try {
if (content.length < 0 || null == password) {
return null;
}
// 返回生成指定算法的秘密密钥的 KeyGenerator 对象
KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM_TYPE);
// 初始化此密钥生成器,使其具有确定的密钥大小
// AES 要求密钥长度为 128
kgen.init(128, new SecureRandom(password.getBytes()));
// 生成一个密钥
SecretKey secretKey = kgen.generateKey();
// 返回基本编码格式的密钥,如果此密钥不支持编码,则返回 null
byte[] enCodeFormat = secretKey.getEncoded();
// 根据给定的字节数组构造一个密钥
SecretKeySpec key = new SecretKeySpec(enCodeFormat, ALGORITHM_TYPE);
// 实例化密码器
Cipher cipher = Cipher.getInstance(ALGORITHM_TYPE_2);
// 使用密钥初始化,设置为解密模式
cipher.init(Cipher.DECRYPT_MODE, key);
// 解密结果
byte[] result = cipher.doFinal(content);
return new String(result);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
}