Chapter 6 Symmetric-Key Operations 183
Block Ciphers
Step 5: Final
Step 6: Destroy
Remember to destroy all objects that you created and free up any memory that you
allocated:
Note: Using
T_free means you can no longer access the data at that address. Do not
free a buffer until you no longer need the data it contains. If you will need the
data later, you might want to save it to a file first.
Decrypting
As in the “Introductory Example” on page 9, decrypting is similar to encrypting. Use
the same AI, IV, and key data. Use the proper decryption AM and call
B_DecryptInit,
B_DecryptUpdate, and B_DecryptFinal.
if ((status = B_EncryptUpdate
(encryptionObject, encryptedData, &outputLenUpdate,
encryptedDataLen, (unsigned char *)dataToEncrypt,
dataToEncryptLen, (B_ALGORITHM_OBJ)NULL_PTR,
(A_SURRENDER_CTX *)NULL_PTR)) != 0)
break;
if ((status = B_EncryptFinal
(encryptionObject, encryptedData + outputLenUpdate,
&outputLenFinal, encryptedDataLen - outputLenUpdate,
(B_ALGORITHM_OBJ)NULL_PTR,
(A_SURRENDER_CTX *)NULL_PTR)) != 0)
break;
B_DestroyKeyObject (&desKey);
B_DestroyAlgorithmObject (&encryptionObject);
B_DestroyAlgorithmObject (&randomAlgorithm);
T_free (encryptedData);