用RSA加密AES的密钥k,后用AES加密数据,Java实现
时间: 2024-05-06 19:18:59 浏览: 22
以下是用RSA加密AES密钥k,再用AES加密数据的Java代码示例:
```java
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
public class Encryptor {
public static void main(String[] args) throws Exception {
// 生成AES密钥
SecretKey aesKey = generateAESKey();
// 生成RSA公私钥对
KeyPair keyPair = generateRSAKeyPair();
// 用RSA公钥加密AES密钥k
byte[] encryptedKey = encryptRSA(aesKey.getEncoded(), keyPair.getPublic());
// 用AES密钥k加密数据
String plainText = "Hello, world!";
byte[] encryptedData = encryptAES(plainText.getBytes(), aesKey);
System.out.println("Encrypted key: " + bytesToHex(encryptedKey));
System.out.println("Encrypted data: " + bytesToHex(encryptedData));
// 用RSA私钥解密AES密钥k
byte[] decryptedKey = decryptRSA(encryptedKey, keyPair.getPrivate());
// 用AES密钥k解密数据
byte[] decryptedData = decryptAES(encryptedData, decryptedKey);
System.out.println("Decrypted data: " + new String(decryptedData));
}
// 生成AES密钥
public static SecretKey generateAESKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = new SecureRandom();
keyGenerator.init(128, secureRandom);
return keyGenerator.generateKey();
}
// 生成RSA公私钥对
public static KeyPair generateRSAKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
SecureRandom secureRandom = new SecureRandom();
keyPairGenerator.initialize(2048, secureRandom);
return keyPairGenerator.generateKeyPair();
}
// 用RSA公钥加密数据
public static byte[] encryptRSA(byte[] data, Key publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
// 用RSA私钥解密数据
public static byte[] decryptRSA(byte[] encryptedData, Key privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(encryptedData);
}
// 用AES密钥加密数据
public static byte[] encryptAES(byte[] data, SecretKey aesKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
return cipher.doFinal(data);
}
// 用AES密钥解密数据
public static byte[] decryptAES(byte[] encryptedData, byte[] aesKeyBytes) throws Exception {
SecretKey aesKey = new SecretKeySpec(aesKeyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, aesKey);
return cipher.doFinal(encryptedData);
}
// 将字节数组转为十六进制字符串
public static String bytesToHex(byte[] bytes) {
StringBuilder stringBuilder = new StringBuilder();
for (byte b : bytes) {
stringBuilder.append(String.format("%02X", b));
}
return stringBuilder.toString();
}
}
```
注意,这里用AES-128加密数据,生成RSA-2048公私钥对,可以根据需要进行调整。此外,为了方便演示,这里使用了字符串类型的数据,实际上在实际应用中,可能需要将数据转为字节数组后再进行加密。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)