-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathECC.java
More file actions
100 lines (91 loc) · 3.59 KB
/
ECC.java
File metadata and controls
100 lines (91 loc) · 3.59 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
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
import java.math.BigInteger;
import java.security.PublicKey;
import java.security.PrivateKey;
import java.security.KeyFactory;
import java.security.Security;
import java.security.KeyPairGenerator;
import java.security.KeyPair;
import java.security.SecureRandom;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.ECGenParameterSpec;
import javax.crypto.KeyAgreement;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.interfaces.ECPublicKey;
import org.bouncycastle.jce.interfaces.ECPrivateKey;
import org.bouncycastle.jce.ECNamedCurveTable;
import org.bouncycastle.jce.spec.ECParameterSpec;
import org.bouncycastle.jce.spec.ECPublicKeySpec;
import org.bouncycastle.jce.spec.ECPrivateKeySpec;
import org.bouncycastle.math.ec.ECPoint;
public class ECC {
static public void stampaBytes(byte[] message) {
for (int i=0; i<message.length; i++) {
if(i%16==0 && i>0) System.out.println("");
System.out.print(String.format("%02X", message[i])+".");
}
System.out.println("\n");
}
public static byte[] savePublicKey (PublicKey key) throws Exception {
ECPublicKey eckey = (ECPublicKey)key;
return eckey.getQ().getEncoded(true);
}
public static PublicKey loadPublicKey (byte [] data) throws Exception {
ECParameterSpec params = ECNamedCurveTable.getParameterSpec("secp256k1");
ECPublicKeySpec pubKey = new ECPublicKeySpec(
params.getCurve().decodePoint(data), params);
try {
KeyFactory kf = KeyFactory.getInstance("ECDH", "BC");
return kf.generatePublic(pubKey);
} catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException e) {
e.printStackTrace();
return null;
}
}
public static byte[] savePrivateKey (PrivateKey key) throws Exception {
ECPrivateKey eckey = (ECPrivateKey)key;
return eckey.getD().toByteArray();
}
public static PrivateKey loadPrivateKey (byte [] data) throws Exception {
ECParameterSpec params = ECNamedCurveTable.getParameterSpec("secp256k1");
ECPrivateKeySpec prvkey = new ECPrivateKeySpec(new BigInteger(data), params);
try {
KeyFactory kf = KeyFactory.getInstance("ECDH", "BC");
return kf.generatePrivate(prvkey);
} catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException e) {
e.printStackTrace();
return null;
}
}
public static byte[] doECDH (byte[] dataPrv, byte[] dataPub) throws Exception {
Security.addProvider(new BouncyCastleProvider());
try {
KeyAgreement ka = KeyAgreement.getInstance("ECDH", "BC");
ka.init(loadPrivateKey(dataPrv));
ka.doPhase(loadPublicKey(dataPub), true);
byte[] secret = ka.generateSecret(); //32bytes
return secret;
} catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
e.printStackTrace();
return null;
}
}
public static byte[][] keysGen() throws Exception {
Security.addProvider(new BouncyCastleProvider());
try {
KeyPairGenerator kpgen = KeyPairGenerator.getInstance("ECDH", "BC");
kpgen.initialize(new ECGenParameterSpec("secp256k1"), new SecureRandom());
KeyPair pair = kpgen.generateKeyPair();
byte[] dataPrv = savePrivateKey(pair.getPrivate()); //32bytes
byte[] dataPub = savePublicKey(pair.getPublic()); //32bytes
byte[][] pairByte={dataPrv,dataPub};
return pairByte;
} catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
e.printStackTrace();
return null;
}
}
}