Archived:CBC mode encryption and decryption of data
Article Metadata
Tested with
Devices(s): Tested on Nokia N95, Nokia N78, Nokia E66
Compatibility
Platform(s): S60 3rd Edition
Article
Keywords: CModeCBCEncryptor, CAESEncryptor
Created: (19 May 2009)
Last edited: Forum Nokia KB
(19 May 2009)
Overview
Cipher-block chaining (CBC) is an encryption mode where each block of plain text is XORed with the previous ciphertext block before being encrypted. This way, each ciphertext block is dependent on all plain text blocks processed up to that point. Also, to make each message unique, an initialisation vector must be used in the first block.
Description
The class CModeCBCEncryptor, available in the Symbian Cryptography library, can be used for encryption of data. It is initialised with a subclass of CBlockTransformation, in this case CAESEncryptor, which it subsequently owns. The following code snippet shows how to encrypt and decrypt a block of data in CBC mode.
Solution
CBC mode encryption of data:
void CCBCExAppUi::CBCEncryption(TPtr8& aDataPtr)
{
CAESEncryptor* aesEncryptor = CAESEncryptor::NewL(iCipherkey);
CModeCBCEncryptor* cbcEncryptor = CModeCBCEncryptor::NewLC(aesEncryptor, iIV);<br>
for(TInt i = 0; i<3; i++)
{
TPtr8 tempDataptr = aDataPtr.MidTPtr(16*i,16);
cbcEncryptor->Transform(tempDataptr);
cbcEncryptor->SetIV(tempDataptr);
}<br>
CleanupStack::PopAndDestroy();
}
CBC mode decryption of data:
void CCBCExAppUi::CBCDecryption(TPtr8& aDataPtr)
{
CAESDecryptor* aesdecryptor = CAESDecryptor::NewL(iCipherkey);
CModeCBCDecryptor* cbcDecryptor = CModeCBCDecryptor::NewLC(aesdecryptor, iIV);<br>
TBuf8<16>tempBuf;
for(TInt i = 0; i<3; i++)
{
TPtr8 tempDataptr = aDataPtr.MidTPtr(16*i,16);
tempBuf.Copy(tempDataptr);
cbcDecryptor->Transform(tempDataptr);
cbcDecryptor->SetIV(tempBuf);
}<br>
CleanupStack::PopAndDestroy();
}

