Exemplo de encapsulamento e desencapsulamento de shared secret com ML-KEM-768 (FIPS-203).
package doxy.examples;
import com.dinamonetworks.Dinamo;
import com.dinamonetworks.KemEncapsResult;
import br.com.trueaccess.TacException;
import br.com.trueaccess.TacNDJavaLib;
public class KemEncapsDecaps {
public static void main(String[] args) throws TacException {
String ip = "127.0.0.1";
String user = "master";
String password = "12345678";
Dinamo api = new Dinamo();
api.openSession(ip, user, password);
String keyId = "ml_kem_768_key";
try {
api.createKey(keyId, TacNDJavaLib.ALG_ML_KEM_768, false);
byte[] pubKey = api.exportKey(keyId, TacNDJavaLib.PUBLICKEY_BLOB);
System.out.printf("Chave pública ML-KEM-768 (%d bytes)%n", pubKey.length);
KemEncapsResult encaps = api.kemEncaps(
TacNDJavaLib.DN_KEM_ENCAPS_OPT_ML_KEM,
TacNDJavaLib.DN_KEM_OP_SHARED_SECRET,
null, null, 0, 0,
pubKey);
byte[] ciphertext = encaps.getCiphertext();
byte[] sharedSecret = encaps.getSharedSecret();
System.out.printf("Ciphertext (%d bytes): %s%n", ciphertext.length, toHex(ciphertext));
System.out.printf("Shared secret encaps (%d bytes): %s%n", sharedSecret.length, toHex(sharedSecret));
byte[] decapsSecret = api.kemDecaps(
TacNDJavaLib.DN_KEM_DECAPS_OPT_ML_KEM,
TacNDJavaLib.DN_KEM_OP_SHARED_SECRET,
null, null, 0, 0,
keyId, ciphertext);
System.out.printf("Shared secret decaps (%d bytes): %s%n", decapsSecret.length, toHex(decapsSecret));
boolean match = java.util.Arrays.equals(sharedSecret, decapsSecret);
System.out.println("Shared secrets iguais: " + match);
} finally {
try { api.deleteKey(keyId); } catch (TacException ignore) {}
api.closeSession();
}
}
private static String toHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02X", b));
}
return sb.toString();
}
}