You Are Here:

Community: Wiki

This page was last modified on 9 September 2009, at 05:49.

Encrypt-Decrypt Contact Database

From Forum Nokia Wiki

Reviewer Approved   

Security of user information has become an essential part at many instances in the application development life cycle. Following code snippets perform some means of security features with Phonebook of user's phone.

  • Contact Database is being encrypted here which results in non-readable form of contact items in the Phonebook of user's phone.
  • On the other side Contact Database is decrypted to get the readable content back.


Hearders Required:

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

Library required:

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.

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia