-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathECKey.java
118 lines (105 loc) · 2.86 KB
/
ECKey.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
109
110
111
112
113
114
115
116
117
118
//package ecc.elliptic;
//import ecc.Key;
//import ecc.Rand;
import java.math.BigInteger;
import java.io.*;
public class ECKey implements Key
{
/** There are to kinds of keys secret and public */
protected boolean secret;
protected BigInteger sk;
protected ECPoint beta;
protected EllipticCurve mother;
/** ECKey generates a random secret key (contains also the public key)*/
public ECKey(EllipticCurve ec)
{
mother = ec;
secret = true;
sk=new BigInteger(ec.getp().bitLength() + 17,Rand.om); //sk is a random num.
if (mother.getOrder() != null) sk=sk.mod(mother.getOrder()); //sk=sk%order
beta=(mother.getGenerator()).multiply(sk); //beta=generator*sk (public key)
beta.fastCache();
}
public String toString()
{
if (secret) return ( "Secret key: "+ sk +" "+ beta +" "+ mother);
else return("Public key:"+ beta +" "+ mother);
}
public boolean isPublic()
{
return (!secret);
}
public void writeKey(OutputStream out) throws IOException
{
DataOutputStream output = new DataOutputStream(out);
mother.writeCurve(output);
output.writeBoolean(secret);
if(secret)
{
byte[] skb = sk.toByteArray();
output.writeInt(skb.length);
output.write(skb);
}
byte[] betab = beta.compress();
output.writeInt(betab.length);
output.write(betab);
}
public Key readKey(InputStream in) throws IOException
{
DataInputStream input = new DataInputStream(in);
ECKey k = new ECKey(new EllipticCurve(input));
k.secret = input.readBoolean();
if(k.secret)
{
byte[] skb = new byte[input.readInt()];
input.read(skb);
k.sk = new BigInteger(skb);
}
byte[] betab = new byte[input.readInt()];
input.read(betab);
k.beta = new ECPoint(betab, k.mother);
return k;
}
/** Turns this key into a public key (does nothing if this key is public) */
public Key getPublic()
{
Key temp = new ECKey(mother);
((ECKey)temp).beta = beta;
((ECKey)temp).sk = BigInteger.ZERO;
((ECKey)temp).secret = false;
System.gc();
return temp;
}
/** Turns this key into a public key (does nothing if this key is public) */
public Key getpublic() throws Exception
{
Key temp = new ECKey(mother);
int[] k = new int[mother.ppodbf.intValue()];
for(int i=0;i<mother.ppodbf.intValue();i++)
{
if(i==0) k[i] =0;
else if(k[i-1]==0) k[i]=1;
else k[i]=0;
}
ECPoint R0 = ((ECKey)temp).beta;
ECPoint R1 = ((ECKey)temp).beta.multiply(new BigInteger("2"));
for(int i=mother.ppodbf.intValue();i>=0;i--)
{
if(k[i]==0)
{
R1 = R0.add(R1);
R0 = R0.multiply(new BigInteger("2"));
}
else
{
R0 = R0.add(R1);
R1 = R1.multiply(new BigInteger("2"));
}
}
((ECKey)temp).beta = R0;
((ECKey)temp).sk = BigInteger.ZERO;
((ECKey)temp).secret = false;
System.gc();
return temp;
}
}