编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

Spring Boot 使用AES加密详解?

wxchong 2024-11-26 09:31:11 开源技术 24 ℃ 0 评论

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

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表