Introductory Example

24 RSA BSAFE Crypto-C Developers Guide

/* Step 4: Update */
encryptedData = T_malloc (dataToEncryptLen);
if ((status = (encryptedData == NULL_PTR)) != 0)
break;
if ((status = B_EncryptUpdate
(rc4Encrypter, encryptedData, &outputLenUpdate,
dataToEncryptLen, (unsigned char *)dataToEncrypt,
dataToEncryptLen, (B_ALGORITHM_OBJ)NULL_PTR,
(A_SURRENDER_CTX *)NULL_PTR)) != 0)
break;
/* Step 5: Final */
if ((status = B_EncryptFinal
(rc4Encrypter, encryptedData + outputLenUpdate,
&outputLenFinal, dataToEncryptLen - outputLenUpdate,
(B_ALGORITHM_OBJ)NULL_PTR,
(A_SURRENDER_CTX *)NULL_PTR)) != 0)
break;
encryptedDataLen = outputLenUpdate + outputLenFinal;
printf ("Encrypted data (%u bytes):\n", encryptedDataLen);
PrintBuf (encryptedData, encryptedDataLen);
} while (0);
/* Done with the key and algorithm objects, so destroy them. */
B_DestroyKeyObject (&rc4Key);
B_DestroyAlgorithmObject (&rc4Encrypter);
/* Free up any memory allocated, save it to a file or print it out first
if you need to save it. */
if (rc4KeyItem.data != NULL_PTR) {
T_memset (rc4KeyItem.data, 0, rc4KeyItem.len);
T_free (rc4KeyItem.data);
rc4KeyItem.data = NULL_PTR;
rc4KeyItem.len = 0;
}