欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > 【SpringBoot】RSA加密(非对称加密)

【SpringBoot】RSA加密(非对称加密)

2024/10/25 16:25:34 来源:https://blog.csdn.net/m0_61606343/article/details/138337298  浏览:    关键词:【SpringBoot】RSA加密(非对称加密)

一、关于RSA

        RSA是一种非对称加密算法,广泛应用于数据加密和数字签名领域。

        RSA算法是由罗纳德·李维斯特(Ron Rivest)、阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)在1977年提出的。该算法基于一个十分简单的数论事实:将两个大素数相乘得到它们的乘积很容易,但反过来,已知乘积压根求解出这两个素数却极其困难。这种特性使得RSA能够有效地进行公钥加密和私钥解密。

        在Java中,实现RSA算法需要生成一对密钥,即公钥和私钥。公钥用于加密数据,而私钥用于解密数据。具体来说,当数据被公钥加密后,只能通过相应的私钥解密;反之亦然,当数据被私钥加密后,只能通过相应的公钥解密。这种设计确保了数据的安全性,因为即使公钥被公开,没有私钥,数据也无法被破解。

        在Java中,可以使用`java.security.KeyPairGenerator`类来生成RSA密钥对。通常,密钥的长度为2048位,这是目前推荐的密钥长度,因为它提供了足够的安全性。生成的公钥和私钥可以转换成Base64编码的字符串,方便存储和传输。

        除了加密和解密功能外,RSA还常用于数字签名和验签。私钥可以用来签署数据,生成签名,而公钥则用来验证这个签名的真实性。这一功能在电子商务和网络通信中尤为重要,它保证了数据的完整性和不可抵赖性。例如,在网上支付过程中,用户通过私钥对支付信息进行签名,支付平台使用用户的公钥验证签名的真实性,从而确认交易是用户本人发起的。

        RSA算法也被广泛应用于HTTPS协议中,以保障互联网数据传输的安全性。在HTTPS协议中,服务器使用RSA算法的公钥加密传输给客户端的数据,客户端使用对应的私钥解密数据,确保数据在传输过程中不被第三方窥视或篡改。

        RSA算法作为一种非对称加密技术,不仅提供了高安全性的数据保护,还在数字签名方面有着广泛的应用。在Java环境中,通过各种工具类和库函数,可以便捷地实现RSA的密钥生成、数据加密解密以及签名和验签操作。

二、代码实现

生成公钥和私钥

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.util.Base64;public class Main {/*** RSA算法常量,用于生成RSA密钥对和进行加解密操作。*/private static final String RSA_ALGORITHM = "RSA";/*** 生成RSA密钥对。** @return KeyPair 包含公钥和私钥的密钥对。* @throws NoSuchAlgorithmException 如果RSA算法不可用,抛出此异常。*/public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {// 实例化一个密钥对生成器,指定算法为RSAKeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA_ALGORITHM);// 初始化密钥对生成器,指定密钥长度为2048位keyPairGenerator.initialize(2048); // 密钥大小为2048位// 生成密钥对return keyPairGenerator.generateKeyPair();}/*** 使用公钥加密数据。** @param data 待加密的明文数据。* @param publicKey 公钥,用于加密数据。* @return String 加密后的数据,以Base64编码表示。* @throws Exception 如果加密过程中发生错误,抛出此异常。** 此部分代码首先实例化一个Cipher对象,用于执行加密操作。它使用RSA算法,* 并初始化为加密模式。然后,使用提供的公钥将Cipher对象配置为准备加密状态。* 最后,将待加密的数据转换为字节数组,进行加密操作,并将加密后的数据* 使用Base64编码为字符串返回。*/public static String encrypt(String data, PublicKey publicKey) throws Exception {Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);cipher.init(Cipher.ENCRYPT_MODE, publicKey);byte[] encryptedData = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));return Base64.getEncoder().encodeToString(encryptedData);}/*** 使用私钥解密数据。** @param encryptedData 加密后的数据,以Base64编码表示。* @param privateKey 私钥,用于解密数据。* @return String 解密后的明文数据。* @throws Exception 如果解密过程中发生错误,抛出此异常。*/public static String decrypt(String encryptedData, PrivateKey privateKey) throws Exception {byte[] decodedData = Base64.getDecoder().decode(encryptedData);Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);cipher.init(Cipher.DECRYPT_MODE, privateKey);byte[] decryptedData = cipher.doFinal(decodedData);return new String(decryptedData, StandardCharsets.UTF_8);}/*** 程序入口主方法,用于演示RSA加密和解密的流程。* 生成RSA密钥对,并将公钥和私钥以Base64编码的形式打印出来。* 使用公钥对明文数据进行加密,然后使用私钥对加密后的数据进行解密,展示加密解密的完整性。** @param args 命令行参数* @throws Exception 如果密钥生成或加密解密过程中发生错误*/public static void main(String[] args) throws Exception {// 生成RSA密钥对KeyPair keyPair = generateKeyPair();// 获取公钥PublicKey publicKey = keyPair.getPublic();// 获取私钥PrivateKey privateKey = keyPair.getPrivate();// 将公钥转换为Base64编码的字符串String publicKeyBase64 = Base64.getEncoder().encodeToString(publicKey.getEncoded());// 打印公钥System.out.println("公钥(Base64编码): \n" + publicKeyBase64 + "\n");// 将私钥转换为Base64编码的字符串String privateKeyBase64 = Base64.getEncoder().encodeToString(privateKey.getEncoded());// 打印私钥System.out.println("私钥(Base64编码): \n" + privateKeyBase64 + "\n");// 待加密的明文数据String data = "学习RSA加密";// 使用公钥加密明文数据String encryptedData = encrypt(data, publicKey);// 打印加密后的数据System.out.println("加密后的数据:" + encryptedData);// 使用私钥解密加密后的数据String decryptedData = decrypt(encryptedData, privateKey);// 打印解密后的数据System.out.println("解密后的数据:" + decryptedData);}}

测试生成的公钥和私钥是否能用

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;public class TestRsaKeys {public static void main(String[] args) throws Exception {// 替换成你的实际公钥和私钥String publicKeyPEM = "公钥";String privateKeyPEM = "私钥";// 解码公钥byte[] publicBytes = Base64.getDecoder().decode(publicKeyPEM);X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(publicBytes);KeyFactory keyFactory = KeyFactory.getInstance("RSA");PublicKey publicKey = keyFactory.generatePublic(publicSpec);// 解码私钥byte[] privateBytes = Base64.getDecoder().decode(privateKeyPEM);PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(privateBytes);PrivateKey privateKey = keyFactory.generatePrivate(privateSpec);// 打印公钥和私钥System.out.println("Public Key: " + publicKey);System.out.println("Private Key: " + privateKey);}
}

将生成的公钥和私钥保存至yml文件

security:rsa:public-key: 公钥private-key: 私钥

相关工具类

import org.springframework.stereotype.Component;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import javax.crypto.Cipher;/*** RSA工具类,提供RSA加密和解密的功能。*/
@Component
public class RsaUtil {private final RsaKeyProperties rsaKeyProperties;private PublicKey publicKey;private PrivateKey privateKey;/*** 构造函数,初始化RSA密钥对。** @param rsaKeyProperties RSA密钥配置属性。* @throws Exception 如果密钥加载失败。*/public RsaUtil(RsaKeyProperties rsaKeyProperties) throws Exception {this.rsaKeyProperties = rsaKeyProperties;loadKeys();}/*** 加载RSA公钥和私钥。** @throws Exception 如果密钥解码或实例化失败。*/private void loadKeys() throws Exception {// 清理公钥字符串中的换行符并解码String publicKeyPEM = rsaKeyProperties.getPublicKey().replaceAll("\\n", "").replaceAll("\\r", "");byte[] publicBytes = Base64.getDecoder().decode(publicKeyPEM);X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(publicBytes);KeyFactory keyFactory = KeyFactory.getInstance("RSA");this.publicKey = keyFactory.generatePublic(publicSpec);// 清理私钥字符串中的换行符并解码String privateKeyPEM = rsaKeyProperties.getPrivateKey().replaceAll("\\n", "").replaceAll("\\r", "");byte[] privateBytes = Base64.getDecoder().decode(privateKeyPEM);PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(privateBytes);this.privateKey = keyFactory.generatePrivate(privateSpec);}/*** 使用RSA公钥加密数据。** @param data 待加密的数据。* @return 加密后的数据字符串。* @throws Exception 如果加密失败。*/public String encrypt(String data) throws Exception {Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE, publicKey);byte[] encryptedData = cipher.doFinal(data.getBytes("UTF-8"));return Base64.getEncoder().encodeToString(encryptedData);}/*** 使用RSA私钥解密数据。** @param encryptedData 待解密的加密数据字符串。* @return 解密后的数据字符串。* @throws Exception 如果解密失败。*/public String decrypt(String encryptedData) throws Exception {Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.DECRYPT_MODE, privateKey);byte[] decodedData = Base64.getDecoder().decode(encryptedData);byte[] decryptedData = cipher.doFinal(decodedData);return new String(decryptedData, "UTF-8");}}
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;/*** RSA密钥配置类,用于存储和访问RSA公钥和私钥。* 该类通过@ConfigurationProperties注解绑定到配置文件中以security.rsa为前缀的属性。*/
@Configuration
@ConfigurationProperties(prefix = "security.rsa")
public class RsaKeyProperties {// 存储RSA公钥private String publicKey;// 存储RSA私钥private String privateKey;/*** 获取RSA公钥。** @return RSA公钥字符串*/public String getPublicKey() {return publicKey;}/*** 设置RSA公钥。** @param publicKey RSA公钥字符串*/public void setPublicKey(String publicKey) {this.publicKey = publicKey;}/*** 获取RSA私钥。** @return RSA私钥字符串*/public String getPrivateKey() {return privateKey;}/*** 设置RSA私钥。** @param privateKey RSA私钥字符串*/public void setPrivateKey(String privateKey) {this.privateKey = privateKey;}}

测试

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class SpringBootRsaApplication implements CommandLineRunner {@Autowiredprivate RsaUtil rsaUtil;public static void main(String[] args) {SpringApplication.run(SpringBootRsaApplication.class, args);}@Overridepublic void run(String... args) throws Exception {String originalMessage = "测试一下RSA加密方法";String encryptedMessage = rsaUtil.encrypt(originalMessage);String decryptedMessage = rsaUtil.decrypt(encryptedMessage);System.out.println("原文:" + originalMessage);System.out.println("加密后: " + encryptedMessage);System.out.println("解密后:" + decryptedMessage);}}

运行结果

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com