-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA.java
104 lines (82 loc) · 2.97 KB
/
RSA.java
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 java.math.BigInteger;
import java.security.SecureRandom;
/**
*
* @author Aris
*/
public class RSA {
public static void main(String[] args) {
long startTime = System.nanoTime();
final int BITS = Integer.parseInt(args[0]);
final BigInteger p = generatePrime(BITS);
final BigInteger q = generatePrime(BITS);
final BigInteger n = p.multiply(q);
final BigInteger phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
final BigInteger e = get_encryption_key(phi);
final BigInteger d = get_decryption_key(e, phi);
// ------------------------------------------------------------------------------------------
final BigInteger message = generateMessage(n);
final BigInteger encrypted_text = encrypt(e, message, n);
final BigInteger decrypted_text = decrypt(d, encrypted_text, n);
System.out.println("Public key: [e = " + e + ", n = " + n + "]");
System.out.println("Private key: [d = " + d + ", n = " + n + "]");
System.out.println("Plain text: " + message);
System.out.println("Encrypted text: " + encrypted_text);
System.out.println("Decrypted text: " + decrypted_text);
long endTime = System.nanoTime();
long totalTime = endTime - startTime;
System.out.println("RSA encryption finished in: " + totalTime / 1000000000.0 + " seconds.");
}
private static BigInteger get_encryption_key(BigInteger phi) {
BigInteger key = BigInteger.ZERO;
BigInteger counter = new BigInteger("2");
while (counter.compareTo(phi) < 0) {
if (gcd(phi, counter).equals(BigInteger.ONE)) {
key = counter;
break;
}
counter = counter.add(BigInteger.ONE);
}
return key;
}
private static BigInteger get_decryption_key(BigInteger e, BigInteger phi) {
return e.modInverse(phi);
}
private static BigInteger encrypt(BigInteger e, BigInteger plain, BigInteger n) {
return plain.modPow(e, n);
}
private static BigInteger decrypt(BigInteger d, BigInteger cipher, BigInteger n) {
return cipher.modPow(d, n);
}
private static BigInteger gcd(BigInteger a, BigInteger b) {
BigInteger dividend = (a.compareTo(b) >= 0) ? a : b;
BigInteger divisor = (a.compareTo(b) <= 0) ? a : b;
while (!divisor.equals(BigInteger.ZERO)) {
BigInteger remainder = dividend.mod(divisor);
dividend = divisor;
divisor = remainder;
}
return dividend;
}
private static BigInteger generatePrime(int bits) {
SecureRandom ran = new SecureRandom();
BigInteger prime = new BigInteger(bits, ran);
while (true) {
if (prime.isProbablePrime(1)) {
break;
}
prime = prime.subtract(new BigInteger("1"));
}
return prime;
}
private static BigInteger generateMessage(BigInteger n) {
BigInteger message = BigInteger.ZERO;
while (true) {
message = new BigInteger(n.bitLength(), new SecureRandom());
if (message.compareTo(n) < 0) {
break;
}
}
return message;
}
}