Export and Import Key
The Java_TEKey_ExportImportKey is used to import and export keys for offline use by clients.
TEKey.importKey(Vector<TEKey.KeyPair> keys, int type) takes TEKey.KEY_FORMAT_NATIVE -- key blobs
exported by a key server this one has an established trust with, which needs the Import Key
operation (the Exchanger role) -- or one of the two formats for keys you already hold:
TEKey.KEY_FORMAT_WRAPPED, a key wrapped to the key server's RSA trust certificate (build it with
TEKey.wrappedKeyBlob), or TEKey.KEY_FORMAT_RAW, a key in the clear (TEKey.rawKeyBlob). For
those two, leave each pair's t-Tag empty and the key server mints one and writes it back into the
pair; see Importing your own keys for the formats and how to
wrap. It returns one result per key pair, in order: TEKey.RESULT_IMPORTED (the caller owns the
imported key), TEKey.RESULT_DUPLICATE (this key server already holds a key under that t-Tag,
expired or not, and it was not overwritten), or the key server's error code for a pair it refused.
Each pair is applied on its own.
A key removed from a key server (TEKey.removeKey) is gone from that key server only: a copy
exported earlier is not reached, and importing it again restores the key. A key expired there
(TEKey.expireKey) keeps its record, so importing a copy of it answers RESULT_DUPLICATE and the
key stays expired. The JCE provider's KeyStore.deleteEntry is a local keystore operation: it does
not delete or expire the key on the key server.
Code Samples
See the Java environment settings for details on setting the project environment.
Java_TEKey_ExportImportKey
The following example demonstrates the use of the Java_TEKey_ExportImportKey.
/*****************************************************************************************
*
* Java_TEKey_ExportImportKey.java
*
******************************************************************************************/
import tech.pisoft.teagent.TEAgentSSLMConnection;
import tech.pisoft.teagent.TEAgentException;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FileNotFoundException;
import tech.pisoft.teagent.TEKey;
import tech.pisoft.teagent.TEAgentConnection;
import java.util.Vector;
import tech.pisoft.teadmin.Administration;
import tech.pisoft.teagent.TEAgentConnectionFactory;
import tech.pisoft.teagent.PAny;
import tech.pisoft.teconfig.AdminConfig;
import tech.pisoft.teconfig.REParameterInfo;
import tech.pisoft.teconfig.TEParameterInfo;
import tech.pisoft.teagent.TEAgentMatrix;
public class Java_TEKey_ExportImportKey {
private TEAgentConnection m_conn;
private Vector<TEKey.KeyPair> m_keyPairs;
private String m_strEncrypted1;
private String m_strEncrypted2;
private byte[] m_bytesEncrypted1;
private byte[] m_bytesEncrypted2;
private String m_strTT1;
private String m_strTT2;
private String m_strIV = "12345678";
private String m_strExportServer = "xyangxpvm";
private String m_strImportServer = "xyang1";
private int m_nExportSystemId = 1912;
private int m_nPort = 8888;
private int m_nAdminPort = 8888;
private String m_strCertKeyFile = "C:\\Documents and Settings\\Administrator\\pkcs12_private_key.pfx";
private String m_strCertKeyFile2 = "C:\\Documents and Settings\\Administrator\\pkcs12_private_key2.pfx";
public void connect_native(String strServerName, int nPort, String strUsername, String strPasswd) throws TEAgentException
{
// Get a SSL connection instance
m_conn = TEAgentConnectionFactory.getInstance(TEAgentConnectionFactory.SECURE_USER_PASSWORD);
// Make connection
m_conn.open(strServerName, nPort, strUsername, strPasswd);
System.out.println("Connected to " + strServerName + " at " + nPort + " as " + strUsername);
}
public void connect_native_admin(String strServerName, int nPort, String strUsername, String strPasswd) throws TEAgentException
{
m_conn = TEAgentConnectionFactory.getInstance(TEAgentConnectionFactory.SECURE_USER_PASSWORD);
// Connect to server.
m_conn.open(strServerName, nPort, strUsername, strPasswd);
System.out.println("Connected to " + strServerName + " at " + nPort + " as " + strUsername);
}
public void connect_cert(String strServerName, int nPort, String strCertFile, String strPasswd) throws Exception
{
try {
// Get a connection for native user
TEAgentSSLMConnection sslmConn = new TEAgentSSLMConnection();
// Read pfx file
InputStream pfx = new FileInputStream(strCertFile);
// Make connection
sslmConn.open(strServerName, nPort, pfx, strPasswd);
m_conn = sslmConn;
System.out.println("Connected to " + strServerName + " at " + nPort + " as certificate user.");
}
catch(FileNotFoundException fnfex) {
throw new Exception(fnfex.getMessage());
}
}
public void disconnect() throws TEAgentException
{
// Close connection
m_conn.close();
System.out.println("Disconnected from server ");
}
/*
Use the TeKey.exportKey(String m_strImportServer, Vector keyPairs)
method to export a user's own key.
*/
public void testExportKey1() throws TEAgentException, Exception {
// Encrypt something to generate keys
System.out.println();
System.out.println("Encrypt something to generate keys:");
connect_native(m_strExportServer, m_nPort, "ls", "ChangeMe-AdminPassword");
//connect_cert(m_strExportServer, m_nAdminPort, m_strCertKeyFile2, "ChangeMe-AdminPassword");
StringBuffer strbufTT1 = new StringBuffer();
StringBuffer strbufTT2 = new StringBuffer();
m_strEncrypted1 = Java_HelperFunctions.encryptSingle(m_conn, "Dummy data 1", strbufTT1);
m_strEncrypted2 = Java_HelperFunctions.encryptSingle(m_conn, "Dummy data 2", strbufTT2);
disconnect();
m_strTT1 = strbufTT1.toString();
m_strTT2 = strbufTT2.toString();
System.out.println("Keys were generated:");
System.out.println("m_strTT1 = " + m_strTT1);
System.out.println("m_strTT2 = " + m_strTT2);
// Prepare container
TEKey.KeyPair keyPair1 = new TEKey.KeyPair();
TEKey.KeyPair keyPair2 = new TEKey.KeyPair();
keyPair1.setHiddenLink(m_strTT1);
keyPair2.setHiddenLink(m_strTT2);
m_keyPairs = new Vector<TEKey.KeyPair>();
m_keyPairs.add(keyPair1);
m_keyPairs.add(keyPair2);
// Export keys
System.out.println();
System.out.println("Export keys:");
TEKey teKey = new TEKey();
connect_native_admin(m_strExportServer, m_nAdminPort, "ls", "ChangeMe-AdminPassword");
//connect_cert(m_strExportServer, m_nAdminPort, m_strCertKeyFile2, "ChangeMe-AdminPassword");
teKey.attachConnection(m_conn);
teKey.exportKey(m_strImportServer, m_keyPairs);
teKey.detachConnection();
disconnect();
String exportedTT1 = keyPair1.getHiddenLink();
String exportedKey1 = keyPair1.getKey();
String exportedTT2 = keyPair2.getHiddenLink();
String exportedKey2 = keyPair2.getKey();
System.out.println("Keys were exported:");
System.out.println("exportedTT1 = " + exportedTT1);
System.out.println("exportedKey1 = " + exportedKey1);
System.out.println("exportedTT2 = " + exportedTT2);
System.out.println("exportedKey2 = " + exportedKey2);
}
/*
Use the TeKey.importKey(Vector keyPairs;)
method to import a user's own key.
*/
public void testImportKey1() throws TEAgentException, Exception {
// Import key
System.out.println();
System.out.println("Import keys:");
TEKey teKey = new TEKey();
connect_native_admin(m_strImportServer, m_nAdminPort, "ls", "ChangeMe-AdminPassword");
//connect_cert(m_strImportServer, m_nAdminPort, m_strCertKeyFile, "ChangeMe-AdminPassword");
teKey.attachConnection(m_conn);
teKey.importKey(m_keyPairs);
teKey.detachConnection();
disconnect();
TEKey.KeyPair keyPair1 = (TEKey.KeyPair) m_keyPairs.get(0);
TEKey.KeyPair keyPair2 = (TEKey.KeyPair) m_keyPairs.get(1);
String exportedTT1 = keyPair1.getHiddenLink();
String exportedKey1 = keyPair1.getKey();
String exportedTT2 = keyPair2.getHiddenLink();
String exportedKey2 = new String(keyPair2.getKey());
System.out.println("Keys were imported:");
System.out.println("exportedTT1 = " + exportedTT1);
System.out.println("exportedKey1 = " + exportedKey1);
System.out.println("exportedTT2 = " + exportedTT2);
System.out.println("exportedKey2 = " + exportedKey2);
// Set trusted server offline policy
System.out.println();
System.out.println("Set trusted server offline policy:");
connect_native_admin(m_strImportServer, m_nAdminPort, "admin", "ChangeMe-AdminPassword");
setTrustedServerOfflinePolicy(m_nExportSystemId, true);
disconnect();
// Decrypt data at importing server
System.out.println();
System.out.println("Decrypt data at importing server:");
System.out.println("m_strEncrypted1 = " + m_strEncrypted1);
System.out.println("m_strEncrypted2 = " + m_strEncrypted2);
connect_native(m_strImportServer, m_nPort, "ls", "ChangeMe-AdminPassword");
//connect_cert(m_strImportServer, m_nAdminPort, m_strCertKeyFile, "ChangeMe-AdminPassword");
String strDecrypted1 = Java_HelperFunctions.decryptSingle(m_conn, m_strEncrypted1, exportedTT1);
String strDecrypted2 = Java_HelperFunctions.decryptSingle(m_conn, m_strEncrypted2, exportedTT2);
disconnect();
System.out.println("Decrypted data:");
System.out.println("strDecrypted1 = " + strDecrypted1);
System.out.println("strDecrypted2 = " + strDecrypted2);
// Unset trusted server offline policy
System.out.println();
System.out.println("Unset trusted server offline policy:");
connect_native_admin(m_strImportServer, m_nAdminPort, "admin", "ChangeMe-AdminPassword1A");
setTrustedServerOfflinePolicy(m_nExportSystemId, false);
disconnect();
}
/**************************************************************************************
Encrypt something to generate keys:
Connected to xyangxpvm at 8888 as ls
Disconnected from server
Keys were generated:
m_strTT1 = AAAHePwiIaaGsFNg4nP6cQUIiDa8fQcyQX35n8dSZwb+792Tp81Qtg==
m_strTT2 = AAAHeCgKA6ogwvyeGoOa+GnX1VwpyHxdqK1ZR8SiMa3FEOLip4KXwA==
Export keys:
Connected to xyangxpvm at 8888 as ls
Disconnected from server
Keys were exported:
exportedTT1 = AAAHePwiIaaGsFNg4nP6cQUIiDa8fQcyQX35n8dSZwb+792Tp81Qtg==
exportedKey1 = TEDN1l5T4LlixMmrFLZvNZs5CpZTpNw8rzsh9i1pl3IrKZzmzW7a1FGJ+3fSedNXWIH5ff9rXzOJ3z8hprKbAln0WjVJFVDeMVWPlue8QwawX4zLUYR+IJ2NCdekkwsos1rfqk+j8PDZIgMu8fWfaCbcPlsQbLZ5YQvgsELbsSI=
exportedTT2 = AAAHeCgKA6ogwvyeGoOa+GnX1VwpyHxdqK1ZR8SiMa3FEOLip4KXwA==
exportedKey2 = X9vgTf2uV4zWU9Be22yjZEzEXrSBF1F6p3kwR1mLZstGtBLT175m/D+CcdkK6HYcPTq/cjdRmL8KSIFiMtddD9D1LhoWyuTXmZ3Cq7jx/AAo6WDIyJzTvOZifSXl1uhW6y78vTk+ifzCPkFlYtawgzugDjhLTrR/uksVIh1vQh0=
Import keys:
Connected to xyang1 at 8888 as ls
Disconnected from server
Keys were imported:
exportedTT1 = AAAHePwiIaaGsFNg4nP6cQUIiDa8fQcyQX35n8dSZwb+792Tp81Qtg==
exportedKey1 = TEDN1l5T4LlixMmrFLZvNZs5CpZTpNw8rzsh9i1pl3IrKZzmzW7a1FGJ+3fSedNXWIH5ff9rXzOJ3z8hprKbAln0WjVJFVDeMVWPlue8QwawX4zLUYR+IJ2NCdekkwsos1rfqk+j8PDZIgMu8fWfaCbcPlsQbLZ5YQvgsELbsSI=
exportedTT2 = AAAHeCgKA6ogwvyeGoOa+GnX1VwpyHxdqK1ZR8SiMa3FEOLip4KXwA==
exportedKey2 = X9vgTf2uV4zWU9Be22yjZEzEXrSBF1F6p3kwR1mLZstGtBLT175m/D+CcdkK6HYcPTq/cjdRmL8KSIFiMtddD9D1LhoWyuTXmZ3Cq7jx/AAo6WDIyJzTvOZifSXl1uhW6y78vTk+ifzCPkFlYtawgzugDjhLTrR/uksVIh1vQh0=
Set trusted server offline policy:
Connected to xyang1 at 8888 as admin
Trusted server offline policy was set.
Disconnected from server
Decrypt data at importing server:
m_strEncrypted1 = WldxdGulSAaC28yT311PvA==
m_strEncrypted2 = PD/W2XBHTbeOm34ZTf0YJg==
Connected to xyang1 at 8888 as ls
Disconnected from server
Decrypted data:
strDecrypted1 = Dummy data 1
strDecrypted2 = Dummy data 2
Unset trusted server offline policy:
Connected to xyang1 at 8888 as admin
Trusted server offline policy was unset.
Disconnected from server
**************************************************************************************/