Skip to main content

Encryption with Matrix

The following example demonstrates how to encrypt and decrypt customer data with TEAgentMatrix.


Code Sample

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


<cpp_matrix_encrypt.cpp>
/*****************************************************************************************
* c++_matrix_encrypt.cpp
*
*
* Use TEAgentMatrix for data encryption/decryption.
*
******************************************************************************************/

#include <iostream>
#include "teconnection.h"
#include "teagent.h"
#include "teexception.h"

using namespace ERUCES;
using namespace std;

int main()
{
string strServerName = "localhost"; // server name
long lPort = 8888; // port number
string strUsername = "alice"; // username
string strPassword = "alice1A"; // password

try
{
auto_ptr<TEConnection> conn =
TEConnection::createInstance(TEConnection::NormalConnection);

// Open connection to the server
conn->open(strServerName, lPort, strUsername, strPassword);

cout << "Connected to " << strServerName << " at " << lPort
<< " as " << strUsername << "\n" << endl;

// Create TEAgentMatrix object
TEAgentMatrix matrix;

// Original data for encryption
string str1_1 = "test string (1, 1)";
string str1_2 = "test string (1, 2)";
string str2_1 = "test string (2, 1)";
string str2_2 = "test string (2, 2)";
string str2_3 = "test string (2, 3)";
string str3_1 = "test string (3, 1)";

// Set matrix for encryption
matrix.setElement(1, 1, str1_1); // set element (1, 1)
matrix.setElement(1, 2, str1_2);
matrix.setElement(2, 1, str2_1); // set element (2, 1)
matrix.setElement(2, 2, str2_2);
matrix.setElement(2, 3, str2_3);
matrix.setElement(3, 1, str3_1); // set element (3, 1)

cout << "Before encryption:\n";
cout << "Original data:\n";
cout << str1_1 << ", " << str1_2 << endl;
cout << str2_1 << ", " << str2_2 << ", " << str2_3 << endl;
cout << str3_1 << endl;

// Encrypt matrix
matrix.attachConnection(conn.operator->()); // Attach connection
matrix.encrypt();
matrix.detachConnection(); // Detach connection

// Get hiddenlink
matrix.setEncodeMode(true);
string strHL1, strHL2, strHL3;
matrix.getHiddenLink(1, strHL1); // hiddenlink for first row
matrix.getHiddenLink(2, strHL2);
matrix.getHiddenLink(3, strHL3);

// Get encrypted values
matrix.getElement(1, 1, str1_1); // get element (1, 1)
matrix.getElement(1, 2, str1_2);
matrix.getElement(2, 1, str2_1); // get element (2, 1)
matrix.getElement(2, 2, str2_2);
matrix.getElement(2, 3, str2_3);
matrix.getElement(3, 1, str3_1); // get element (3, 1)

cout << "\nAfter encryption:\n";
cout << "HiddenLink:\n";
cout << strHL1 << "\n";
cout << strHL2 << "\n";
cout << strHL3 << "\n";
cout << "Encrypted data:\n";
cout << str1_1 << ", " << str1_2 << endl;
cout << str2_1 << ", " << str2_2 << ", " << str2_3 << endl;
cout << str3_1 << endl;

// Set matrix for decryption
matrix.clear(); // clear previous content
matrix.setDecodeMode(true);
matrix.setHiddenLink(1, strHL1); // set hiddenlink for first row
matrix.setHiddenLink(2, strHL2);
matrix.setHiddenLink(3, strHL3);

matrix.setElement(1, 1, str1_1); // set element (1, 1)
matrix.setElement(1, 2, str1_2);
matrix.setElement(2, 1, str2_1); // set element (2, 1)
matrix.setElement(2, 2, str2_2);
matrix.setElement(2, 3, str2_3);
matrix.setElement(3, 1, str3_1); // set element (3, 1)

// Decrypt matrix
matrix.attachConnection(conn.operator->());
matrix.decrypt();
matrix.detachConnection();

// Get decrypted data
matrix.setEncodeMode(false);
matrix.getElement(1, 1, str1_1); // get element (1, 1)
matrix.getElement(1, 2, str1_2);
matrix.getElement(2, 1, str2_1); // get element (2, 1)
matrix.getElement(2, 2, str2_2);
matrix.getElement(2, 3, str2_3);
matrix.getElement(3, 1, str3_1); // get element (3, 1)

cout << "\nAfter decryption:\n";
cout << "Decrypted data:\n";
cout << str1_1 << ", " << str1_2 << endl;
cout << str2_1 << ", " << str2_2 << ", " << str2_3 << endl;
cout << str3_1 << endl;

// Close conneciton
conn->close();

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

return 0;
}
Output
Connected to localhost at 8888 as alice

Before encryption:
Original data:
test string (1, 1), test string (1, 2)
test string (2, 1), test string (2, 2), test string (2, 3)
test string (3, 1)

After encryption:
HiddenLink:
AAAAATJOayYYqRfwoLGOgDFIjORQc6M+o16oiLtGdipJ5yUvBs9RjA==
AAAAATJOayYYqRfwoLGOgDFIjORQc6M+o16oiLtGdipJ5yUvBs9RjA==
AAAAATJOayYYqRfwoLGOgDFIjORQc6M+o16oiLtGdipJ5yUvBs9RjA==
Encrypted data:
V+1vOX6q02hqczSP2kFT34Qo6j5ZwmPC, V+1vOX6q02hqczSP2kFT3yJrLMNXE+kU
V+1vOX6q02iQrLNsCnaMtDhmtdVihBrM, V+1vOX6q02iQrLNsCnaMtIqOfV3dJr/S, V+1vOX6q02iQrLNsCnaMtDrlWJwq2biA
V+1vOX6q02gs7Vn1Uf8WbOklrqsUMcyF

After decryption:
Decrypted data:
test string (1, 1), test string (1, 2)
test string (2, 1), test string (2, 2), test string (2, 3)
test string (3, 1)
Disconnected from server.