Chapter 2 Quick Start 19
Introductory Example
function must know the size of the buffer. The Update function will not attempt to
place data into unallocated memory; instead, it returns an error if it needs to place
more bytes into the buffer than are allocated. In our example, we will use
dataToEncryptLen
as our output data size.
The seventh argument is a random algorithm. Recall that in Chapter 2 of the Reference
Manual, the description of AI_RC4 states:
That is exactly what we will supply in our example.
For the eighth argument, once again we pass a properly cast NULL_PTR as the
surrender context. When we put this all together, our Update call is:
Note the warning in the Reference Manual Chapter 2 entry for AI_RC4:
This simply means that you should not use the same key for two different encryption
sessions.
Step 5: Final
B_EncryptFinal finalizes the encryption process by encrypting any data that
B_EncryptUpdate could not. See Chapter 4 of the Reference Manual for the functions
description and prototype:
You may pass (B_ALGORITHM_OBJ)NULL_PTR for all
randomAlgorithm
arguments.
if ((status = B_EncryptUpdate
(rc4Encrypter, encryptedData, &outputLenUpdate,
dataToEncryptLen, dataToEncrypt, dataToEncryptLen,
(B_ALGORITHM_OBJ)NULL_PTR,
(A_SURRENDER_CTX *)NULL_PTR)) != 0)
break;
encryptedDataLen = outputLenUpdate + outputLenFinal
Due to the nature of the RC4 algorithm, security is compromised if multiple data
blocks are encrypted with the same RC4 key. Therefore, B_EncryptUpdate cannot be
called after B_EncryptFinal. This is because after a call to B_EncryptFinal and
B_DecryptFinal, the state of the algorithm object is reset to the state in which it was
following the call to B_EncryptInit and B_DecryptInit. To begin an encryption
operation for a new data block, you must call B_EncryptInit and supply a new key.