AES(Advanced Encryption Standard,高级加密标准)是一种对称加密算法,广泛应用于数据加密领域。本文将详细介绍如何在 Spring Boot 项目中使用 AES 进行数据加密和解密。
基本概念
对称加密
对称加密使用相同的密钥进行加密和解密。加密和解密过程中的密钥必须保密,只有加密方和解密方知道该密钥。
AES 密钥长度
AES 支持三种密钥长度:
-
128 位
-
192 位
-
256 位
不同的密钥长度对应不同的安全强度和计算开销。
在 Spring Boot 项目中实战演练
配置加密密钥
在 application.yml 文件中配置加密密钥:
aes_secret_key: XKrCHvcwbCOvfJxwE7cjcs5ALnz9i0ElE05RlRJnT84=
添加加密依赖
在你的pom.xml
中添加如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.68</version>
</dependency>
编写加密工具
接下来,我们需要一个工具类来进行加密和解密。我们使用 AES 对称加密算法。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
@Component
public class AESUtil {
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES";
private static String SECRET_KEY;
// 从配置文件中读取密钥
@Value("${aes_secret_key}")
public void setSecretKey(String secretKey) {
SECRET_KEY = secretKey;
}
// 加密
public static String encrypt(String data) throws Exception {
System.out.println(SECRET_KEY);
SecretKeySpec secretKey = new SecretKeySpec(Base64.getDecoder().decode(SECRET_KEY), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedData = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encryptedData);
}
// 解密
public static String decrypt(String encryptedData) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(Base64.getDecoder().decode(SECRET_KEY), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
return new String(decryptedData);
}
}
创建一个加密的 API
现在,我们可以创建一个简单的 API 来测试我们的加密功能。创建一个控制器类来处理 API 请求。
import com.example.securityapi.utils.AESUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EncryptionController {
@GetMapping("/encrypt")
public String encrypt(@RequestParam String data) {
try {
return AESUtil.encrypt(data);
} catch (Exception e) {
return "加密失败: " + e.getMessage();
}
}
@GetMapping("/decrypt")
public String decrypt(@RequestParam String data) {
try {
return AESUtil.decrypt(data);
} catch (Exception e) {
return "解密失败: " + e.getMessage();
}
}
}
运行你的项目
现在,你可以运行你的 Spring Boot 应用,并通过浏览器或 Postman 来测试 API 加密和解密功能。访问以下URL来测试:
-
解密数据:http://localhost:8080/decrypt?data=加密后的数据
总结
AES 是一种高效、安全的对称加密算法,通过一系列复杂的轮操作来确保数据的保密性和完整性。理解 AES 的工作原理,有助于在实际应用中正确和安全地使用 AES 加密算法,保护敏感数据的安全。
在实际项目中,AES 通常与其他安全措施(如 HMAC、RSA 和 HTTPS)结合使用,以提供全面的数据安全保护。