javaaes加密/解密
时间: 2023-10-14 16:07:56 浏览: 131
Java中可以使用javax.crypto包中的类来进行AES加密和解密。具体实现步骤如下:
1. 生成一个AES密钥
```
KeyGenerator keygen = KeyGenerator.getInstance("AES");
keygen.init(128);
SecretKey key = keygen.generateKey();
```
2. 创建Cipher对象,并初始化为加密模式或解密模式
```
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
```
3. 加密/解密数据
```
byte[] ciphertext = cipher.doFinal(plaintext);
byte[] plaintext = cipher.doFinal(ciphertext);
```
完整代码示例:
```
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class AESUtil {
private static final String CHARSET_NAME = "UTF-8";
private static final String AES_ALGORITHM = "AES/CBC/PKCS5Padding";
/**
* 随机生成AES密钥
*
* @param keySize 密钥长度,单位:位
* @return 生成的密钥
* @throws NoSuchAlgorithmException 异常
*/
public static byte[] generateKey(int keySize) throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(keySize);
SecretKey secretKey = keyGenerator.generateKey();
return secretKey.getEncoded();
}
/**
* AES加密
*
* @param plaintext 明文
* @param key 密钥
* @param iv 初始化向量
* @return 密文
* @throws NoSuchPaddingException 异常
* @throws NoSuchAlgorithmException 异常
* @throws InvalidAlgorithmParameterException 异常
* @throws InvalidKeyException 异常
* @throws BadPaddingException 异常
* @throws IllegalBlockSizeException 异常
*/
public static byte[] encrypt(byte[] plaintext, byte[] key, byte[] iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(iv));
return cipher.doFinal(plaintext);
}
/**
* AES解密
*
* @param ciphertext 密文
* @param key 密钥
* @param iv 初始化向量
* @return 明文
* @throws NoSuchPaddingException 异常
* @throws NoSuchAlgorithmException 异常
* @throws InvalidAlgorithmParameterException 异常
* @throws InvalidKeyException 异常
* @throws BadPaddingException 异常
* @throws IllegalBlockSizeException 异常
*/
public static byte[] decrypt(byte[] ciphertext, byte[] key, byte[] iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(iv));
return cipher.doFinal(ciphertext);
}
/**
* 生成随机的初始化向量
*
* @param ivSize 初始化向量长度,单位:位
* @return 生成的初始化向量
*/
public static byte[] generateIv(int ivSize) {
byte[] iv = new byte[ivSize / 8];
new SecureRandom().nextBytes(iv);
return iv;
}
/**
* 将字节数组转换为十六进制字符串
*
* @param bytes 字节数组
* @return 十六进制字符串
*/
public static String toHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2);
for (byte b : bytes) {
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
}
/**
* 将十六进制字符串转换为字节数组
*
* @param hexString 十六进制字符串
* @return 字节数组
*/
public static byte[] toByteArray(String hexString) {
int len = hexString.length() / 2;
byte[] bytes = new byte[len];
for (int i = 0; i < len; i++) {
int index = i * 2;
bytes[i] = (byte) Integer.parseInt(hexString.substring(index, index + 2), 16);
}
return bytes;
}
public static void main(String[] args) throws Exception {
// 随机生成16字节的AES密钥和16字节的初始化向量
byte[] key = generateKey(128);
byte[] iv = generateIv(128);
String plaintextStr = "hello, AES!";
byte[] plaintext = plaintextStr.getBytes(CHARSET_NAME);
// AES加密
byte[] ciphertext = encrypt(plaintext, key, iv);
String ciphertextHex = toHexString(ciphertext);
System.out.println("密文:" + ciphertextHex);
// AES解密
byte[] ciphertextBytes = toByteArray(ciphertextHex);
byte[] decrypted = decrypt(ciphertextBytes, key, iv);
String decryptedStr = new String(decrypted, CHARSET_NAME);
System.out.println("明文:" + decryptedStr);
}
}
相关问题:
阅读全文