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的密钥生成、数据加密解密以及签名和验签操作。
二、代码实现
生成公钥和私钥
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | 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 { // 实例化一个密钥对生成器,指定算法为RSA KeyPairGenerator 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); } } |
测试生成的公钥和私钥是否能用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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文件
1 2 3 4 | security: rsa: public -key: 公钥 private -key: 私钥 |
相关工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | 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" ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 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; } } |
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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 { @Autowired private RsaUtil rsaUtil; public static void main(String[] args) { SpringApplication.run(SpringBootRsaApplication. class , args); } @Override public 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); } } |
原文链接:https://blog.csdn.net/m0_61606343/article/details/138337298
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了