forked from DP-3T/dp3t-sdk-backend
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGenerateKeyPairEC.java
39 lines (36 loc) · 1.49 KB
/
GenerateKeyPairEC.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
/**
* This file only serves as an example on how to get keys in the right encoding.
* In order to use it you need BouncyCastle on the classpath!
*
* DO NOT USE THEM IN PRODUCTION UNLESS THE KEYSPECS ARE OK FOR YOU
*/
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import java.security.spec.ECGenParameterSpec;
import java.security.Security;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class GenerateKeyPairEC{
public static void main(String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());
Security.setProperty("crypto.policy", "unlimited");
KeyPairGenerator generator = KeyPairGenerator.getInstance("ECDSA", "BC");
ECGenParameterSpec spec = new ECGenParameterSpec("secp256r1");
generator.initialize(spec);
KeyPair pair = generator.genKeyPair();
PrivateKey privateKey = pair.getPrivate();
PublicKey publicKey = pair.getPublic();
FileOutputStream outputStream = new FileOutputStream("generated_pub.pem");
outputStream.write(Base64.getEncoder().encode(publicKey.getEncoded()));
outputStream.close();
outputStream = new FileOutputStream("generated_private.pem");
outputStream.write(Base64.getEncoder().encode(privateKey.getEncoded()));
outputStream.close();
}
}