-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyGenMain.java
More file actions
73 lines (56 loc) · 1.91 KB
/
KeyGenMain.java
File metadata and controls
73 lines (56 loc) · 1.91 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
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Random;
public class KeyGenMain{
static public void main(String[] argv){
int n = Integer.valueOf(argv[0]);
n = n/2;
if(n <= 0) return;
BigInteger one = BigInteger.ONE;
BigInteger zero = BigInteger.ZERO;
SecureRandom rand = new SecureRandom();
FermatTest FT = new FermatTest();
GCDBinaryEuclid GCDBE = new GCDBinaryEuclid();
//素数pの生成
BigInteger p = one;
Boolean bool = false;
do{
BigInteger v1 = new BigInteger(n, rand);
if(v1.signum() == 1) p = v1;
int t = Integer.valueOf("10");
if(p.compareTo(BigInteger.valueOf(3)) < 0 || !p.testBit(0)) continue;
bool = FT.isProbablePrime(p, t);
}while(bool == false);
//System.out.println("Prime p:\n"+ p +"\n");//素数pの表示
//素数qの生成
BigInteger q = one;
bool = false;
do{
BigInteger v2 = new BigInteger(n, rand);
if(v2.signum() == 1) q = v2;
int t2 = Integer.valueOf("10");
if(q.compareTo(BigInteger.valueOf(3)) < 0 || !q.testBit(0)) continue;
bool = FT.isProbablePrime(q, t2);
}while(bool == false);
//System.out.println("Prime q:\n"+ q + "\n");//素数qの表示
BigInteger N = p.multiply(q);
System.out.println("N:\n" + N);
BigInteger a = p.subtract(one); //マイナス1
BigInteger b = q.subtract(one); //マイナス1
BigInteger pq = a.multiply(b);
BigInteger g = GCDBE.gcd(a, b);
BigInteger L = pq.divide(g);
BigInteger gcd = one.add(one);
BigInteger e = one;
do{
BigInteger v3 = new BigInteger(2*n, rand);
if(v3.signum() == 1 && v3.compareTo(pq) == -1) e = v3;
if(e.compareTo(BigInteger.valueOf(3)) < 0 || !e.testBit(0)) continue;
gcd = GCDBE.gcd(e, L);
}while(gcd.compareTo(one) == 1);
System.out.println("Public Key:\n" + e + "\n");
ModInverse MI = new ModInverse();
BigInteger d = MI.modInverse(e, L);
System.out.println("Private Key:\n" + d + "\n");
}
}