Skip to main content

Working with a Data Matrix in C

Working with a Data Matrix in C

TEMatrixEncrypt() and TEMatrixDecrypt() are used to encrypt/decrypt a 2-D data matrix. The indices of the matrix start at 1. Data contained in the matrix can be encrypted, decrypted.

The following sequence describes how to encrypt and decrypt customer data with TEMatrixEncrypt() and TEMatrixDecrypt().

  1. Create a TEMatrix handle
  2. Set the decode mode if necessary
  3. Set the data to be encrypted or decrypted into the matrix, and then set hidden links (t-Tags) if necessary
  4. Encrypt or decrypt the data
  5. Set the decode mode if necessary
  6. Get the encrypted or decrypted data, and get hidden links (T-tags) if necessary

Code Samples

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


< c_encryptmatrix.c>
/****************************************************************************************
* c_encryptmatrix.c
*
*
*****************************************************************************************/

#include <stdio.h>
#include <string.h>
#include <memory.h>
#include "teagent_c.h"

void PrintError(const char * msg)
{
char szErr[1024];
uint32_t nLength = 1024;
uint32_t nCode = GetLastTEError(szErr, &nLength);
printf("%s Code = 0x%x, Message = %s\n", msg, nCode, szErr);
}

int main()
{
TE_HANDLE h;
TE_RETCODE ret;

/* Initialize the TE environment */
TEEnvInit();

/* Open connection to KeyServer */
{
char * szServerName = "localhost"; /* server name */
long lPort = 8888; /* port number */
char * szUserName = "ls"; /* user name */
char * szPassword = "password1A"; /* password */

/* Open a connection */
h = OpenConnectionWithNativeUser(
TE_SSL_CONNECTION, /* connection type */
szServerName, /* TE server name */
lPort, /* TE server port */
szUserName, /* user name */
strlen(szUserName), /* user name length */
szPassword, /* password */
strlen(szPassword) /* password length */
);
if (h == 0)
{
PrintError("Connection error:");
goto CLEANUP1;
}
else
printf("Connected to %s at %d as %s\n", szServerName, lPort, szUserName);
}

/* Encrypt/Decrypt matrix */
{
#define ROW_NUM 5
#define COL_NUM 5
#define DATA_LEN 128
#define HL_LEN 60

char data[ROW_NUM][COL_NUM][DATA_LEN]; /* original data */
char data_enc[ROW_NUM][COL_NUM][DATA_LEN]; /* encrypted data */
char data_dec[ROW_NUM][COL_NUM][DATA_LEN]; /* decrypted data */
char hl[ROW_NUM][HL_LEN]; /* hiddenlink (t-Tag)*/

int datalen_enc[ROW_NUM][COL_NUM]; /* length of encrypted data */
int hllen[ROW_NUM]; /* length of hiddenlink (t-Tag) */

TE_MATRIX_HANDLE mh = 0; /* TE matrix handle */
TE_MATRIX_HANDLE mh2 = 0;
int i, j;

/* Set original data */
printf("Original data:\n");
for(i=0; i<ROW_NUM; i++)
{
for(j=0; j<COL_NUM; j++)
{
sprintf(data[i][j], "this is data at row %d and column %d", i, j);
printf("data[%d][%d] = %s\n", i, j, data[i][j]);
}
}

/* Set matric elements */
mh = TEMatrixCreate();
for(i=0; i<ROW_NUM; i++)
{
for(j=0; j<COL_NUM; j++)
{
ret = TEMatrixSetElement(mh, i+1, j+1, data[i][j], strlen(data[i][j]));
if(ret) {
PrintError("TEMatrixSetElement() failed:");
goto CLEANUP2;
}
}
}

/* Encrypt matrix */
ret = TEMatrixEncrypt(h, mh);
if(ret) {
PrintError("TEMatrixEncrypt() failed:");
goto CLEANUP2;
}

TEMatrixSetEncode(mh, 1); /* set data format */

/* Get encrypted data and hidden link */
printf("Encrypted data:\n");
for(i=0; i<ROW_NUM; i++)
{
for(j=0; j<COL_NUM; j++)
{

ret = TEMatrixGetElement(mh, i+1, j+1, data_enc[i][j], DATA_LEN, &datalen_enc[i][j]);
if(ret) {
PrintError("TEMatrixGetElement() failed:");
goto CLEANUP2;
}
if(TEMatrixGetEncode(mh))
printf("data_enc[%d][%d] = %s\n", i, j, data_enc[i][j]);
else
printf("datalen_enc[%d][%d] = %d\n", i, j, datalen_enc[i][j]);
}

ret = TEMatrixGetHiddenLink(mh, i+1, hl[i], HL_LEN, &hllen[i]);
if(ret) {
PrintError("TEMatrixGetHiddenLink() failed:");
goto CLEANUP2;
}
if(TEMatrixGetEncode(mh))
printf("hllen[%d] = %s\n", i, hl[i]);
else
printf("hllen[%d] = %d\n", i, hllen[i]);
}

/* Prepare matrix for decryption */
mh2 = TEMatrixCreate();

TEMatrixSetDecode(mh2, 1); /* set data format */

/* Set encrypted data and hidden link */
for(i=0; i<ROW_NUM; i++)
{
for(j=0; j<COL_NUM; j++)
{
ret = TEMatrixSetElement(mh2, i+1, j+1, data_enc[i][j], datalen_enc[i][j]);
if(ret) {
PrintError("TEMatrixSetElement() failed:");
goto CLEANUP3;
}
}

ret = TEMatrixSetHiddenLink(mh2, i+1, hl[i], hllen[i]);
if(ret) {
PrintError("TEMatrixSetHiddenLink() failed:");
goto CLEANUP3;
}
}

/* Decrypt matrix */
ret = TEMatrixDecrypt(h, mh2);
if(ret) {
PrintError("TEMatrixDecrypt() failed:");
goto CLEANUP3;
}

/* Get decrypted data and hidden link */
printf("Decrypted data:\n");
for(i=0; i<ROW_NUM; i++)
{
for(j=0; j<COL_NUM; j++)
{
int nLen;
memset(data_dec[i][j], 0, DATA_LEN);
ret = TEMatrixGetElement(mh2, i+1, j+1, data_dec[i][j], DATA_LEN, &nLen);
if(ret) {
PrintError("TEMatrixGetElement() failed:");
goto CLEANUP3;
}
printf("data_dec[%d][%d] = %s\n", i, j, data_dec[i][j]);
}
}

printf("Test matrix OK\n");

CLEANUP3:
TEMatrixRelease(mh);

CLEANUP2:
TEMatrixRelease(mh);
}


CLEANUP1:
/* Close a connection */
CloseConnection(h);

printf("Disconnected from server\n\n");

/* Uninitialize the TE environment */
TEEnvClose();
return 0;
}
Output
Connected to localhost at 8888 as ls
Original data:
data[0][0] = this is data at row 0 and column 0
data[0][1] = this is data at row 0 and column 1
data[0][2] = this is data at row 0 and column 2
data[0][3] = this is data at row 0 and column 3
data[0][4] = this is data at row 0 and column 4
data[1][0] = this is data at row 1 and column 0
data[1][1] = this is data at row 1 and column 1
data[1][2] = this is data at row 1 and column 2
data[1][3] = this is data at row 1 and column 3
data[1][4] = this is data at row 1 and column 4
data[2][0] = this is data at row 2 and column 0
data[2][1] = this is data at row 2 and column 1
data[2][2] = this is data at row 2 and column 2
data[2][3] = this is data at row 2 and column 3
data[2][4] = this is data at row 2 and column 4
data[3][0] = this is data at row 3 and column 0
data[3][1] = this is data at row 3 and column 1
data[3][2] = this is data at row 3 and column 2
data[3][3] = this is data at row 3 and column 3
data[3][4] = this is data at row 3 and column 4
data[4][0] = this is data at row 4 and column 0
data[4][1] = this is data at row 4 and column 1
data[4][2] = this is data at row 4 and column 2
data[4][3] = this is data at row 4 and column 3
data[4][4] = this is data at row 4 and column 4
Encrypted data:
data_enc[0][0] = KEZut18Pa4srQscwDa9GVVRpAqqniM68xujsXX7un65zCjCRMOLsyRAl+jgpQKRV
data_enc[0][1] = KEZut18Pa4srQscwDa9GVVRpAqqniM68xujsXX7un67koV8ijIXtLdO9/82DUFgD
data_enc[0][2] = KEZut18Pa4srQscwDa9GVVRpAqqniM68xujsXX7un66g+lEpaYLgrRo+tmRDIwTx
data_enc[0][3] = KEZut18Pa4srQscwDa9GVVRpAqqniM68xujsXX7un6431VqgYYgYEonIcYtAQRV7
data_enc[0][4] = KEZut18Pa4srQscwDa9GVVRpAqqniM68xujsXX7un64mpofU5FhBGzQeFp0nviKq
hllen[0] = AAAAATSYOjsbQOg92Og7rngtXO1mxD9MyRQ2HAtJxpoHHg6J977AaA==
data_enc[1][0] = KEZut18Pa4srQscwDa9GVZCbtupaJahXlo+FyxwKJUAJ7pAzLx4IITozD/wnuGA9
data_enc[1][1] = KEZut18Pa4srQscwDa9GVZCbtupaJahXlo+FyxwKJUCFw1yAp/hFWrHT5uz3ZwOD
data_enc[1][2] = KEZut18Pa4srQscwDa9GVZCbtupaJahXlo+FyxwKJUBNswqFqXp7cwe+NL+82C9V
data_enc[1][3] = KEZut18Pa4srQscwDa9GVZCbtupaJahXlo+FyxwKJUC50qHJKgXVQ24Eq4BrEjL7
data_enc[1][4] = KEZut18Pa4srQscwDa9GVZCbtupaJahXlo+FyxwKJUBRhh1uxfhH8KSQuEwN+AYF
hllen[1] = AAAAATSYOjsbQOg92Og7rngtXO1mxD9MyRQ2HAtJxpoHHg6J977AaA==
data_enc[2][0] = KEZut18Pa4srQscwDa9GVdKYpHFZcbDYLTqpynNa6xml9Q/jCa0kSwr58wLA6atg
data_enc[2][1] = KEZut18Pa4srQscwDa9GVdKYpHFZcbDYLTqpynNa6xkn6mK5hNR8LjU9Z0jIA4+j
data_enc[2][2] = KEZut18Pa4srQscwDa9GVdKYpHFZcbDYLTqpynNa6xn1SGgfsHM+RL0O/eruStZl
data_enc[2][3] = KEZut18Pa4srQscwDa9GVdKYpHFZcbDYLTqpynNa6xm6JiPDLTIqVo7h1/g2PY+i
data_enc[2][4] = KEZut18Pa4srQscwDa9GVdKYpHFZcbDYLTqpynNa6xkFksGn1zVdFSJE0gOmyw4s
hllen[2] = AAAAATSYOjsbQOg92Og7rngtXO1mxD9MyRQ2HAtJxpoHHg6J977AaA==
data_enc[3][0] = KEZut18Pa4srQscwDa9GVTitojqpZ+UOnN+OM8qwsW6Oe2MYn/Ku3V8TqFj3EXi/
data_enc[3][1] = KEZut18Pa4srQscwDa9GVTitojqpZ+UOnN+OM8qwsW6N0R0B9vDEYNG3beHaNd42
data_enc[3][2] = KEZut18Pa4srQscwDa9GVTitojqpZ+UOnN+OM8qwsW4vW/0gl+y+irqFvgOe+hTA
data_enc[3][3] = KEZut18Pa4srQscwDa9GVTitojqpZ+UOnN+OM8qwsW6QcTUeMi0GoOQj0U5moUWo
data_enc[3][4] = KEZut18Pa4srQscwDa9GVTitojqpZ+UOnN+OM8qwsW46CDDyfkAa4XosE10YGs83
hllen[3] = AAAAATSYOjsbQOg92Og7rngtXO1mxD9MyRQ2HAtJxpoHHg6J977AaA==
data_enc[4][0] = KEZut18Pa4srQscwDa9GVUDcOppaEhQSAxZ/aZKf47DuQOIYOsW41muNQG8VP6Ml
data_enc[4][1] = KEZut18Pa4srQscwDa9GVUDcOppaEhQSAxZ/aZKf47DKcgpPJhs5uQUOAGsOZGkE
data_enc[4][2] = KEZut18Pa4srQscwDa9GVUDcOppaEhQSAxZ/aZKf47BStjNZRmxaEUxgb8E+OAF9
data_enc[4][3] = KEZut18Pa4srQscwDa9GVUDcOppaEhQSAxZ/aZKf47DhR2yDj/r9vrc2X+uctHvn
data_enc[4][4] = KEZut18Pa4srQscwDa9GVUDcOppaEhQSAxZ/aZKf47CYZGaFZKMx3cAPdMO1/ifk
hllen[4] = AAAAATSYOjsbQOg92Og7rngtXO1mxD9MyRQ2HAtJxpoHHg6J977AaA==
Decrypted data:
data_dec[0][0] = this is data at row 0 and column 0
data_dec[0][1] = this is data at row 0 and column 1
data_dec[0][2] = this is data at row 0 and column 2
data_dec[0][3] = this is data at row 0 and column 3
data_dec[0][4] = this is data at row 0 and column 4
data_dec[1][0] = this is data at row 1 and column 0
data_dec[1][1] = this is data at row 1 and column 1
data_dec[1][2] = this is data at row 1 and column 2
data_dec[1][3] = this is data at row 1 and column 3
data_dec[1][4] = this is data at row 1 and column 4
data_dec[2][0] = this is data at row 2 and column 0
data_dec[2][1] = this is data at row 2 and column 1
data_dec[2][2] = this is data at row 2 and column 2
data_dec[2][3] = this is data at row 2 and column 3
data_dec[2][4] = this is data at row 2 and column 4
data_dec[3][0] = this is data at row 3 and column 0
data_dec[3][1] = this is data at row 3 and column 1
data_dec[3][2] = this is data at row 3 and column 2
data_dec[3][3] = this is data at row 3 and column 3
data_dec[3][4] = this is data at row 3 and column 4
data_dec[4][0] = this is data at row 4 and column 0
data_dec[4][1] = this is data at row 4 and column 1
data_dec[4][2] = this is data at row 4 and column 2
data_dec[4][3] = this is data at row 4 and column 3
data_dec[4][4] = this is data at row 4 and column 4
Test matrix OK
Disconnected from server