java編寫的AES數據加密

主要是aes加密後的密文,會變的奇大,便想把數據壓縮和加密結合到一起,於是寫了一下這個工具類!

不過如果加密的數據過短,數據量反而會變大,只有數據量比較大的時候纔會體現出來!


csdn下載地址:http://download.csdn.net/detail/yuanfen7650/7816033

希望賺點資源分,謝謝大家!


測試函數:

public static void main(String[] args) {
String key = "abdawqe";
String content = "alsdlsalfdlflaslelwqlewqlals";
String miwen = encrypt(key, content);
System.out.println("密文:"+miwen);
System.out.println("原文:"+decrypt(key, miwen));
System.out.println("原文長度:"+content.length() + "   密文長度:" + miwen.length());
}


源代碼:

public static boolean isCompress=true;//是否進行壓縮

// 壓縮
private static String compress(String str) {
if(!isCompress)return str;
if (str == null || str.length() == 0) {
return str;
}
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
// return out.toString("ISO-8859-1");
return Base64.encode(out.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}


// 解壓�?
private static String uncompress(String str) {
if(!isCompress)return str;
if (str == null || str.length() == 0) {
return str;
}
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(Base64.decode(str));
GZIPInputStream gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
// toString()使用平臺默認編碼,也可以顯式的指定如toString("GBK")
return out.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}


/**
* 加密

* @param key
*            密鑰
* @param content
*            內容
*/
public static String encrypt(String key, String content) {
if(content==null)return null;
content=compress(content);
byte[] encryptResult = encryptByte(content, key);
if (encryptResult == null)
return null;
String miwen = Base64.encode(encryptResult);
return compress(miwen);
}


/**
* 解密

* @param key
*            密鑰
* @param content
*            密文
*/
public static String decrypt(String key, String content) {
byte[] decryptResult = decryptByte(Base64.decode(uncompress(content)),
key);
if (decryptResult == null)
return null;
return uncompress(new String(decryptResult));
}


/**
* 加密

* @param content
*            �?��加密的內�?
* @param password
*            加密密碼
* @return
*/
public static byte[] encryptByte(String content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 創建密碼�?
byte[] byteContent = content.getBytes("utf-8");
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;
}


/**
* 解密

* @param content
*            待解密內�?
* @param password
*            解密密鑰
* @return
*/
public static byte[] decryptByte(byte[] content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 創建密碼�?
cipher.init(Cipher.DECRYPT_MODE, key);// 初始�?
byte[] result = cipher.doFinal(content);
return 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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章