-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRSA.java
More file actions
139 lines (113 loc) · 4.21 KB
/
RSA.java
File metadata and controls
139 lines (113 loc) · 4.21 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Copyright © https://github.com/microwind All rights reserved.
*
* @author: jarryli@gmail.com
* @version: 1.0
*/
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Base64;
public class RSA {
private BigInteger n, e, d;
/**
* RSA构造函数
* @param bitLength 密钥位数
*/
public RSA(int bitLength) {
generateKeys(bitLength);
}
/**
* 生成RSA密钥对
* @param bitLength 密钥位数
*/
private void generateKeys(int bitLength) {
System.out.println("生成RSA密钥对...");
SecureRandom random = new SecureRandom();
// 选择两个大质数
BigInteger p = BigInteger.probablePrime(bitLength / 2, random);
BigInteger q = BigInteger.probablePrime(bitLength / 2, random);
System.out.println("生成质数p: " + p);
System.out.println("生成质数q: " + q);
// 计算n = p * q
n = p.multiply(q);
System.out.println("计算n = p * q: " + n);
// 计算φ(n) = (p-1)*(q-1)
BigInteger phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
System.out.println("计算φ(n): " + phi);
// 选择公钥指数e
e = BigInteger.valueOf(65537);
System.out.println("选择公钥指数e: " + e);
// 计算私钥指数d
d = e.modInverse(phi);
System.out.println("计算私钥指数d: " + d);
System.out.println("RSA密钥对生成完成");
}
/**
* RSA加密算法
* @param message 明文消息
* @return Base64编码的密文
*/
public String encrypt(String message) {
System.out.println("开始RSA加密...");
System.out.println("明文: " + message);
BigInteger m = new BigInteger(message.getBytes());
System.out.println("转换为数字: " + m);
BigInteger c = m.modPow(e, n);
System.out.println("加密计算 c = m^e mod n: " + c);
String result = Base64.getEncoder().encodeToString(c.toByteArray());
System.out.println("加密完成,密文长度: " + result.length());
return result;
}
/**
* RSA解密算法
* @param ciphertext Base64编码的密文
* @return 解密后的明文
*/
public String decrypt(String ciphertext) {
System.out.println("开始RSA解密...");
System.out.println("密文: " + ciphertext);
BigInteger c = new BigInteger(Base64.getDecoder().decode(ciphertext));
System.out.println("转换为数字: " + c);
BigInteger m = c.modPow(d, n);
System.out.println("解密计算 m = c^d mod n: " + m);
String result = new String(m.toByteArray());
System.out.println("解密完成,明文长度: " + result.length());
return result;
}
/**
* 获取公钥信息
* @return 公钥字符串
*/
public String getPublicKey() {
return String.format("公钥(n,e): (%s,%s)", n, e);
}
/**
* 获取私钥信息
* @return 私钥字符串
*/
public String getPrivateKey() {
return String.format("私钥(n,d): (%s,%s)", n, d);
}
/**
* 主函数,演示RSA加密和解密
*/
public static void main(String[] args) {
System.out.println("=== RSA算法演示 ===");
RSA rsa = new RSA(512); // 512位密钥
System.out.println(rsa.getPublicKey());
System.out.println(rsa.getPrivateKey());
String message = "Hello, RSA!";
System.out.println("\n原始消息: " + message);
System.out.println("消息长度: " + message.length());
// 加密
String encrypted = rsa.encrypt(message);
System.out.println("加密结果: " + encrypted);
// 解密
String decrypted = rsa.decrypt(encrypted);
System.out.println("解密结果: " + decrypted);
// 验证
boolean isValid = message.equals(decrypted);
System.out.println("验证结果: " + (isValid ? "✓ 成功" : "✗ 失败"));
System.out.println("=== 演示结束 ===");
}
}