-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSAKeyGen.java
More file actions
36 lines (32 loc) · 1.49 KB
/
Copy pathRSAKeyGen.java
File metadata and controls
36 lines (32 loc) · 1.49 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
import java.io.FileOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class RSAKeyGen {
public static void main(String[] args) {
try {
// Generate a key pair
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048); // You can adjust the key size as needed
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// Save the public key to publickey.pem
PublicKey publicKey = keyPair.getPublic();
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
try (FileOutputStream fos = new FileOutputStream("publickey.pem")) {
fos.write(x509EncodedKeySpec.getEncoded());
}
// Save the private key to privatekey.pem
PrivateKey privateKey = keyPair.getPrivate();
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
try (FileOutputStream fos = new FileOutputStream("privatekey.pem")) {
fos.write(pkcs8EncodedKeySpec.getEncoded());
}
System.out.println("Public and private keys have been generated and saved.");
} catch (Exception e) {
e.printStackTrace();
}
}
}