-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA.java
67 lines (55 loc) · 1.4 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
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Scanner;
public class RSA
{
private BigInteger P;
private BigInteger Q;
private BigInteger N;
private BigInteger PHI;
private BigInteger e;
private BigInteger d;
private int maxLength = 1024;
private SecureRandom R = new SecureRandom();
public RSA()
{
P = BigInteger.probablePrime(maxLength, R);
Q = BigInteger.probablePrime(maxLength, R);
N = P.multiply(Q);
PHI = P.subtract(BigInteger.ONE).multiply( Q.subtract(BigInteger.ONE));
e = BigInteger.probablePrime(maxLength / 2, R);
while (PHI.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(PHI) < 0)
{
e.add(BigInteger.ONE);
}
d = e.modInverse(PHI);
}
public RSA(BigInteger e, BigInteger N)
{
this.e = e;
this.N = N;
}
public RSA(BigInteger e, BigInteger N, BigInteger d)
{
this.e = e;
this.d = d;
this.N = N;
}
public BigInteger encryptMessage(BigInteger message)
{
return message.modPow(e, N);
}
public BigInteger decryptMessage(BigInteger message)
{
return message.modPow(d, N);
}
public BigInteger getE(){
return e;
}
public BigInteger getN(){
return N;
}
public BigInteger getD(){
return d;
}
}