-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakeKeys.java
More file actions
36 lines (30 loc) · 1.12 KB
/
MakeKeys.java
File metadata and controls
36 lines (30 loc) · 1.12 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;
public class MakeKeys {
public static void saveKey(byte[] key, String filename) throws Exception {
FileOutputStream fos = new FileOutputStream(filename);
fos.write(key);
fos.close();
}
public static void generateAndSaveKeys(String name) throws Exception {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
KeyPair pair = keyGen.generateKeyPair();
PrivateKey privateKey = pair.getPrivate();
PublicKey publicKey = pair.getPublic();
saveKey(privateKey.getEncoded(), name + "_private.key");
saveKey(publicKey.getEncoded(), name + "_public.key");
}
public static void main(String[] args) {
try {
generateAndSaveKeys("sender");
generateAndSaveKeys("receiver");
System.out.println("RSA keys generated and saved.");
} catch (Exception e) {
e.printStackTrace();
}
}
}