
Introductory Example
20 RSA BSAFE Crypto-C Developer’s Guide
For our example, the first argument is
rc4Encrypter
.
The second argument is a pointer to the output buffer that we created for
B_EncryptUpdate. However, B_EncryptUpdate has already placed some data into that
buffer, so we must pass the address of the next byte that is available after the already
filled bytes to
B_EncryptFinal. That is the address of the beginning of the buffer plus
the number of bytes that
B_EncryptUpdate filled, or
encryptedData
+
outputLenUpdate
.
The third argument is a pointer to an
unsigned int; B_EncryptFinal will set that
unsigned int to the number of bytes it encrypted.
The fourth argument is the size of the buffer available to
B_EncryptFinal. Because
B_EncryptUpdate has already written to part of the buffer, this value will be the total
size of the buffer minus the number of bytes
B_EncryptUpdate has used, or
dataToEncryptLen
-
outputLenUpdate
.
Once again, we can pass properly cast null pointers for the fifth and sixth arguments,
which are the random algorithm and surrender context.
Then, for our example, we have:
Step 6: Destroy
When you are done with an algorithm or key object, you must destroy it. The Destroy
function frees up any memory that was allocated by Crypto-C and zeroizes any
sensitive memory. Because you will always want to destroy the objects, place these
int B_EncryptFinal (
B_ALGORITHM_OBJ algorithmObject, /* algorithm object */
unsigned char *partOut, /* output data buffer */
unsigned int *partOutLen, /* length of output data */
unsigned int maxPartOutLen, /* size of output data buffer */
B_ALGORITHM_OBJ randomAlgorithm, /* random byte source */
A_SURRENDER_CTX *surrenderContext /* surrender context */
);
if ((status = B_EncryptFinal
(rc4Encrypter, encryptedData + outputLenUpdate,
&outputLenFinal, dataToEncryptLen - outputLenUpdate,
(B_ALGORITHM_OBJ)NULL_PTR,
(A_SURRENDER_CTX *)NULL_PTR)) != 0)
break;