Create Key in Cpp
The following code sample illustrates how to create key.
Code Sample
See the C++ environment settings for details on setting the project environment.
<cpp_create_key.cpp>
/*****************************************************************************************
* cpp_create_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;
void connect_native(string strServer, int nPort, 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, nPort, *ctx.operator->());
cout << "Connected to " << strServer << " at " << nPort << " as " << strUsername << endl;
}
void connect_native_admin(string strServer, int nPort, 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, nPort, *ctx.operator->());
cout << "Connected to " << strServer << " at " << nPort << " as " << strUsername << endl;
}
void connect_cert_PKCS12(string strServer, int nPort, 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, nPort, strCertChain, strPrivKey);
cout << "Connected to " << strServer << " at " << nPort << " as certificate user" << endl;
}
void disconnect()
{
te_conn->close();
cout << "Disconnected from server" << endl;
}
int main()
{
try
{
connect_native("localhost", 8888, "ls", "password");
//string strPKCS12_PKey_File = "C:\\Documents and Settings\\Administrator\\pkcs12_private_key.pfx"; // PKCS#12 private key file.
//connect_cert_PKCS12("localhost", 8888, strPKCS12_PKey_File, "password");
TEKey tekey;
tekey.attachConnection(te_conn.get());
string strHiddenLink;
string strACLTemplateName = "";
tekey.createKey(strHiddenLink, strACLTemplateName);
tekey.detachConnection();
cout << "Key was created" << endl;
cout << "HiddenLink = ";
int i;
for(i=0; i<strHiddenLink.size(); i++) {
cout << " [" << dec << (int)strHiddenLink[i] << "]";
}
cout << endl;
cout << "HiddenLink size = " << strHiddenLink.size() << endl;
disconnect();
}
catch (TEException &e)
{
cout << e.getAllMessages() << endl;
}
catch (...)
{
cout << "Unexpected error" << endl;
}
return 0;
}
Output
Connected to localhost at 8888 as ls
Key was created
HiddenLink = [0] [0] [0] [1] [89] [-93] [25] [8] [-99] [91] [42] [-93] [13] [87
] [-29] [-58] [108] [-127] [123] [-104] [-14] [-42] [-20] [121] [12] [-88] [107]
[-95] [57] [-6] [35] [78] [-13] [-58] [-80] [30] [124] [-46] [27] [124]
HiddenLink size = 40
Disconnected from server
Press any key to continue