RSA和AES基础知识,参考上篇超详细!RSA和AES基础知识,本章主要讲解Spring Boot 中实战使用
在 Spring Boot 中实现 RSA + AES 自动接口解密,涉及到使用这两种加密算法来确保数据在传输过程中的安全性。以下是一个高层次的步骤指南,帮助你实现这一功能,亲测有效!
1. 准备工作
生成密钥对
首先,你需要生成 RSA 密钥对(公钥和私钥)。可以使用 openssl
工具或者 Java 的 KeyPairGenerator
类来生成这些密钥。
生成 AES 密钥
你也需要生成一个 AES 密钥,用于对称加密数据。AES 密钥通常是随机生成的,并在传输中通过 RSA 加密进行保护。
2. 配置 Spring Boot
在你的 Spring Boot 项目中,首先需要添加依赖,确保你可以使用相关的加密库:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 你可能还需要其他的加密库 -->
</dependencies>
3. 实现加密和解密
以下是一个简单的实现思路:
1. 加密解密工具类
创建一个工具类,用于处理 AES 和 RSA 加密解密操作。
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
public class CryptoUtils {
private static final String RSA = "RSA";
private static final String AES = "AES";
private static final String RSA_ALGORITHM = "RSA/ECB/PKCS1Padding";
private static final String AES_ALGORITHM = "AES/ECB/PKCS5Padding";
// 生成 AES 密钥
public static SecretKey generateAESKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(AES);
keyGenerator.init(128); // 192 或 256 位也可以
return keyGenerator.generateKey();
}
// AES 加密
public static byte[] encryptAES(byte[] data, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(data);
}
// AES 解密
public static byte[] decryptAES(byte[] encryptedData, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(encryptedData);
}
// RSA 加密
public static byte[] encryptRSA(byte[] data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
// RSA 解密
public static byte[] decryptRSA(byte[] encryptedData, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(encryptedData);
}
// 从 Base64 字符串中获取公钥
public static PublicKey getPublicKey(String base64Key) throws Exception {
byte[] keyBytes = Base64.getDecoder().decode(base64Key);
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
return keyFactory.generatePublic(spec);
}
// 从 Base64 字符串中获取私钥
public static PrivateKey getPrivateKey(String base64Key) throws Exception {
byte[] keyBytes = Base64.getDecoder().decode(base64Key);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
return keyFactory.generatePrivate(spec);
}
}
2. 控制器示例
在你的 Spring Boot 控制器中,你可以使用这些工具类来解密传入的数据。
import org.springframework.web.bind.annotation.*;
import javax.crypto.SecretKey;
import java.util.Base64;
@RestController
@RequestMapping("/api")
public class CryptoController {
private static final String RSA_PUBLIC_KEY = "你的RSA公钥";
private static final String RSA_PRIVATE_KEY = "你的RSA私钥";
@PostMapping("/secure-data")
public String handleSecureData(@RequestBody String encryptedData) throws Exception {
// 1. 解密RSA密钥
PrivateKey privateKey = CryptoUtils.getPrivateKey(RSA_PRIVATE_KEY);
// 2. 解密AES密钥
byte[] decryptedAESKeyBytes = CryptoUtils.decryptRSA(Base64.getDecoder().decode(encryptedData), privateKey);
SecretKey aesKey = new SecretKeySpec(decryptedAESKeyBytes, "AES");
// 3. 解密数据
byte[] encryptedDataBytes = Base64.getDecoder().decode(encryptedData);
byte[] decryptedData = CryptoUtils.decryptAES(encryptedDataBytes, aesKey);
// 处理解密后的数据
return new String(decryptedData);
}
}
4. 安全性注意事项
- 密钥管理:确保密钥的安全存储。实际生产环境中,密钥管理是一个复杂的问题,可能需要使用专门的密钥管理服务。
- 性能考量:加密和解密操作可能会影响性能,尤其是在处理大数据量时。请务必测试并优化你的应用性能。