-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyGenerator.java
More file actions
48 lines (40 loc) · 1.71 KB
/
KeyGenerator.java
File metadata and controls
48 lines (40 loc) · 1.71 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
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import java.io.FileOutputStream;
import java.io.IOException;
public class KeyGenerator {
public static String serializePublicKey(PublicKey publicKey) {
// Encode the public key in X.509 format (DER encoded) and then Base64 encode
return Base64.getEncoder().encodeToString(publicKey.getEncoded());
}
public static String serializePrivateKey(PrivateKey privateKey) {
// Encode the private key in PKCS#8 format (DER encoded) and then Base64 encode
return Base64.getEncoder().encodeToString(privateKey.getEncoded());
}
public static void main(String[] args) throws Exception {
if (args.length < 1) {
System.err.println("Usage: java KeyGenerator <name>");
System.exit(1);
}
String name = args[0];
// Generate RSA key pair
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
KeyPair pair = keyGen.generateKeyPair();
// Serialize keys
String publicKeyStr = serializePublicKey(pair.getPublic());
String privateKeyStr = serializePrivateKey(pair.getPrivate());
// Save keys to files
writeToFile(name + ".pub", publicKeyStr);
writeToFile(name + ".key", privateKeyStr);
System.out.println("Keys saved as " + name + ".pub and " + name + ".key");
}
private static void writeToFile(String filename, String content) throws IOException {
try (FileOutputStream fos = new FileOutputStream(filename)) {
fos.write(content.getBytes());
}
}
}