Skip to main content

Export and Import Key

The Java_TEKey_ExportImportKey is used to import and export keys for offline use by clients.


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 com.eruces.teagent.TEAgentSSLMConnection;
import com.eruces.teagent.TEAgentException;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FileNotFoundException;
import com.eruces.teagent.TEKey;
import com.eruces.teagent.TEAgentConnection;
import java.util.Vector;
import com.eruces.teadmin.Administration;
import com.eruces.teagent.TEAgentConnectionFactory;
import com.eruces.teagent.PAny;
import com.eruces.teconfig.AdminConfig;
import com.eruces.teconfig.REParameterInfo;
import com.eruces.teconfig.TEParameterInfo;
import com.eruces.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", "password1A");
//connect_cert(m_strExportServer, m_nAdminPort, m_strCertKeyFile2, "password1A");
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", "password1A");
//connect_cert(m_strExportServer, m_nAdminPort, m_strCertKeyFile2, "password1A");
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", "password1A");
//connect_cert(m_strImportServer, m_nAdminPort, m_strCertKeyFile, "password1A");
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", "password1A");
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", "password1A");
//connect_cert(m_strImportServer, m_nAdminPort, m_strCertKeyFile, "password1A");
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", "password1A1A");
setTrustedServerOfflinePolicy(m_nExportSystemId, false);
disconnect();
}

/*
* Export a user's own key with
* TeKey.exportKey(String trustedServerName, String[] TTs, int type)
*/
public String testExportKey2(int type) 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", "password1A");
//connect_cert(m_strExportServer, m_nAdminPort, m_strCertKeyFile2, "password1A");

// Encrypt one data
String strData1 = "This is a test string";
byte[] szData = strData1.getBytes();
byte[][] szTT = new byte[1][];
szTT[0] = null;
byte[] szIV = m_strIV.getBytes();
TEAgentMatrix matrix = new TEAgentMatrix(); // Create TEAgentMatrix object
matrix.attachConnection(m_conn);
m_bytesEncrypted1 = matrix.encryptBuffer(m_conn, szData, szTT, szIV, true);
m_strTT1 = new String(szTT[0]);

String strData2 = "This is another test string";
szData = strData2.getBytes();
szTT[0] = null;
m_bytesEncrypted2 = matrix.encryptBuffer(m_conn, szData, szTT, szIV, true);
m_strTT2 = new String(szTT[0]);

System.out.println("Data was encrypted:");
System.out.println("m_strTT1 = " + m_strTT1);
System.out.println("m_strTT2 = " + m_strTT2);

// Prepare T-tags
String [] TTs = new String[2];
TTs[0] = m_strTT1;
TTs[1] = m_strTT2;

// Export keys
System.out.println();
System.out.println("Export keys:");
TEKey teKey = new TEKey();
connect_native_admin(m_strExportServer, m_nAdminPort, "ls", "password1A");
//connect_cert(m_strExportServer, m_nAdminPort, m_strCertKeyFile2, "password1A");
teKey.attachConnection(m_conn);
String strExportedKey = teKey.exportKey(m_strImportServer, TTs, type);
teKey.detachConnection();
disconnect();

System.out.println("Keys were exported:");
System.out.println("exportedTT1 = " + m_strTT1);
System.out.println("exportedTT2 = " + m_strTT2);
System.out.println("Exported key = " + strExportedKey);
return strExportedKey;
}
/**************************************************************************************
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

Encrypt something to generate keys:
Connected to xyangxpvm at 8888 as ls
Data was encrypted:
m_strTT1 = AAAHeLoZ5a8jE6vFHzyk5hZYlmny4tmHnK3FvcYK5ygsaZIKbivWzg==
m_strTT2 = AAAHeFDV/7gL2YhpnTNvy3uKAahnyi3oI83xULvSmVctULOge/ARJw==

Export keys:
Connected to xyangxpvm at 8888 as ls
Disconnected from server
Keys were exported:
exportedTT1 = AAAHeLoZ5a8jE6vFHzyk5hZYlmny4tmHnK3FvcYK5ygsaZIKbivWzg==
exportedTT2 = AAAHeFDV/7gL2YhpnTNvy3uKAahnyi3oI83xULvSmVctULOge/ARJw==
Exported key = <XMLKeyContainer><xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Type="http://www.w3.org/2001/04/xmlenc#Element">
<xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc"/>
<ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<xenc:EncryptedKey Recipient="2BRwBEWNEEY=" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5"/>
<xenc:CipherData>
<xenc:CipherValue>oiKFQO/O2VAte7iMx8NhiWvmbYr9uSJ4I4pOTZfl1IR1ts65+XBGZBZJiVv+tbQod/6CnoADm7Ql
Op4nJlGRsVa5Gb9tSVD4L/yOIr+4XvBQzVjezgxvFxazI/X9GI1ciHopkFnkoSJ6qWf+ERl9Fcqn
TTw9NEXSh5vTk4pZO8Q=</xenc:CipherValue>
</xenc:CipherData>
<xenc:CarriedKeyName>AAAHeLoZ5a8jE6vFHzyk5hZYlmny4tmHnK3FvcYK5ygsaZIKbivWzg==</xenc:CarriedKeyName>
</xenc:EncryptedKey>
</ds:KeyInfo>
<xenc:CipherData>
<xenc:CipherValue>KCPwYoGilUhpq0MUKsFJ6WOTpreUx6c2</xenc:CipherValue>
</xenc:CipherData>
</xenc:EncryptedData><xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Type="http://www.w3.org/2001/04/xmlenc#Element">
<xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc"/>
<ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<xenc:EncryptedKey Recipient="JOiyn6r827A=" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5"/>
<xenc:CipherData>
<xenc:CipherValue>YpjWlL5L70FE0G8Wglu2/ADGOOFdAya9UMSB390FpqhGC0988GoPQjpEU/APMmAD9+KXUxFsYYAu
3LiPwTEyE3E3Cx2+LlW40j1ONZk910T6w8IFk4HxDdtjTleUly3OoM2KA6DrXbp7igImlfU9Y9Bb
/uiUCI3c6UrplU7rwgo=</xenc:CipherValue>
</xenc:CipherData>
<xenc:CarriedKeyName>AAAHeFDV/7gL2YhpnTNvy3uKAahnyi3oI83xULvSmVctULOge/ARJw==</xenc:CarriedKeyName>
</xenc:EncryptedKey>
</ds:KeyInfo>
<xenc:CipherData>
<xenc:CipherValue>jvfp5RlQOZL93HjIt7tX4iefHpaVcwPM</xenc:CipherValue>
</xenc:CipherData>
</xenc:EncryptedData></XMLKeyContainer>

Import keys:
Connected to xyang1 at 8888 as ls
Disconnected from server
Keys were imported.

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:
Connected to xyang1 at 8888 as ls
Data was decrypted:
Decrypted data 1 = This is a test string
Decrypted data 2 = This is another test string

Unset trusted server offline policy:
Connected to xyang1 at 8888 as admin
Trusted server offline policy was unset.
Disconnected from server

**************************************************************************************/