forked from DP-3T/dp3t-sdk-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateKeyPair.java
34 lines (31 loc) · 1.27 KB
/
GenerateKeyPair.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
/**
* This file only serves as an example on how to get keys in the right encoding.
* This outputs the keys in the PKCS8 format for the private key and the X509 format for the publickey
*
* 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.Security;
public class GenerateKeyPair{
public static void main(String[] args) throws Exception {
Security.setProperty("crypto.policy", "unlimited");
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
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();
}
}