AES加密

****以上實現需要Apache下的codec相關jar文件和bouncy相關jar文件


package com.natureframework.util.cipher;



import java.security.Key;
import java.security.Security;


import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;


/**
 * AES安全編碼通用組件工具
 * 
 * @author 周正德
 * @version 1.0
 * 
 */
public class AESCoder {


static {
try {
Security.addProvider(new BouncyCastleProvider());
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* 密鑰算法名
*/
public static final String KEY_ALGORITHM = "AES";


/**

* 加密/解密算法 / 工作模式 / 填充方式 Java 7支持PKCS5PADDEING ,需要修改JDK授權jar文件,只持256的長度

* 注意在不需要額外修改JDK相關安全授權jar文件,利用PKSC7Padding因爲在國內能支持128位的長度

*/
public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS7Padding";


/**
* 轉換密鑰

* @param key
*            二進制密鑰
* @return Key 加密的密鑰
* @throws Exception
*/
private static Key toKey(byte[] key) throws Exception {
SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
return secretKey;
}


/**
* 解密

* @param data
*            等解密數據
* @param key
*            密鑰
* @return byte[] 解密的數據
* @throws Exception
*/
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
Key k = toKey(key);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
cipher.init(Cipher.DECRYPT_MODE, k);
return cipher.doFinal(data);
}


/**
* 加密

* @param data
*            等加密數據
* @param key
*            密鑰
* @return byte[] 加密後的數據
* @throws Exception
*/
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
Key k = toKey(key);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, k);
return cipher.doFinal(data);
}


/**
* 生成密鑰

* @return byte[] 二進制密鑰
* @throws Exception
*/
public static byte[] initKey() throws Exception {
KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
kg.init(128);
SecretKey secretKey = kg.generateKey();
return secretKey.getEncoded();
}


/**
* 初始化密鑰

* @return
* @throws Exception
*/
public static String initKeyString() throws Exception {
return Base64.encodeBase64String(initKey());
}


/**
* 獲取密鑰

* @param key
* @return
* @throws Exception
*/
public static byte[] getKey(String key) throws Exception {
return Base64.decodeBase64(key);
}


/**
* 解密

* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] decrypt(byte[] data, String key) throws Exception {
return decrypt(data, getKey(key));
}


/**
* 加密

* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encrypt(byte[] data, String key) throws Exception {
return encrypt(data, getKey(key));
}


/**
* 摘要處理

* @param data
* @return
*/
public static String shaHex(byte[] data) {
return DigestUtils.md5Hex(data);
}

/**
* 驗證
* @param data
* @param messageDigest
* @return
*/
public static boolean validate(byte[] data, String messageDigest) {
return messageDigest.equals(shaHex(data));
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章