-
Notifications
You must be signed in to change notification settings - Fork 24
/
KEMExample.java
52 lines (39 loc) · 2 KB
/
KEMExample.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import org.openquantumsafe.*;
import java.util.ArrayList;
import java.util.Arrays;
public class KEMExample {
public static void main(String[] args) {
System.out.println("Supported KEMs:");
Common.print_list(KEMs.get_supported_KEMs());
System.out.println();
System.out.println("Enabled KEMs:");
Common.print_list(KEMs.get_enabled_KEMs());
System.out.println();
String kem_name = "Kyber512";
KeyEncapsulation client = new KeyEncapsulation(kem_name);
client.print_details();
System.out.println();
long t = System.currentTimeMillis();
byte[] client_public_key = client.generate_keypair();
long timeElapsed = System.currentTimeMillis() - t;
System.out.println("Client public key:");
System.out.println(Common.chop_hex(client_public_key));
System.out.println("\nIt took " + timeElapsed + " millisecs to generate the key pair.");
KeyEncapsulation server = new KeyEncapsulation(kem_name);
t = System.currentTimeMillis();
Pair<byte[], byte[]> server_pair = server.encap_secret(client_public_key);
System.out.println("It took " + (System.currentTimeMillis() - t) + " millisecs to encapsulate the secret.");
byte[] ciphertext = server_pair.getLeft();
byte[] shared_secret_server = server_pair.getRight();
t = System.currentTimeMillis();
byte[] shared_secret_client = client.decap_secret(ciphertext);
System.out.println("It took " + (System.currentTimeMillis() - t) + " millisecs to decapsulate the secret.");
client.dispose_KEM();
server.dispose_KEM();
System.out.println("\nClient shared secret:");
System.out.println(Common.chop_hex(shared_secret_client));
System.out.println("\nServer shared secret:");
System.out.println(Common.chop_hex(shared_secret_server));
System.out.println("\nShared secrets coincide? " + Arrays.equals(shared_secret_client, shared_secret_server));
}
}