-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRSAKey.java
108 lines (97 loc) · 2.92 KB
/
RSAKey.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
105
106
107
108
//package ecc.rsa;
//import ecc.Key;
//import ecc.Rand;
import java.math.BigInteger;
import java.io.*;
public class RSAKey implements Key {
public BigInteger n;
protected BigInteger phin;
protected BigInteger p;
protected BigInteger q;
protected BigInteger e;
protected BigInteger d;
protected boolean secret;
public boolean isPublic() {
return (!secret);
}
public RSAKey(int bits) {
secret=true;
p = new BigInteger(bits/2, 500, Rand.om);
q = new BigInteger(bits/2, 500, Rand.om);
n = p.multiply(q);
phin = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
e = new BigInteger(bits, Rand.om);
while(!e.gcd(phin).equals(BigInteger.ONE)) {
e = new BigInteger(bits, Rand.om);
}
d = e.modInverse(phin);
}
private RSAKey() {}
/** setkey takes a key string generated by tostring and sets this key to its parameters*/
public void setkey(String keystr) {
return;
}
/** Turns this key into a public key (does nothing if this key is public) */
public Key getPublic() {
Key temp = new RSAKey();
((RSAKey)temp).n = n;
((RSAKey)temp).e = e;
((RSAKey)temp).secret=false;
return temp;
}
public void writeKey(OutputStream out) throws IOException {
DataOutputStream output = new DataOutputStream(out);
output.writeBoolean(secret);
if(secret) {
System.out.println("Writing Secret key");
byte[] pb = p.toByteArray();
output.writeInt(pb.length);
output.write(pb);
byte[] qb = q.toByteArray();
output.writeInt(qb.length);
output.write(qb);
byte[] db = d.toByteArray();
output.writeInt(db.length);
output.write(db);
} else {
System.out.println("Writing Public key");
byte[] nb = n.toByteArray();
output.writeInt(nb.length);
output.write(nb);
}
byte[] eb = e.toByteArray();
output.writeInt(eb.length);
output.write(eb);
}
// Secret: (p, q, d, e)
// Public: (n, e)
public Key readKey(InputStream in) throws IOException {
DataInputStream input = new DataInputStream(in);
RSAKey key = new RSAKey();
if(input.readBoolean()) {
System.out.println("Reading Secret key");
key.secret = true;
byte[] pb = new byte[input.readInt()];
input.read(pb);
key.p = new BigInteger(pb);
byte[] qb = new byte[input.readInt()];
input.read(qb);
key.q = new BigInteger(qb);
byte[] db = new byte[input.readInt()];
input.read(pb);
key.d = new BigInteger(db);
key.n = p.multiply(q);
key.phin = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
} else {
System.out.println("Reading Public key");
key.secret = false;
byte[] nb = new byte[input.readInt()];
input.read(nb);
key.n = new BigInteger(nb);
}
byte[] eb = new byte[input.readInt()];
input.read(eb);
key.e = new BigInteger(eb);
return key;
}
}