-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyGeneratorTest.java
More file actions
71 lines (59 loc) · 2.8 KB
/
KeyGeneratorTest.java
File metadata and controls
71 lines (59 loc) · 2.8 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
import java.security.KeyFactory;
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;
import java.util.Base64;
import javax.crypto.Cipher;
public class KeyGeneratorTest {
// Serialize public key to Base64 string
public static String serializePublicKey(PublicKey publicKey) {
return Base64.getEncoder().encodeToString(publicKey.getEncoded());
}
// Serialize private key to Base64 string
public static String serializePrivateKey(PrivateKey privateKey) {
return Base64.getEncoder().encodeToString(privateKey.getEncoded());
}
// Deserialize public key from Base64 string
public static PublicKey deserializePublicKey(String publicKeyStr) throws Exception {
byte[] bytes = Base64.getDecoder().decode(publicKeyStr);
X509EncodedKeySpec spec = new X509EncodedKeySpec(bytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(spec);
}
// Deserialize private key from Base64 string
public static PrivateKey deserializePrivateKey(String privateKeyStr) throws Exception {
byte[] bytes = Base64.getDecoder().decode(privateKeyStr);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(bytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(spec);
}
public static void main(String[] args) throws Exception {
// Generate key pair
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
KeyPair pair = keyGen.generateKeyPair();
// Serialize to strings
String pubStr = serializePublicKey(pair.getPublic());
String privStr = serializePrivateKey(pair.getPrivate());
// Deserialize from strings
PublicKey pubDeserialized = deserializePublicKey(pubStr);
PrivateKey privDeserialized = deserializePrivateKey(privStr);
// Test: Encrypt with public, decrypt with private
String message = "Hello, world!";
Cipher encryptCipher = Cipher.getInstance("RSA");
encryptCipher.init(Cipher.ENCRYPT_MODE, pubDeserialized);
byte[] encrypted = encryptCipher.doFinal(message.getBytes());
Cipher decryptCipher = Cipher.getInstance("RSA");
decryptCipher.init(Cipher.DECRYPT_MODE, privDeserialized);
byte[] decrypted = decryptCipher.doFinal(encrypted);
String decryptedMessage = new String(decrypted);
if (message.equals(decryptedMessage)) {
System.out.println("Test passed: Key serialization/deserialization works.");
} else {
System.out.println("Test failed: Decrypted message does not match original.");
}
}
}