Add java21-features module: runnable demos and captured output for the ankurm.com java21-features article

This commit is contained in:
2026-10-20 17:13:59 +00:00
parent 5a076c826f
commit c3cc57f482
33 changed files with 354 additions and 0 deletions
+20
View File
@@ -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");
}
}