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

Ejemplo de generación de claves ECDH x9.63 en HSM.

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 GenEcdhKeyX963 {
private static String strAddr = "127.0.0.1";
private static String strUsrId = "maestro";
private static String strPwd = "12345678" ;
private static int nPort = 4433;
private static String strLocalKey = "test_local_key";
private static String strPeerKey = "test_peer_key";
private static String strTargetKey = "test_target_key";
private static String strSessionKey = "test_session_key";
public static void main(String[] args) {
int nFlags = 0;
Dinamo api = new Dinamo();
intentar {
api.openSession(strAddr, strUsrId, strPwd, nPort, nFlags);
api.deleteKeyIfExists(strLocalKey);
api.deleteKeyIfExists(strTargetKey);
api.deleteKeyIfExists(strSessionKey);
api.deleteKeyIfExists(strPeerKey);
System.out.println("--> ¡Generar claves ECDH!");
api.createKey(strLocalKey, TacNDJavaLib.ALG_ECC_BRAINPOOL_P512T1);
api.createKey(strPeerKey, TacNDJavaLib.ALG_ECC_BRAINPOOL_P512T1);
System.out.println("--> ¡Exporta la clave pública de la clave par!");
byte[] pbPeerPubKey = api.exportKey(strPeerKey, TacNDJavaLib.PUBLICKEY_BLOB);
byte[] pbKDFData = {(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,
(byte)0x11,(byte)0x12,(byte)0x13,(byte)0x14,(byte)0x15,(byte)0x16,(byte)0x17,(byte)0x18};
/*
* Primera forma de generar la clave.
*
* Genera la clave derivada y la almacena en el HSM.
* */
System.out.println("--> ¡Generar secreto compartido!");
api.genEcdhKeyX963Sha256(strLocalKey,
strTargetKey,
TacNDJavaLib.ALG_AES_256,
cierto,
falso,
pbPeerPubKey,
pbKDFData);
/*
* Segunda forma de generar la clave.
*
* Genera la clave derivada dentro del HSM y la devuelve a quien la solicita.
* Como los parámetros son los mismos, las claves generadas son las mismas.
* */
System.out.println("--> ¡Generar secreto compartido! (2ª opción)");
byte[] pbKey = api.genEcdhKeyX963Sha256(strLocalKey,
nulo,
TacNDJavaLib.ALG_AES_256,
falso,
falso,
pbPeerPubKey,
pbKDFData);
byte[] pbClearBuffer = "askdfkasdfaksdfa".getBytes();
/*
* Importar la clave con el contenido devuelto.
* */
System.out.println("--> ¡Importar secreto compartido!");
api.importKey( strSessionKey,
TacNDJavaLib.PLAINTEXTKEY_BLOB,
TacNDJavaLib.ALG_AES_256,
TacNDJavaLib.EXPORTABLE_KEY,
pbKey,
TacNDJavaLib.ALG_AES_256_LEN);
System.out.println("--> ¡Encriptar y desencriptar búfer!");
byte[] pbEncryptedBuffer = api.encrypt(strSessionKey, pbClearBuffer);
byte[] pbDecryptedBuffer = api.decrypt(strTargetKey, pbEncryptedBuffer);
if(!Arrays.equals(pbClearBuffer, pbDecryptedBuffer))
{
System.out.println("¡El búfer descifrado y el búfer de texto claro son diferentes!");
}
System.out.println("--> ¡Borrar teclas!");
api.deleteKey(strLocalKey);
api.deleteKey(strPeerKey);
api.deleteKey(strTargetKey);
api.deleteKey(strSessionKey);
api.closeSession();
} catch (TacException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}