×
Namespaces

Variants
Actions
Revision as of 14:24, 19 May 2009 by Forum Nokia KB (Talk | contribs)

Archived:CBC mode encryption and decryption of data

From Nokia Developer Wiki
Jump to: navigation, search


Template:KBTS

Article Metadata
Tested with
Devices(s): Tested on Nokia N95, Nokia N78, Nokia E66
CompatibilityArticle
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();
}

Example application

File:CBCEx.zip

85 page views in the last 30 days.