c_encryptcstrings.c
/****************************************************************************************
* c_encryptcstrings.c
*
* Note: See Tricryption Engine SDK for details on how to set the project environment.
*
*****************************************************************************************/
#include <stdio.h>
#include <string.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 = "ChangeMe-AdminPassword"; /* 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 CLEANUP;
}
else
printf("Connected to %s at %d as %s\n", szServerName, lPort, szUserName);
}
/* Encrypt/Decrypt c-style strings */
{
#define HLSIZE 60
#define DATASIZE 250
char hl[HLSIZE];
char str1[DATASIZE];
char str2[DATASIZE];
char str3[DATASIZE];
/* One descriptor per buffer. Each carries its own input length AND its own
writable capacity, so nothing in the SDK has to guess either. */
TE_CSTRING_BUFFER ttag;
TE_CSTRING_BUFFER data[3];
uint32_t num = 3;
uint32_t i;
/* Prepare the original data */
strcpy(str1, "This is test string 1");
strcpy(str2, "This is test string 2");
strcpy(str3, "This is test string 3");
strcpy(hl, ""); /* KeyServer will generate a new key if you provide an empty hiddenlink */
printf("Original data:\n");
printf("str1 = %s\n", str1);
printf("str2 = %s\n", str2);
printf("str3 = %s\n", str3);
printf(" hl = %s\n", hl);
ttag.data = (unsigned char *)hl;
ttag.input_len = (uint32_t)strlen(hl); /* 0 here: ask for a new T-tag */
ttag.capacity = HLSIZE;
ttag.output_len = 0;
data[0].data = (unsigned char *)str1;
data[1].data = (unsigned char *)str2;
data[2].data = (unsigned char *)str3;
for (i = 0; i < num; i++) {
data[i].input_len = (uint32_t)strlen((char *)data[i].data);
data[i].capacity = DATASIZE;
data[i].output_len = 0;
}
/* Encrypt */
ret = TEEncryptStringBatch(
h, /* connection handle */
&ttag, /* T-tag descriptor, filled in on return */
data, /* descriptor array */
num /* number of descriptors */
);
if(ret) {
/* On TE_INSUFFICIENT_BUFFER_SIZE every descriptor's output_len holds the
capacity it needed, and NO buffer has been written. */
PrintError("TEEncryptStringBatch() failed:");
goto CLEANUP;
}
printf("After encrypt:\n");
printf("str1 = %s\n", str1);
printf("str2 = %s\n", str2);
printf("str3 = %s\n", str3);
printf(" hl = %s\n", hl);
/* Feed each result back in as the next call's input. */
ttag.input_len = ttag.output_len;
for (i = 0; i < num; i++)
data[i].input_len = data[i].output_len;
/* Decrypt */
ret = TEDecryptStringBatch(
h, /* connection handle */
&ttag, /* T-tag descriptor, read only here */
data, /* descriptor array */
num /* number of descriptors */
);
if(ret) {
PrintError("TEDecryptStringBatch() failed:");
goto CLEANUP;
}
printf("After decrypt:\n");
printf("str1 = %s\n", str1);
printf("str2 = %s\n", str2);
printf("str3 = %s\n", str3);
printf(" hl = %s\n", hl);
}
CLEANUP:
/* Close a connection */
CloseConnection(h);
printf("Disconnected from server\n\n");
/* Uninitialize the TE environment */
TEEnvClose();
return 0;
}