天天看點

java aes 解密_java實作AES加密解密--資料加解密

packagecom.kongxincai.encanddec;importjava.security.Key;importjava.security.NoSuchAlgorithmException;importjavax.crypto.Cipher;importjavax.crypto.KeyGenerator;importjavax.crypto.SecretKey;importjavax.crypto.spec.SecretKeySpec;public classAESCoder {private static final String KEY_ALGORITHM = "AES";private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";//預設的加密算法

public static byte[] initSecretKey() {//傳回生成指定算法密鑰生成器的 KeyGenerator 對象

KeyGenerator kg = null;try{

kg=KeyGenerator.getInstance(KEY_ALGORITHM);

}catch(NoSuchAlgorithmException e) {

e.printStackTrace();return new byte[0];

}//初始化此密鑰生成器,使其具有确定的密鑰大小//AES 要求密鑰長度為 128

kg.init(128);//生成一個密鑰

SecretKey secretKey =kg.generateKey();returnsecretKey.getEncoded();

}private static Key toKey(byte[] key){//生成密鑰

return newSecretKeySpec(key, KEY_ALGORITHM);

}public static byte[] encrypt(byte[] data,Key key) throwsException{returnencrypt(data, key,DEFAULT_CIPHER_ALGORITHM);

}public static byte[] encrypt(byte[] data,byte[] key) throwsException{returnencrypt(data, key,DEFAULT_CIPHER_ALGORITHM);

}public static byte[] encrypt(byte[] data,byte[] key,String cipherAlgorithm) throwsException{//還原密鑰

Key k =toKey(key);returnencrypt(data, k, cipherAlgorithm);

}public static byte[] encrypt(byte[] data,Key key,String cipherAlgorithm) throwsException{//執行個體化

Cipher cipher =Cipher.getInstance(cipherAlgorithm);//使用密鑰初始化,設定為加密模式

cipher.init(Cipher.ENCRYPT_MODE, key);//執行操作

returncipher.doFinal(data);

}public static byte[] decrypt(byte[] data,byte[] key) throwsException{returndecrypt(data, key,DEFAULT_CIPHER_ALGORITHM);

}public static byte[] decrypt(byte[] data,Key key) throwsException{returndecrypt(data, key,DEFAULT_CIPHER_ALGORITHM);

}public static byte[] decrypt(byte[] data,byte[] key,String cipherAlgorithm) throwsException{//還原密鑰

Key k =toKey(key);returndecrypt(data, k, cipherAlgorithm);

}public static byte[] decrypt(byte[] data,Key key,String cipherAlgorithm) throwsException{//執行個體化

Cipher cipher =Cipher.getInstance(cipherAlgorithm);//使用密鑰初始化,設定為解密模式

cipher.init(Cipher.DECRYPT_MODE, key);//執行操作

returncipher.doFinal(data);

}private static String showByteArray(byte[] data){if(null ==data){return null;

}

StringBuilder sb= new StringBuilder("{");for(byteb:data){

sb.append(b).append(",");

}

sb.deleteCharAt(sb.length()-1);

sb.append("}");returnsb.toString();

}public static void main(String[] args) throwsException {byte[] key =initSecretKey();

System.out.println("key:"+showByteArray(key));

Key k= toKey(key); //生成秘鑰

String data ="AES資料";

System.out.println("加密前資料: string:"+data);

System.out.println("加密前資料: byte[]:"+showByteArray(data.getBytes()));

System.out.println();byte[] encryptData = encrypt(data.getBytes(), k);//資料加密

System.out.println("加密後資料: byte[]:"+showByteArray(encryptData));//System.out.println("加密後資料: hexStr:"+Hex.encodeHexStr(encryptData));

System.out.println();byte[] decryptData = decrypt(encryptData, k);//資料解密

System.out.println("解密後資料: byte[]:"+showByteArray(decryptData));

System.out.println("解密後資料: string:"+newString(decryptData));

}

}