public class AES {
private static final String key ="1234(^&*#$)Wx678";
/**
* AES加密算法
*/
public AES() {
}
/**
* 加密
*
* @param content
* 需要加密的内容
* @param keyWord
* 加密密钥
* @return byte[] 加密后的字节数组
*/
public static String encrypt(String content) {
try {
if (key == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为16位
if (key.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
byte[] raw = key.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
// "算法/模式/补码方式"
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(content.getBytes("utf-8"));
// 此处使用BASE64做转码功能,同时能起到2次加密的作用。
return new Base64().encodeToString(encrypted);
} catch (UnsupportedEncodingException e) {
Logs.geterrorLogger().error(e.getMessage(), e);
} catch (NoSuchAlgorithmException e) {
Logs.geterrorLogger().error(e.getMessage(), e);
} catch (NoSuchPaddingException e) {
Logs.geterrorLogger().error(e.getMessage(), e);
} catch (InvalidKeyException e) {
Logs.geterrorLogger().error(e.getMessage(), e);
} catch (IllegalBlockSizeException e) {
Logs.geterrorLogger().error(e.getMessage(), e);
} catch (BadPaddingException e) {
Logs.geterrorLogger().error(e.getMessage(), e);
}
return null;
}
/**
* 解密
*
* @param content
* 待解密内容
* @param keyWord
* 解密密钥
* @return byte[]
*/
public static String decrypt(String content,String key) {
try {
// 判断Key是否正确
if (key == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为16位
if (key.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
byte[] raw = key.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
// 先用base64解密
byte[] encrypted1 = new Base64().decode(content);
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
} catch (UnsupportedEncodingException e) {
} catch (NoSuchAlgorithmException e) {
} catch (NoSuchPaddingException e) {
} catch (InvalidKeyException e) {
} catch (IllegalBlockSizeException e) {
} catch (BadPaddingException e) {
}
return null;
}
/**
* 加密
*
* @param content
* 需要加密的内容
* @param keyWord
* 加密密钥
* @return byte[] 加密后的字节数组
*/
public static String encrypt(String content,String key) {
try {
if (key == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为16位
if (key.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
byte[] raw = key.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
// "算法/模式/补码方式"
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(content.getBytes("utf-8"));
// 此处使用BASE64做转码功能,同时能起到2次加密的作用。
return new Base64().encodeToString(encrypted);
} catch (UnsupportedEncodingException e) {
Logs.geterrorLogger().error(e.getMessage(), e);
} catch (NoSuchAlgorithmException e) {
Logs.geterrorLogger().error(e.getMessage(), e);
} catch (NoSuchPaddingException e) {
Logs.geterrorLogger().error(e.getMessage(), e);
} catch (InvalidKeyException e) {
Logs.geterrorLogger().error(e.getMessage(), e);
} catch (IllegalBlockSizeException e) {
Logs.geterrorLogger().error(e.getMessage(), e);
} catch (BadPaddingException e) {
Logs.geterrorLogger().error(e.getMessage(), e);
}
return null;
}
/**
* 解密
*
* @param content
* 待解密内容
* @param keyWord
* 解密密钥
* @return byte[]
*/
public static String decrypt(String content) {
try {
// 判断Key是否正确
if (key == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为16位
if (key.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
byte[] raw = key.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
// 先用base64解密
byte[] encrypted1 = new Base64().decode(content);
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
} catch (UnsupportedEncodingException e) {
} catch (NoSuchAlgorithmException e) {
} catch (NoSuchPaddingException e) {
} catch (InvalidKeyException e) {
} catch (IllegalBlockSizeException e) {
} catch (BadPaddingException e) {
}
return null;
}
public static void main(String[] args) {
// 需要加密的字串
// String cSrc = "123456";
// String cSrc = "ZZFIUPJUS2LLSHIO";
String cSrc = "TLMG6PI6MGMOQDVJ";
System.out.println(cSrc);
// 加密
String enString = AES.encrypt(cSrc);
System.out.println("加密后的字串是:" + enString);
System.out.println("加密后的字串是:" + enString.length());
// 解密
String DeString = AES.decrypt(enString);
System.out.println("解密后的字串是:" + DeString);
System.out.println("解密后的字串是:" + DeString.length());
}
}
|
|