❝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/encrypt?data=hello 解密数据:http://localhost:8080/decrypt?data=加密后的数据
结语
AES 是一种高效、安全的对称加密算法,通过一系列复杂的轮操作来确保数据的保密性和完整性。理解 AES 的工作原理,有助于在实际应用中正确和安全地使用 AES 加密算法,保护敏感数据的安全。
在实际项目中,AES 通常与其他安全措施(如 HMAC、RSA 和 HTTPS)结合使用,以提供全面的数据安全保护。
个人观点,仅供参考,如有问题,欢迎留言讨论。
文章转载自源话编程,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
Java 开发玩转 MCP:从 Claude 自动化到 Spring AI Alibaba 生态整合
阿里巴巴中间件
70次阅读
2025-04-08 11:01:30
Spring 实现数据库读写分离
听溪
42次阅读
2025-03-30 23:19:06
SpringMVC+SpringBoot篇
戏说数据那点事
25次阅读
2025-04-17 10:42:02
【阿斯加德特快】WebFlux:Spring召唤雷霆战甲!每秒百万请求?索尔:我还没发力呢!
让天下没有难学的编程
19次阅读
2025-04-23 14:33:56
UEFI深度解析
老柴杂货铺
18次阅读
2025-04-04 18:44:11
Spring Boot集成openGauss DataVec实现高效RAG知识问答
openGauss
14次阅读
2025-04-15 09:49:56
Spring这6种初始化Bean的方式,个个是精华!
猿java
7次阅读
2025-04-16 08:00:51
在 Spring中,id和name命名Bean有什么区别?
猿java
7次阅读
2025-04-17 08:00:15
什么是服务限流?为什么要限流?
猿java
6次阅读
2025-04-09 08:00:39