×
Namespaces

Variants
Actions

Encrypt-Decrypt contacts database entries using Symbian C++

From Nokia Developer Wiki
Jump to: navigation, search

This article shows how you might encrypt and decrypt the Contacts database using Symbian C++. The code snippet shows only a basic encryption for demonstration purposes. Note also that this implementation is at the database level so that encrypted data would be displayed encrypted in the existing phone book.

Article Metadata
Platform Security
Signing Required: DevCert
Capabilities: WriteDeviceData
Article
Created: kiran10182 (28 May 2007)
Last edited: hamishwillee (30 Jan 2012)

Headers Required:

#include <cntdb.h> // CContactDatabse, 
#include <cntitem.h> //CContactItem,CContactItemFieldSet

Library required (mmp file)

LIBRARY   cntmodel.lib  //CContactDatabse, CContactItem, CContactItemFieldSet

Capability required:

Capability       WriteDeviceData


Encrypt Contact Fields

void CEncryptContactContainer::EncryptAll()
{
CContactDatabase *contactDB = CContactDatabase::OpenL();
CleanupStack::PushL(contactDB);
 
TContactIter iter(*contactDB);
TContactItemId aContactId;
 
//Developer can take Heap based descriptor for large/unknown size of contact items.
TBuf16<70> aValue;
 
const CContactIdArray* contactArray = contactDB->SortedItemsL();
 
TInt cnt=contactArray->Count();
 
for(TInt i=0;i<cnt;i++)
{
CContactItem* contactItem=NULL;
 
contactItem= contactDB->OpenContactL((*contactArray)[i]);
CleanupStack::PushL(contactItem);
 
CContactItemFieldSet& fieldSet= contactItem->CardFields();
TInt fieldCount=fieldSet.Count(); // This will give number of contact fields.
 
for(TInt index=0; index < fieldCount; index++)
{
CContactItemField& field = fieldSet[index];
const CContentType& type = field.ContentType();
if(!(type.ContainsFieldType(KUidContactFieldBirthday)))
{
TPtrC name = contactItem->CardFields()[index].TextStorage()->Text();
aValue.Copy(name);
Encrypt(aValue); // Call real encyption here
contactItem->CardFields()[index].TextStorage()->SetTextL(aValue);
}
} //Inner for loop ends here
contactDB->CommitContactL(*contactItem);
CleanupStack::PopAndDestroy(contactItem);
} //Outer for loop ends here
CleanupStack::PopAndDestroy(contactDB);
}
void CEncryptContactContainer:: Encrypt (TDes& aValue)
{
for(TInt iCount=0; iCount< aValue.Length();iCount++)
{
aValue[iCount]+=3;
}
}

Decrypt Contact Fields

void CEncryptContactContainer::DecryptAll()
{
CContactDatabase *contactDB = CContactDatabase::OpenL();
CleanupStack::PushL(contactDB);
 
TContactIter iter(*contactDB);
TContactItemId aContactId;
TBuf16<70> aValue;
 
const CContactIdArray* contactArray = contactDB->SortedItemsL();
 
TInt cnt=contactArray->Count();
 
for(TInt i=0;i<cnt;i++)
{
CContactItem* contactItem=NULL;
 
contactItem= contactDB->OpenContactL((*contactArray)[i]);
CleanupStack::PushL(contactItem);
 
CContactItemFieldSet& fieldSet= contactItem->CardFields();
TInt fieldCount=fieldSet.Count(); // This will give number of contact fields.
 
for(TInt index=0; index < fieldCount; index++)
{
CContactItemField& field = fieldSet[index];
const CContentType& type = field.ContentType();
if(!(type.ContainsFieldType(KUidContactFieldBirthday)))
{
TPtrC name = contactItem->CardFields()[index].TextStorage()->Text();
aValue.Copy(name);
Decrypt(aValue);
contactItem->CardFields()[index].TextStorage()->SetTextL(aValue);
}
} //Inner for loop ends here
contactDB->CommitContactL(*contactItem);
CleanupStack::PopAndDestroy(contactItem);
} //Outer for loop ends here
CleanupStack::PopAndDestroy(contactDB);
}
void CEncryptContactContainer:: Decrypt (TDes& aValue)
{
for(TInt iCount=0; iCount< aValue.Length();iCount++)
{
aValue[iCount]-=3;
}
}
  • Here simple encryption is used to give an idea. Developer can use more complex Encyption-Decryption algorithms.
This page was last modified on 30 January 2012, at 08:42.
73 page views in the last 30 days.
×