API Java
HSM Dinamo
Cargando...
Buscando...
No se han encontrado entradas
GenSessionKeyFromEcdhKey.java

Ejemplo de generación de una clave de sesión a partir de claves elípticas (ECDH).

Véase la nota sobre ejemplos.
paquete doxy.examples;
import java.util.Arrays;
import com.dinamonetworks.Dinamo;
import br.com.trueaccess.TacException;
import br.com.trueaccess.TacNDJavaLib;
public class GenSessionKeyFromEcdhKey {
private static String strAddr = "127.0.0.1";
private static String strUsrId = "maestro";
private static String strPwd = "12345678" ;
private static String strLocalKey = "test_local_key";
private static String strPeerKey = "test_peer_key";
private static String strSessionKey = "test_session_key";
public static void main(String[] args) {
Dinamo api = new Dinamo();
intentar {
System.out.println("--> Iniciar sesión en HSM y crear 2 claves Brainpool");
api.openSession(strAddr, strUsrId, strPwd);
api.createKey(strLocalKey, TacNDJavaLib.ALG_ECC_BRAINPOOL_P512T1);
api.createKey(strPeerKey, TacNDJavaLib.ALG_ECC_BRAINPOOL_P512T1);
System.out.println("--> Extraer la parte pública de la clave local");
byte[] pbPeerPubKey = api.exportKey(strPeerKey, TacNDJavaLib.PUBLICKEY_BLOB);
System.out.println("--> Construir una clave de sesión");
byte[] pbSharedSecret = api.genEcdhKey( TacNDJavaLib.DN_GEN_KEY_KDF_RAW_SECRET,
strLocalKey,
pbPeerPubKey );
/*
* ¡¡¡El secreto compartido se utiliza directamente en este ejemplo, sólo con fines educativos!!!
* Recomendamos encarecidamente utilizar KDF (Key Derivation Function) antes de importar la clave.
* */
api.importKey( strSessionKey,
TacNDJavaLib.PLAINTEXTKEY_BLOB,
TacNDJavaLib.ALG_AES_256,
TacNDJavaLib.EXPORTABLE_KEY,
pbSharedSecret,
TacNDJavaLib.ALG_AES_256_LEN);
System.out.println("--> Cifrar datos con búfer de prueba");
byte[] pbClearBuffer = "askdfkasdfaksdfa".getBytes();
byte[] pbEncryptedBuffer = api.encrypt(strSessionKey, pbClearBuffer);
byte[] pbDecryptedBuffer = api.decrypt(strSessionKey, pbEncryptedBuffer);
System.out.println("--> Operación de prueba");
if(!Arrays.equals(pbClearBuffer, pbDecryptedBuffer))
{
System.out.println("¡El búfer descifrado y el búfer de texto claro son diferentes!");
}
System.out.println("--> Eliminar teclas");
api.deleteKey(strLocalKey);
api.deleteKey(strPeerKey);
api.deleteKey(strSessionKey);
api.closeSession();
System.out.println("El proceso finalizó correctamente");
} catch (TacException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}