Join Now
Quality Rating:
  • Currently 3.0 / 5
(3.0 / 5 - 1 vote cast)
Expertise Level:
  • Currently 4.0 / 5
(4.0 / 5 - 1 vote cast)

This page was last modified 12:39, 28 May 2007.

Encrypt-Decrypt Contact Database

From Forum Nokia Wiki

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.

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 Discussions
Thread Thread Starter Forum Replies Last Post
Sorting ContactItems awasthi_vivek Symbian User Interface 4 2004-03-23 06:11
How do I save an edited entry in the contacts database? reshmasp General Symbian C++ 1 2002-12-02 00:12
Contacts vijayasreesv General Symbian C++ 1 2005-03-02 13:27
Check if contact group exist? MDanielsson General Symbian C++ 15 2007-12-19 09:37
Modified/Added contact's ID sahdev_nitin General Symbian C++ 2 2007-07-04 13:35
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX