-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA.java
37 lines (32 loc) · 1.26 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
import java.math.BigInteger;
public class RSA {
public static void main(String[] args) {
// M = C^d mod N, ed = 1 mod (p-1)(q-1)
BigInteger n = new BigInteger("3174654383");
BigInteger e = new BigInteger("65537");
BigInteger C = new BigInteger("2487688703");
BigInteger p = null;
BigInteger q = null;
BigInteger euler_phi_function = null; // 오일러 피함수
BigInteger d = null; // e의 역수
BigInteger M = null; // plaintext
BigInteger i = new BigInteger("2");
while(i.compareTo(n) <= 0){
if(n.remainder(i).equals(new BigInteger("0"))){
p = i;
q = n.divide(p);
euler_phi_function = p.subtract(BigInteger.ONE).
multiply(q.subtract(BigInteger.ONE)); // (p-1)(q-1)
break;
}
i=i.add(BigInteger.ONE);
}
d = e.modPow(new BigInteger("-1"), euler_phi_function);
M = C.modPow(d, n);
System.out.println("p = " + p);
System.out.println("q = " + q);
System.out.println("euler_phi_function = (p-1)*(q-1) = " + euler_phi_function);
System.out.println("\nPlaintext = " + M);
System.out.println("d = " + d);
}
}