-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyGen.java
More file actions
28 lines (21 loc) · 817 Bytes
/
KeyGen.java
File metadata and controls
28 lines (21 loc) · 817 Bytes
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
import java.io.FileOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.util.Base64;
import java.io.File;
public class KeyGen {
public static void main(String[] args) throws Exception {
new File("keys").mkdirs();
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
gen.initialize(2048);
KeyPair pair = gen.generateKeyPair();
write("keys/public.key", pair.getPublic().getEncoded());
write("keys/private.key", pair.getPrivate().getEncoded());
System.out.println("Keys generated.");
}
private static void write(String path, byte[] key) throws Exception {
try (FileOutputStream fos = new FileOutputStream(path)) {
fos.write(Base64.getEncoder().encode(key));
}
}
}