Skip to main content

Export and Import Key in Cpp

The following code sample illustrates how to export and import key.


Code Sample

See the C++ environment settings for details on setting the project environment.


<cpp_export_import_key.cpp>
/*****************************************************************************************
* cpp_export_import_key.cpp
*
*
******************************************************************************************/

#include <iostream>
#include "teconnection.h"
#include "teagent.h"
#include "teexception.h"
#include "teauthldapctx.h"
#include "teutil_keycert.h"
#include "teauthuserctx.h"
#include "teadmin.h"
#include "teparameter.h"
#include "teadmin_config.h"
#include "tekey.h"

using namespace ERUCES;
using namespace std;

auto_ptr<TEConnection> te_conn;
string strIV = "";

void connect_native(string strServer, int nAdminPort, string strUsername, string strPassword)
{
te_conn = TEConnection::createInstance(TEConnection::NormalConnection);

auto_ptr<TEUserAuthenticationContext> ctx = TEUserAuthenticationContext::getInstance();
ctx->setUser(strUsername);
ctx->setPasswd(strPassword);

te_conn->open(strServer, nAdminPort, *ctx.operator->());
cout << "Connected to " << strServer << " at " << nAdminPort << " as " << strUsername << endl;
}

void connect_native_admin(string strServer, int nAdminPort, string strUsername, string strPassword)
{
te_conn = TEConnection::createInstance(TEConnection::SecureConnection);

auto_ptr<TEUserAuthenticationContext> ctx = TEUserAuthenticationContext::getInstance();
ctx->setUser(strUsername);
ctx->setPasswd(strPassword);

te_conn->open(strServer, nAdminPort, *ctx.operator->());
cout << "Connected to " << strServer << " at " << nAdminPort << " as " << strUsername << endl;
}

void connect_cert_PKCS12(string strServer, int nAdminPort, string strPKCS12_PKey_File, string strPassword)
{
te_conn = TEConnection::createInstance(TEConnection::SSLMutualConnection); // SSLMutualConnection = 0x00000004

// Read Certificate and Private Key from PKCS#12 private key file.
string strCertChain = "";
string strPrivKey = "";
TEKeyCertUtil::loadFromFile(strPKCS12_PKey_File, strPassword, strCertChain, strPrivKey, false);

// Open connection to the server for certificate user
te_conn->open(strServer, nAdminPort, strCertChain, strPrivKey);

cout << "Connected to " << strServer << " at " << nAdminPort << " as certificate user" << endl;
}

void disconnect()
{
te_conn->close();
cout << "Disconnected from server" << endl;
}

void setTrustedServerOfflinePolicy(int nTrustedServerSystemId, bool isOffline) {
TEParameter param;
char buffer[40];
sprintf(buffer, "TrustedServer.%d.offline", nTrustedServerSystemId);

TEAdminConfig admin;
admin.attachConnection(te_conn.get());
string strKey = buffer;
string strValue;
isOffline ? strValue = "YES" : strValue = "NO";
param.setKey(strKey);
param.setValue(strValue);
admin.setParameter(param);

admin.detachConnection();

if (isOffline)
cout << "Trusted server offline policy was set." << endl;
else
cout << "Trusted server offline policy was unset." << endl;

}

void EncryptData(string& strData, string& strHiddenLink) {

TEAgent teagent;
teagent.attachConnection(te_conn.get());

cout << "Original data = " << strData.c_str() << endl;
cout << "Original data size = " << strData.size() << endl;

teagent.encryptData(strData, strHiddenLink, strIV);
teagent.detachConnection();

cout << "Encrypted data = ";
int i;
for(i=0; i<strData.size(); i++) {
cout << "[" << (int)strData[i] << "]";
}
cout << endl;
cout << "Encrypted size = " << strData.size() << endl;

cout << "HiddenLink = ";
for(i=0; i<strHiddenLink.size(); i++) {
cout << "[" << (int)strHiddenLink[i] << "]";
}
cout << endl;
cout << "HiddenLink size = " << strHiddenLink.size() << endl;
}

void DecryptData(string& strData, string& strHiddenLink) {

TEAgent teagent;
teagent.attachConnection(te_conn.get());
teagent.detachConnection();

teagent.decryptData(strData, strHiddenLink, strIV);
cout << "Decrypted data = " << strData.c_str() << endl;
cout << "Decrypted size = " << strData.size() << endl;
}

void ExportKeyWithServerName(string& strHiddenLink, string& strExportedKey, string strTrustedServerName, unsigned int type) {

// Attach Connection to the Key class.
TEKey tekey;
tekey.attachConnection(te_conn.get());

if(type == 0) {
KeyPair strPair = make_pair<string *, string *>(&strHiddenLink, &strExportedKey);
KeyPair pairArray[1];
pairArray[0] = strPair;
tekey.exportKey(strTrustedServerName, pairArray, sizeof(pairArray)/sizeof(KeyPair));
}
else {
string * hlArray[1];
hlArray[0] = &strHiddenLink;

tekey.exportKeyFormatted(strTrustedServerName, hlArray, 1, strExportedKey, type);
}

tekey.detachConnection();
}

void ImportKey(string& strHiddenLink, string& strExportedKey, unsigned int type) {

// Attach Connection to the Key class. You can later on detach connection or disconnect it
TEKey tekey;
tekey.attachConnection(te_conn.get());

if(type == 0) {
KeyPair strPair = make_pair<string *, string *>(&strHiddenLink, &strExportedKey);
KeyPair pairArray[1];
pairArray[0] = strPair;
tekey.importKey(pairArray, sizeof(pairArray)/sizeof(KeyPair));
}
else {
tekey.importKeyFormatted(strExportedKey, type);
}

tekey.detachConnection();
}

int main()
{

string strExportServer = "xyangxpvm";
string strImportServer = "xyang1";
int nExportSystemId = 1912;
int nAdminPort = 8888;

string strPKCS12_PKey_File2 = "C:\\Documents and Settings\\Administrator\\pkcs12_private_key2.pfx"; // PKCS#12 private key file.
string strPKCS12_PKey_File = "C:\\Documents and Settings\\Administrator\\pkcs12_private_key.pfx"; // PKCS#12 private key file.

unsigned int type = 0; // 0 - NORMAL, can export and import keys.
// 1 - SMIME, can only export keys.
// 2 - XML, can export and import keys.

string strData = "this is a test string.";
string strHiddenLink;
string strExportedKey;

try
{
// Encrypt data
cout << endl << "Encrypt data at export server:" << endl;
connect_native(strExportServer, nAdminPort, "ls", "password");
//connect_cert_PKCS12(strExportServer, nAdminPort, strPKCS12_PKey_File2, "password");
EncryptData(strData, strHiddenLink);
disconnect();


// Export key
cout << endl << "Export key:" << endl;
connect_native_admin(strExportServer, nAdminPort, "ls", "password");
//connect_cert_PKCS12(strExportServer, nAdminPort, strPKCS12_PKey_File2, "password");
ExportKeyWithServerName(strHiddenLink, strExportedKey, strImportServer, type);
cout << "export key done." << endl;
cout << "HiddenLink = ";
int i;
for(i=0; i<strHiddenLink.size(); i++) {
cout << "[" << (int)strHiddenLink[i] << "]";
}
cout << endl;
cout << "HiddenLink size = " << strHiddenLink.size() << endl;
cout << "Exported key = ";
for(i=0; i<strExportedKey.size(); i++) {
cout << "[" << (int)strExportedKey[i] << "]";
}
cout << endl;
cout << "Exported key size = " << strExportedKey.size() << endl;
disconnect();

if (type != 1)
{
// Set trusted server offline policy
cout << endl << "Set trusted server offline policy:" << endl;
connect_native_admin(strImportServer, nAdminPort, "admin", "password1A");
setTrustedServerOfflinePolicy(nExportSystemId, true);
disconnect();

// Import key
cout << endl << "Import key:" << endl;
connect_native_admin(strImportServer, nAdminPort, "ls", "password");
//connect_cert_PKCS12(strImportServer, nAdminPort, strPKCS12_PKey_File, "password");
ImportKey(strHiddenLink, strExportedKey, type);
cout << "import key done." << endl;
disconnect();

// Decrypt data
cout << endl << "Decrypt data at import server:" << endl;
connect_native(strImportServer, nPort, "ls", "password");
//connect_cert_PKCS12(strImportServer, nAdminPort, strPKCS12_PKey_File, "password");
DecryptData(strData, strHiddenLink);
disconnect();

// Unset trusted server offline policy
cout << endl << "Unset trusted server offline policy:" << endl;
connect_native_admin(strImportServer, nAdminPort, "admin", "password1A");
setTrustedServerOfflinePolicy(nExportSystemId, false);
disconnect();
}

}
catch (TEException &e)
{
cout << e.getAllMessages() << endl;
}
catch (...)
{
cout << "Unexpected error" << endl;
}

return 0;
}
Output Encrypt data at export server:
Connected to xyangxpvm at 8888 as ls Original data = this is a test string. Original data size = 22

Encrypted data = [-56][-105][122][79][3][-96][-58][38][-102][-120][-120][-23][-1 16][112][-61][-52][47][-87][-15][3][-64][10][-52][93]
Encrypted size = 24
HiddenLink = [0][0][7][120][-36][-97][-106][8][-119][45][-48][50][12][36][-20][- 12][-85][122][89][16][-13][-115][28][81][81][-54][-59][-32][-13][53][-126][92][1 9][-49][74][21][-62][23][-13][-10]
HiddenLink size = 40
Disconnected from server

Export key: Connected to xyangxpvm at 8888 as ls export key done.
HiddenLink = [0][0][7][120][-36][-97][-106][8][-119][45][-48][50][12][36][-20][- 12][-85][122][89][16][-13][-115][28][81][81][-54][-59][-32][-13][53][-126][92][1 9][-49][74][21][-62][23][-13][-10]
HiddenLink size = 40
Exported key = [-98][68][-19][-86][19][127][-71][102][-52][-39][19][-84][-87][90 ][100][58][-125][66][95][42][85][-108][11][113][-23][-112][37][-3][10][-93][-67] [-69][107][-29][79][95][-26][3][-9][37][-80][98][-126][26][-107][22][70][29][32] [-8][-42][46][-93][-127][30][-98][-46][36][-27][-32][31][14][14][-111][22][-91][ -23][78][72][17][-96][75][35][18][116][-40][-122][-113][-13][-28][48][112][-62][ -125][44][25][12][-64][-12][-49][-60][-91][-37][125][55][-95][-89][61][49][34][- 60][-101][-46][-93][-110][-46][-8][-2][107][79][-15][-38][-26][110][-113][-93][- 114][-11][-25][-53][94][1][2][-56][85][-109][73][-68]
Exported key size = 128
Disconnected from server
Set trusted server offline policy: Connected to xyang1 at 8888 as admin Trusted server offline policy was set.
Disconnected from server Import key: Connected to xyang1 at 8888 as ls import key done.
Disconnected from server Decrypt data at import server: Connected to xyang1 at 8888 as ls Decrypted data = this is a test string. Decrypted size = 22
Disconnected from server Unset trusted server offline policy: Connected to xyang1 at 8888 as admin Trusted server offline policy was unset.
Disconnected from server Press any key to continue

<cpp_export_import_key.cpp>