Java AES 加密算法实现与模式详解
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.shiro.codec.Hex;
public class AES {
static Cipher cipher;
static final String KEY_ALGORITHM = "AES";
static final String CIPHER_ALGORITHM_ECB = "AES/ECB/PKCS5Padding";
static final String CIPHER_ALGORITHM_CBC = "AES/CBC/PKCS5Padding";
static final String CIPHER_ALGORITHM_CBC_NoPadding = "AES/CBC/NoPadding";
static SecretKey secretKey;
public static void main(String[] args) throws Exception {
}
static void method1(String str) throws Exception {
cipher = Cipher.getInstance(KEY_ALGORITHM);
secretKey = KeyGenerator.getInstance(KEY_ALGORITHM).generateKey();
System.out.println("服务器端生成密钥的为:" + secretKey.getEncoded());
byte[] keyBytes = secretKey.getEncoded();
secretKey = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encrypt = cipher.doFinal(str.getBytes());
System.out.println("客户端加密后:" + Arrays.toString(encrypt));
secretKey = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decrypt = cipher.doFinal(encrypt);
System.out.println("服务器端解密后:" + new String(decrypt));
}
public static void testAES(String content) throws Exception {
Cipher serverCiper = Cipher.getInstance("AES");
SecretKey serverSecretKey = KeyGenerator.getInstance("AES").generateKey();
byte[] keyBytes = serverSecretKey.getEncoded();
String keyStr = Hex.encodeToString(keyBytes);
System.out.println("密钥为:"+keyStr);
SecretKey clientSecretKey = new SecretKeySpec(Hex.decode(keyStr), "AES");
Cipher clientCiper = Cipher.getInstance("AES");
clientCiper.init(Cipher.ENCRYPT_MODE, clientSecretKey);
String encryptStr = Hex.encodeToString(clientCiper.doFinal(content.getBytes()));
System.out.println("客户端加密后数据:"+encryptStr);
serverCiper.init(Cipher.DECRYPT_MODE, serverSecretKey);
byte[] decodeBytes = serverCiper.doFinal(Hex.decode(encryptStr));
System.out.println("服务器解密后数据:"+new String(decodeBytes));
}
static void method2(String str) throws Exception {
cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);
secretKey = KeyGenerator.getInstance(KEY_ALGORITHM).generateKey();
System.out.println("密钥的长度为:" + secretKey.getEncoded().length);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encrypt = cipher.doFinal(str.getBytes());
System.out.println("method2-加密:" + Arrays.toString(encrypt));
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decrypt = cipher.doFinal(encrypt);
System.out.println("method2-解密后:" + new String(decrypt));
}
static byte[] getIV() {
String iv = "1234567812345678";
return iv.getBytes();
}
static void method3(String str) throws Exception {
cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
secretKey = KeyGenerator.getInstance(KEY_ALGORITHM).generateKey();
System.out.println("密钥的长度为:" + secretKey.getEncoded().length);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(getIV()));
byte[] encrypt = cipher.doFinal(str.getBytes());
System.out.println("method3-加密:" + Arrays.toString(encrypt));
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(getIV()));
byte[] decrypt = cipher.doFinal(encrypt);
System.out.println("method3-解密后:" + new String(decrypt));
}
static void method4(String str) throws Exception {
cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC_NoPadding);
secretKey = KeyGenerator.getInstance(KEY_ALGORITHM).generateKey();
System.out.println("密钥的长度为:" + secretKey.getEncoded().length);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(getIV()));
byte[] encrypt = cipher.doFinal(str.getBytes(), 0, str.length());
System.out.println("method4-加密:" + Arrays.toString(encrypt));
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(getIV()));
byte[] decrypt = cipher.doFinal(encrypt);
System.out.println("method4-解密后:" + new String(decrypt));
}
}