Skip to main content

Encrypting Strings

Encrypting Strings

The EncryptCStrings() and DecryptCStrings() functions are used to encrypt and decrypt c-style strings. Their usage is straightforward. The following example demonstrates data encryption and decryption using these two functions.


Code Samples

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


< c_encryptcstrings.c>
/****************************************************************************************
* c_encryptcstrings.c
*
*
*****************************************************************************************/

#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 = "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 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];

uint32_t num = 3;

/* 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 Ttag */

printf("Original data:\n");
printf("str1 = %s\n", str1);
printf("str2 = %s\n", str2);
printf("str3 = %s\n", str3);
printf(" hl = %s\n", hl);

/* Encrypt */
ret = EncryptCStrings(
h, /* connection handle */
hl, /* T-tag */
HLSIZE, /* T-tag buffer size */
num, /* number of (c-string, buffer size) pairs */
str1, DATASIZE, str2, DATASIZE, str3, DATASIZE /* (c-string, buffer size) pairs */
);

if(ret) {
PrintError("EncryptCStrings() 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);

/* Decrypt */
ret = DecryptCStrings(
h, /* connection handle */
hl, /* T-tag */
num, /* number of c-strings */
str1, str2, str3 /* c-strings */
);

if(ret) {
PrintError("DecryptCStrings() 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;
}
Output
Connected to localhost at 8888 as ls
Original data:
str1 = This is test string 1
str2 = This is test string 2
str3 = This is test string 3
hl =
After encrypt:
str1 = dd/fH65n8ePwKjp2YgUNiOP3O8ukEyiDX/GD1gHgNks=
str2 = dd/fH65n8ePwKjp2YgUNiDhKqSKgojRnUSQHR9vW6Ek=
str3 = dd/fH65n8ePwKjp2YgUNiCAI/FvF+bKh79s09DgTx68=
hl = AAAAAehFbDRZOStyLbwgtpQfqHZJQyF2WF0y/7b/RX1/fFlJ7hPsvQ==
After decrypt:
str1 = This is test string 1
str2 = This is test string 2
str3 = This is test string 3
hl = AAAAAehFbDRZOStyLbwgtpQfqHZJQyF2WF0y/7b/RX1/fFlJ7hPsvQ==
Disconnected from server