在现代软件开发中,数据加密和解密是保护敏感信息的重要手段。本文将介绍如何在 Spring Boot 项目中使用 Java 的 SecretKeySpec
和 Cipher
类来实现对称加密和解密。
对称加密算法使用相同的密钥进行加密和解密。其主要优点包括速度快和实现简单。常见的对称加密算法有 AES、DES 等。本文将以 AES 为例,展示如何在 Spring Boot 项目中进行数据加密和解密。
对称加密(Symmetric Encryption)是一种使用单一密钥(即同一密钥)进行加密和解密的加密方法。加密和解密过程使用相同的密钥,因此加密方和解密方都必须拥有该密钥。
非对称加密(Asymmetric Encryption)是一种使用一对密钥(公钥和私钥)进行加密和解密的加密方法。公钥用于加密,私钥用于解密。公钥可以公开发布,而私钥必须保密。
在实际应用中,常常将对称加密和非对称加密结合使用。例如,在HTTPS协议中,首先使用非对称加密进行密钥交换,然后使用对称加密进行数据传输。这样既保证了密钥的安全性,又提高了数据传输的效率。
首先,确保你的 Spring Boot 项目已经创建并运行。你可以使用 Spring Initializr 或者你的 IDE 快速创建一个新的 Spring Boot 项目。
在 pom.xml
文件中添加以下依赖:
xml复制代码
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
接下来,我们创建一个工具类 CryptoUtil
,用于实现加密和解密功能。
java复制代码
package com.example.demo.util;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class CryptoUtil {
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES";
// 16-byte secret key
private static final String SECRET_KEY = "mySuperSecretKey";
public static String encrypt(String input) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] encryptedBytes = cipher.doFinal(input.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String input) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] decodedBytes = Base64.getDecoder().decode(input);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes);
}
}
接下来,我们创建一个 REST 控制器来测试加密和解密功能。
java复制代码
package com.example.demo.controller;
import com.example.demo.util.CryptoUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CryptoController {
@GetMapping("/encrypt")
public String encrypt(@RequestParam String plaintext) {
try {
return CryptoUtil.encrypt(plaintext);
} catch (Exception e) {
e.printStackTrace();
return "Error encrypting data";
}
}
@GetMapping("/decrypt")
public String decrypt(@RequestParam String ciphertext) {
try {
return CryptoUtil.decrypt(ciphertext);
} catch (Exception e) {
e.printStackTrace();
return "Error decrypting data";
}
}
}
启动 Spring Boot 应用,并使用浏览器或者 Postman 访问以下 URL:
http://localhost:8080/encrypt?plaintext=HelloWorld
YWJjZGVmZ2hpamtsbW5vcHFy
http://localhost:8080/decrypt?ciphertext=YWJjZGVmZ2hpamtsbW5vcHFy
HelloWorld
通过本文,你学会了如何在 Spring Boot 项目中使用 SecretKeySpec
和 Cipher
实现对称加密和解密。我们使用 AES 算法对字符串进行加密和解密,并通过 REST 控制器来测试这些功能。希望这篇文章对你有所帮助,并能为你的项目提供安全性保障。如果你有任何问题或建议,欢迎随时交流。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。