-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathPEMEncodingsTest.java
More file actions
99 lines (77 loc) · 2.67 KB
/
PEMEncodingsTest.java
File metadata and controls
99 lines (77 loc) · 2.67 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.security.*;
String PEM = """
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEi/kRGOL7wCPTN4KJ2ppeSt5UYB6u
cPjjuKDtFTXbguOIFDdZ65O/8HTUqS/sVzRF+dg7H3/tkQ/36KdtuADbwQ==
-----END PUBLIC KEY-----
""";
void main() throws Exception {
decodeExamples();
var keyPair = createKeyPair();
var encodedKeys = encodeKeyPair(keyPair);
decode(encodedKeys);
}
void decodeExamples() {
IO.println("=== Decoding Examples ===");
PEMDecoder pemDec = PEMDecoder.of();
var message = switch (pemDec.decode(PEM)) {
case PublicKey publicKey -> "PublicKey created:\n" + publicKey;
case PrivateKey privateKey -> "PrivateKey created:\n" + privateKey;
default -> "Invalid PEM";
};
IO.println(message);
// we also can direct decode the type
PublicKey publicKey = pemDec.decode(PEM, ECPublicKey.class);
IO.println("\nECPublicKey created:");
IO.println(publicKey);
// using password
PublicKey eckey = pemDec.withDecryption("password".toCharArray())
.decode(PEM, ECPublicKey.class);
}
KeyPair createKeyPair() throws NoSuchAlgorithmException {
IO.println("\n=== Generating keys ===");
KeyPairGenerator generator = KeyPairGenerator.getInstance("EC");
generator.initialize(256);
var keyPair = generator.generateKeyPair();
IO.println("PublicKey generated:");
IO.println(keyPair.getPublic());
IO.println("\nPrivateKey generated:");
IO.println(keyPair.getPrivate());
return keyPair;
}
PEMKeys encodeKeyPair(KeyPair pair) {
IO.println("\n=== Encoding keys ===");
PrivateKey privateKey = pair.getPrivate();
PublicKey publicKey = pair.getPublic();
// Old way
/*
byte[] privBytes = privateKey.getEncoded(); // X.509
byte[] pubBytes = publicKey.getEncoded(); // PKCS#8
String privPem = "-----BEGIN PRIVATE KEY-----\n" +
Base64.getMimeEncoder().encodeToString(privBytes) +
"\n-----END PRIVATE KEY-----";
String pubPem = "-----BEGIN PUBLIC KEY-----\n" +
Base64.getMimeEncoder().encodeToString(pubBytes) +
pemEnc.encodeToString(publicKey) +
*/
PEMEncoder pemEnc = PEMEncoder.of();
String privPem = pemEnc.encodeToString(privateKey);
String pubPem = pemEnc.encodeToString(publicKey);
IO.println("Public Key encoded:");
IO.println(pubPem);
IO.println("Private Key encoded:");
IO.println(privPem);
return new PEMKeys(pubPem, privPem);
}
void decode(PEMKeys keys) {
IO.println("\n=== Decoding keys ===");
PEMDecoder pemDec = PEMDecoder.of();
PublicKey publicKey = pemDec.decode(keys.publicKey(), PublicKey.class);
IO.println("PublicKey decoded:");
IO.println(publicKey);
PrivateKey privateKey = pemDec.decode(keys.privateKey(), PrivateKey.class);
IO.println("\nPrivateKey decoded:");
IO.println(privateKey);
}
record PEMKeys(String publicKey, String privateKey) {
}