Chapter 2 Quick Start 23

Introductory Example
static char dataToEncrypt[] = "Encrypt this sentence.";
unsigned char *encryptedData = NULL_PTR;
unsigned int dataToEncryptLen, encryptedDataLen;
unsigned int outputLenUpdate, outputLenFinal;
int status;
do {
dataToEncryptLen = strlen (dataToEncrypt) + 1;
/* Step 1: Create an algorithm object. */
if ((status = B_CreateAlgorithmObject (&rc4Encrypter)) != 0)
break;
/* Step 2: Set the algorithm to a type that does rc4 encryption.
AI_RC4 will do. */
if ((status = B_SetAlgorithmInfo
(rc4Encrypter, AI_RC4, NULL_PTR)) != 0)
break;
/* Step 3a: Create a key object. */
if ((status = B_CreateKeyObject (&rc4Key)) != 0)
break;
/* Step 3b: Set the key object with the 10-byte key. */
rc4KeyItem.data = rc4KeyData;
rc4KeyItem.len = rc4KeyDataLen;
if ((status = B_SetKeyInfo
(rc4Key, KI_Item, (POINTER)&rc4KeyItem)) != 0)
break;
if (rc4KeyItem.data != NULL_PTR) {
T_memset (rc4KeyItem.data, 0, rc4KeyItem.len);
T_free (rc4KeyItem.data);
rc4KeyItem.data = NULL_PTR;
rc4KeyItem.len = 0;
}
/* Step 3: Init */
if ((status = B_EncryptInit
(rc4Encrypter, rc4Key, DEMO_ALGORITHM_CHOOSER,
(A_SURRENDER_CTX *)NULL_PTR)) != 0)
break;