Skip to main content

Create Key in C

Create Key in C

The TECreateKey() function is used to create a key. You can set the algorithm and the ACL template when you call this function.


Code Samples

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


< c_createkey.c>
/****************************************************************************************
* c_createkey.c
*
*
*****************************************************************************************/

#include <stdio.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);
}

/* Create key */
{
#define KEY_BUFFER_SIZE 1024
#define HIDDENLINK_BUFFER_SIZE 100

char * algorithm = NULL; /* set to NULL or "" to use KeyServer default algorithm */
char * acltemplate = NULL; /* set to NULL or "" if no alc template is provided */
unsigned char hl[HIDDENLINK_BUFFER_SIZE];
int len;
int i;

ret = TECreateKey(
h, /* connection handle */
algorithm, /* algorithm */
acltemplate, /* ACL template name */
hl, /* T-tag buffer */
HIDDENLINK_BUFFER_SIZE, /* T-tag buffer size */
&len /* returned T-tag data length */
);

if(ret) {
PrintError("Create key failed:");
goto CLEANUP;
}

printf("T-tag (len = %d) :\n", len);
for(i=0; i<len; i++) {
printf("[%2x] ", hl[i]);
}
printf("\n");
}


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
T-tag (len = 40) :
[ 0] [ 0] [ 0] [ 1] [99] [c7] [ae] [43] [95] [c8] [71] [b8] [ee] [a4] [d0] [40] [a2] [f7] [53] [a3] [6c] [c1] [86] [43]
[46] [96] [7a] [a0] [a9] [6e] [ 4] [12] [66] [99] [b6] [4a] [c4] [db] [7b] [e2]
Disconnected from server