-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandshakeCryptoTester.java
30 lines (26 loc) · 1.3 KB
/
HandshakeCryptoTester.java
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
import java.security.*;
public class HandshakeCryptoTester {
static String PRIVATEKEYFILE = "client-private.der";
static String CERTFILE = "ca.pem";
static String PLAINTEXT = "Time flies like an arrow. Fruit flies like a banana.";
static String ENCODING = "UTF-8"; /* For converting between strings and byte arrays */
static public void main(String[] args) throws Exception {
/* Extract key pair */
PublicKey publickey = HandshakeCrypto.getPublicKeyFromCertFile(CERTFILE);
PrivateKey privatekey = HandshakeCrypto.getPrivateKeyFromKeyFile(PRIVATEKEYFILE);
/* Encode string as bytes */
byte[] plaininputbytes = PLAINTEXT.getBytes(ENCODING);
/* Encrypt it */
byte[] cipher = HandshakeCrypto.encrypt(plaininputbytes, publickey);
/* Then decrypt back */
byte[] plainoutputbytes = HandshakeCrypto.decrypt(cipher, privatekey);
/* Decode bytes into string */
String plainoutput = new String(plainoutputbytes, ENCODING);
if (plainoutput.equals(PLAINTEXT)) {
System.out.println("Pass. Input and output strings are the same: \"" + PLAINTEXT + "\"");
}
else {
System.out.println("Fail. Expected \"" + PLAINTEXT + "\", but got \"" + plainoutput + "\'");
}
}
}