Add java21-features module: runnable demos and captured output for the ankurm.com java21-features article
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
import javax.crypto.*;
|
||||
import java.security.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
/** JEP 452 (final): Key Encapsulation Mechanism API, with the DHKEM implementation that ships in 21. */
|
||||
public class KemDemo {
|
||||
public static void main(String[] args) throws Exception {
|
||||
KeyPairGenerator g = KeyPairGenerator.getInstance("X25519");
|
||||
KeyPair kp = g.generateKeyPair();
|
||||
KEM kem = KEM.getInstance("DHKEM");
|
||||
KEM.Encapsulator enc = kem.newEncapsulator(kp.getPublic());
|
||||
KEM.Encapsulated e = enc.encapsulate();
|
||||
SecretKey sender = e.key();
|
||||
SecretKey receiver = kem.newDecapsulator(kp.getPrivate()).decapsulate(e.encapsulation());
|
||||
System.out.println("algorithm : " + kem.getAlgorithm());
|
||||
System.out.println("secrets equal : " + Arrays.equals(sender.getEncoded(), receiver.getEncoded()));
|
||||
System.out.println("secret length : " + sender.getEncoded().length + " bytes");
|
||||
System.out.println("ciphertext length: " + e.encapsulation().length + " bytes");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user