Expire Key in Cpp
The following code sample illustrates how to expire a key so it cannot ever be used again.
Code Sample
See the C++ environment settings for details on setting the project environment.
<cpp_create_key.cpp>
/*****************************************************************************************
* cpp_expire_key.cpp
*
*
******************************************************************************************/
#include <teconnection.h>
#include <teexception.h>
#include <tekey.h>
#include <iostream>
int main (int argc, char* argv[])
{
if (argc < 3) {
_TESTD cerr << "usage: " << argv[0] << " username password [server [port]]" << _TESTD endl;
return 1;
}
_TESTD string username = argv[1];
_TESTD string password = argv[2];
_TESTD string host = "localhost";
int port = 8888;
if (argc > 3) {
host = argv[3];
if (argc > 4) {
port = atoi (argv[4]);
}
}
try {
_TESTD auto_ptr<ERUCES::TEConnection> conn =
ERUCES::TEConnection::createInstance (ERUCES::TEConnection::SecureConnection);
conn->open (host, port, username, password);
ERUCES::TEKey agent;
agent.attachConnection (conn.get());
_TESTD string data = "Test Data";
_TESTD string hl, iv;
agent.encryptData (data, hl, iv);
_TESTD string encrypted = data;
agent.decryptData (data, hl, iv);
_TESTD cout << "decrypted: " << data << _TESTD endl;
_TESTD string* hlptr = &hl;
agent.expireKey (&hlptr, 1, false);
data = encrypted;
agent.decryptData (data, hl, iv);
_TESTD cout << "decrypted with expired key: " << data << _TESTD endl;
agent.expireKey (&hlptr, 1, true);
try {
data = encrypted;
agent.decryptData (data, hl, iv);
_TESTD cout << "Failed, should not decrypt!" << _TESTD endl;
return 1;
} catch (...) {
_TESTD cout << "Passed, key removed!" << _TESTD endl;
}
agent.detachConnection ();
_TESTD cout << "Done!" << _TESTD endl;
return 0;
} catch (ERUCES::TEException& ex) {
_TESTD cout << "Failed!" << _TESTD endl;
_TESTD cerr << ex.getAllMessages() << _TESTD endl;
return 1;
}
}