110,502
社区成员
发帖
与我相关
我的任务
分享
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/*******************************************************************************
* AES加解密算法
*
* @使用byte2hex做转码
*
*/
public class Aes2{
// 加密
public static String Encrypt(String sSrc, String sKey) 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;
}
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes());
return byte2hex(encrypted).toLowerCase();
}
// 解密
public static String Decrypt(String sSrc, String sKey) 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;
}
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec("0102030405060708"
.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = hex2byte(sSrc);
try {
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString;
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
public static byte[] hex2byte(String strhex) {
if (strhex == null) {
return null;
}
int l = strhex.length();
if (l % 2 == 1) {
return null;
}
byte[] b = new byte[l / 2];
for (int i = 0; i != l / 2; i++) {
b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),
16);
}
return b;
}
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) {
hs = hs + "0" + stmp;
} else {
hs = hs + stmp;
}
}
return hs.toUpperCase();
}
public static void main(String[] args) throws Exception {
/*
* 加密用的Key 可以用26个字母和数字组成,最好不要用保留字符,虽然不会错,至于怎么裁决,个人看情况而定
*/
String cKey = " Lvtu24Md9k3Fh5A8";
// 需要加密的字串
String cSrc = "Email : arix04@xxx.com";
System.out.println(cSrc);
// 加密
long lStart = System.currentTimeMillis();
String enString = Aes2.Encrypt(cSrc, cKey);
System.out.println("加密后的字串是:" + enString);
long lUseTime = System.currentTimeMillis() - lStart;
System.out.println("加密耗时:" + lUseTime + "毫秒");
// 解密
lStart = System.currentTimeMillis();
String DeString = Aes2.Decrypt(enString, cKey);
System.out.println("解密后的字串是:" + DeString);
lUseTime = System.currentTimeMillis() - lStart;
System.out.println("解密耗时:" + lUseTime + "毫秒");
}
}
public static string AesDecrypt(string s, string sKey)
{
AesCryptoServiceProvider provider = new AesCryptoServiceProvider();
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < (s.Length / 2); i++)
{
int num2 = Convert.ToInt32(s.Substring(i * 2, 2), 0x10);
buffer[i] = (byte)num2;
}
provider.Key = Encoding.ASCII.GetBytes(sKey);
provider.IV = Encoding.ASCII.GetBytes("0102030405060708");
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
stream2.Close();
stream.Close();
return Encoding.Default.GetString(stream.ToArray());
}
public static string AesEncrypt(string s, string sKey)
{
AesCryptoServiceProvider provider = new AesCryptoServiceProvider();
if (sKey == null)
{
throw new Exception("Key为空null");
}
// 判断Key是否为16位
if (sKey.Length != 16)
{
throw new Exception("Key长度不是16位");
}
byte[] bytes = Encoding.Default.GetBytes(s);
provider.Key = Encoding.ASCII.GetBytes(sKey);
provider.IV = Encoding.ASCII.GetBytes("0102030405060708");
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
foreach (byte num in stream.ToArray())
{
builder.AppendFormat("{0:X2}", num);
}
builder.ToString();
stream2.Close();
stream.Close();
return builder.ToString();
}
public static string Encrypt(string strEncrypt, string strKey)
{
try
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strKey, "md5"));
byte[] strEncryptArray = UTF8Encoding.UTF8.GetBytes(strEncrypt);
byte[] resultArray = null;
using (RijndaelManaged rDel = new RijndaelManaged())
{
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateEncryptor();
resultArray = cTransform.TransformFinalBlock(strEncryptArray, 0, strEncryptArray.Length);
}
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
catch
{
return null;
}
}
public static string Decrypt(string strDecrypt, string strKey)
{
try
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strKey, "md5"));
byte[] strDecryptArray = Convert.FromBase64String(strDecrypt);
byte[] resultArray = null;
using (RijndaelManaged rDel = new RijndaelManaged())
{
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor();
resultArray = cTransform.TransformFinalBlock(strDecryptArray, 0, strDecryptArray.Length);
}
return UTF8Encoding.UTF8.GetString(resultArray);
}
catch
{
return null;
}
}