网站首页 > 开源技术 正文
AES(Advanced Encryption Standard,高级加密标准)是美国国家标准与技术研究院(NIST)于2001年发布的一种对称加密算法,用于替代原有的DES(Data Encryption Standard,数据加密标准)。
AES 具有高度的安全性,支持了超长的密钥长度(128、192、256 位)通过穷举攻击从计算上就是不可行的,它是经过严格的数学分析和广泛的实战测试,可以抵御已知的多种密码攻击方式,例如常见的线性密码分析和差分密码分析等
使用 AES 加密在 Spring Boot 应用中保护敏感数据是在数据安全方面最常见的一种方式,下面我们就来看看在SpringBoot中如何整合AES进行数据加密。
添加依赖
如下所示,引入Security模块,这里需要注意他与SpringSecurity是不一样的。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
创建 AES 加密/解密工具类
创建一个AesUtil的工具类来封装AES加密和解密的操作。如下所示。
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AesUtil {
private static final String ALGORITHM = "AES";
// 生成 AES Key
public static String generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);
keyGen.init(256);
SecretKey secretKey = keyGen.generateKey();
return Base64.getEncoder().encodeToString(secretKey.getEncoded());
}
// 加密操作
public static String encrypt(String data, String key) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode(key), ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encryptedBytes = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
// 解密操作
public static String decrypt(String encryptedData, String key) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode(key), ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decodedBytes = Base64.getDecoder().decode(encryptedData);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes);
}
}
使用 AES 工具类
通过一个Controller控制类来验证AES加密操作。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class EncryptionController {
private static final String SECRET_KEY = "12321kjdahfl"; // 这里你可以用 AesUtil.generateKey() 来生成一个密钥
@GetMapping("/encrypt")
public String encrypt(@RequestParam String data) throws Exception {
return AesUtil.encrypt(data, SECRET_KEY);
}
@GetMapping("/decrypt")
public String decrypt(@RequestParam String encryptedData) throws Exception {
return AesUtil.decrypt(encryptedData, SECRET_KEY);
}
}
测试加密和解密功能
接下来我们就可以通过调用接口来实现数据加解密的操作了,如下所示。
加密操作
http://localhost:8080/api/encrypt?data=hello
解密操作
http://localhost:8080/api/decrypt?encryptedData
到这里我们就完成了整合AES的加解密操作。AES 广泛应用于各种需要数据加密的场合,例如对于静态数据的保护,对于网络传输数据的保护,在移动物联网领域数据传输的加密保护等。
猜你喜欢
- 2024-11-26 MySQL分库分表实战
- 2024-11-26 MobaXterm 之 Network Services
- 2024-11-26 SpringBoot系列(十五)整合缓存,项目必用的技术
- 2024-11-26 盘点认证框架 : SpringSecurity OAuth 篇
- 2024-11-26 @Cacheable缓存key生成策略及自定义key
- 2024-11-26 对称加密算法之Java SM4算法应用 附可用工具类
- 2024-11-26 java中加密算法AES和RSA
- 2024-11-26 领域驱动设计系列课程-领域服务(4)
- 2024-11-26 设计模式——简单工厂模式( Simple Factory Pattern )
- 2024-11-26 Spring Boot使用Redis + Cafeine实现二级缓存?
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)