AESRSA混合加密java
时间: 2023-10-19 09:08:46 浏览: 130
AESRSA混合加密是一种常见的加密方式,它结合了AES(对称加密)和RSA(非对称加密)两种加密算法。下面是一个Java示例代码,演示如何使用AESRSA混合加密:
```java
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
public class AESRSACipher {
private static final String AES_ALGORITHM = "AES/CBC/PKCS5Padding";
private static final String RSA_ALGORITHM = "RSA";
// 生成RSA公私钥对
public static KeyPair generateRSAKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA_ALGORITHM);
keyPairGenerator.initialize(2048); // 设置RSA密钥长度为2048位
return keyPairGenerator.generateKeyPair();
}
// 使用AES加密明文,并返回加密后的密文和IV
public static byte[][] encryptAES(byte[] plaintext, SecretKeySpec key) throws Exception {
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
AlgorithmParameters params = cipher.getParameters();
byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
byte[] ciphertext = cipher.doFinal(plaintext);
return new byte[][] { ciphertext, iv };
}
// 使用RSA公钥加密AES密钥,并返回加密后的密文
public static byte[] encryptRSA(byte[] keyBytes, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] ciphertext = cipher.doFinal(keyBytes);
return ciphertext;
}
// 使用RSA私钥解密AES密钥,并返回解密后的明文
public static byte[] decryptRSA(byte[] keyBytes, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] plaintext = cipher.doFinal(keyBytes);
return plaintext;
}
// 使用AES解密密文,并返回解密后的明文
public static byte[] decryptAES(byte[] ciphertext, byte[] iv, SecretKeySpec key) throws Exception {
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
byte[] plaintext = cipher.doFinal(ciphertext);
return plaintext;
}
public static void main(String[] args) throws Exception {
// 生成RSA公私钥对
KeyPair keyPair = generateRSAKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
// 准备待加密的数据和密钥
String plaintext = "Hello, world!";
byte[] aesKeyBytes = new byte[16]; // AES密钥长度为128位
SecretKeySpec aesKey = new SecretKeySpec(aesKeyBytes, "AES");
// 使用AES加密明文,并返回加密后的密文和IV
byte[][] aesCiphertextAndIV = encryptAES(plaintext.getBytes(), aesKey);
byte[] aesCiphertext = aesCiphertextAndIV[0];
byte[] iv = aesCiphertextAndIV[1];
// 使用RSA公钥加密AES密钥,并返回加密后的密文
byte[] rsaCiphertext = encryptRSA(aesKeyBytes, publicKey);
// 使用RSA私钥解密AES密钥,并返回解密后的明文
byte[] aesKeyBytesDecrypted = decryptRSA(rsaCiphertext, privateKey);
SecretKeySpec aesKeyDecrypted = new SecretKeySpec(aesKeyBytesDecrypted, "AES");
// 使用AES解密密文,并返回解密后的明文
byte[] plaintextDecrypted = decryptAES(aesCiphertext, iv, aesKeyDecrypted);
System.out.println(new String(plaintextDecrypted)); // 输出明文
}
}
```
上述代码中,我们首先生成了RSA公私钥对。接着,我们使用AES加密明文,并使用RSA公钥加密AES密钥,将加密后的AES密钥与密文一起发送给接收方。接收方使用RSA私钥解密AES密钥,再使用AES解密密文,从而得到明文。
阅读全文