Spring Boot 使用AES加密详解?

Spring Boot 使用AES加密详解?AES Advanced Encryption Standard 高级加密标准 是美国国家标准与技术研究院 NIST 于 2001 年发布的一种对称加密算法 用于替代原有的 DES Data Encryption Standard 数据加密标准

大家好,欢迎来到IT知识分享网。

Spring Boot 使用AES加密详解?

AES(Advanced Encryption Standard,高级加密标准)是美国国家标准与技术研究院(NIST)于2001年发布的一种对称加密算法,用于替代原有的DES(Data Encryption Standard,数据加密标准)。

AES 具有高度的安全性,支持了超长的密钥长度(128、192、256 位)通过穷举攻击从计算上就是不可行的,它是经过严格的数学分析和广泛的实战测试,可以抵御已知的多种密码攻击方式,例如常见的线性密码分析和差分密码分析等

使用 AES 加密在 Spring Boot 应用中保护敏感数据是在数据安全方面最常见的一种方式,下面我们就来看看在SpringBoot中如何整合AES进行数据加密。

添加依赖

如下所示,引入Security模块,这里需要注意他与SpringSecurity是不一样的。

 
   
   
     org.springframework.boot 
    
   
     spring-boot-starter 
    
   
   
   
     org.springframework.boot 
    
   
     spring-boot-starter-security 
    
   

创建 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 广泛应用于各种需要数据加密的场合,例如对于静态数据的保护,对于网络传输数据的保护,在移动物联网领域数据传输的加密保护等。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/90077.html

(0)
上一篇 2026-04-01 16:15
下一篇 2026-04-01 22:46

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信