DINAMO Networks is now aDINAMO cyber security company!
Java API
HSM DINAMO
Loading...
Searching...
No entry found
ExportImportKeyParts.java

Example of exporting and importing keys by parts.

See Note on examples.
package doxy.examples;
import com.dinamonetworks.Dinamo;
import com.dinamonetworks.EftKeyParts;
import br.com.trueaccess.TacException;
import br.com.trueaccess.TacNDJavaLib;
public class ExportImportKeyParts {
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 keyName = "3des112";
// Cria a chave 3DES 112 bits
// A chave é criada como exportável, o que permite a exportação de suas partes
api.createKey(keyName, TacNDJavaLib.ALG_3DES_112, true);
System.out.println("Chave 3DES 112 bits criada: " + keyName);
EftKeyParts parts = api.exportKeyParts(keyName, 0);
System.out.println("Partes da chave 3DES 112 bits exportadas: " + keyName);
String importedKeyName = "3des112_imp";
// Importa as partes da chave 3DES 112 bits
api.importKeyParts(importedKeyName, TacNDJavaLib.ALG_3DES_112, parts, 0);
System.out.println("Partes da chave 3DES 112 bits importadas: " + importedKeyName);
// Verifica se a chave importada funciona corretamente
byte[] clearText = "Texto claro para teste".getBytes();
byte[] encryptedText = api.encrypt(importedKeyName, clearText);
byte[] decryptedText = api.decrypt(keyName, encryptedText);
if (new String(clearText).equals(new String(decryptedText))) {
System.out.println("Chave importada funciona corretamente!");
} else {
System.out.println("Erro: a chave importada não funciona corretamente.");
}
api.deleteKeyIfExists(importedKeyName);
api.deleteKeyIfExists(keyName);
System.out.println("Chaves deletadas: " + keyName + ", " + importedKeyName);
api.closeSession();
}
}