Encrypt-Decrypt Messages
Article Metadata
Code Example
Article
Contents |
Introduction
Short Messaging Service(SMS) is the heart of Mobile Device. SMS has gained popularity in the communication medium over the years. People generally do not share their private information in nature. Thus, messages are part of this so called private information. In some cases user may wish to encrypt-hide-delete his/her messages when his mobile device is out of reach to him. The following article is all about playing with such security features that can be enabled with messages.
Prerequisite
- Download SmsHandler.zip as follows:
- For S60 2nd edition = SmsHander for S60 2nd.Zip
- For S60 3rd edition = SmsHander for S60 3rd.Zip
- Extracting SmsHandler.zip will result into SmsHandler.h and SmsHandler.cpp
- Copy-Paste SmsHandler.h into your project's /inc folder.
- Copy-Paste SmsHandler.cpp into your project's /src folder.
- Edit your .mmp file. Add an entry for SmsHandler.cpp in SOURCE directive.
SOURCE SMSHandler.cpp
- Edit your .mmp file. Add libraries for SMS handling.
//Libraries included for SMS support-
LIBRARY msgs.lib smcm.lib gsmu.lib mtur.lib
- Open your CYrApplicationContainer.h file.
- Inlcude SmsHandler.h.
#include "SMSHandler.h" //Added for SMS Handling- Define an object of SmsHandler class.
private: //data
.......
.......
CSmsHandler* iSmsHandler;
- Open your CYrApplicationContainer.cpp file.
- Initialize SmsHanlder object.
CYrApplicationContainer::Constructl()......
{
.....
.....
SetRect(aRect);
ActivateL();
iSmsHandler = CSmsHandler::NewL(); // SmsHandler
}
Encrypt Messages
The following code snippets illustrate the Encryption part of the messages.
Argument aFieldToBeProcessed can be:
- KMsvGlobalInBoxIndexEntryId
- KMsvGlobalOutBoxIndexEntryId
- KMsvDraftEntryId
- KMsvSentEntryId
void CSmsHandler::EncryptAllL(TMsvId aFieldToBeProcessed)
{
TBuf16<500> SMSContent;
TMsvSelectionOrdering sort;
sort.SetShowInvisibleEntries(ETrue); // we want to handle also the invisible entries
CMsvEntry* inboxContext=CMsvEntry::NewL(*iSession,aFieldToBeProcessed,sort);
CleanupStack::PushL(inboxContext);
CMsvEntrySelection* entries = inboxContext->ChildrenL();
CleanupStack::PushL( entries );
TInt msgCount= entries->Count();
for (TInt i=0; i<entries->Count(); i++)
{
TMsvId entryID = entries->At(i);
iSmsMtm->SwitchCurrentEntryL(entryID);
CMsvEntry* entry= iSession->GetEntryL((*entries)[i]);
CleanupStack::PushL(entry);
TMsvEntry entry1 = entry->Entry();
CMsvStore* inboxStore= entry->EditStoreL();
CleanupStack::PushL(inboxStore);
iSmsMtm->Entry().ChangeL( entry1 );
if (inboxStore->HasBodyTextL())
{
TBufC<50> aText(entry1.iDetails);
CRichText& RichText= iSmsMtm->Body();
inboxStore->RestoreBodyTextL(RichText);
const TInt length = RichText.DocumentLength();
SMSContent.Copy(RichText.Read(0,length));
Encrypt(SMSContent ); // Encrypt Message Body
RichText.Reset();
RichText.InsertL( 0 , SMSContent);
inboxStore->StoreBodyTextL(RichText);
TBuf16<20> PhoneNumber;
PhoneNumber.Copy(aText);
Encrypt( PhoneNumber ); // Encrypt Phone Number header of the Message
entry1.iDescription.Set(SMSContent);
entry1.iDetails.Set(PhoneNumber);
iSmsMtm->Entry().ChangeL( entry1 );
inboxStore->CommitL();
}
else
{
// no text in SMS
}
CleanupStack::PopAndDestroy(inboxStore);
CleanupStack::PopAndDestroy(entry);
}
CleanupStack::PopAndDestroy(entries);
CleanupStack::PopAndDestroy(inboxContext);
}
void CSmsHandler:: Encrypt(TDes& val)
{
for(TInt count=0; count< val.Length();count++)
{
val[count]+=5;
}
}
Decrypt Messages
The following code snippets illustrate the reverse procedure of encryption which will let messages in original readable form.
Argument aFieldToBeProcessed can be:
- KMsvGlobalInBoxIndexEntryId
- KMsvGlobalOutBoxIndexEntryId
- KMsvDraftEntryId
- KMsvSentEntryId
void CSmsHandler::DecryptAllL(TMsvId aFieldToBeProcessed)
{
TBuf16<500> SMSContent;
TMsvSelectionOrdering sort;
sort.SetShowInvisibleEntries(ETrue); // we want to handle also the invisible entries
CMsvEntry* inboxContext=CMsvEntry::NewL(*iSession,aFieldToBeProcessed,sort);
CleanupStack::PushL(inboxContext);
CMsvEntrySelection* entries = inboxContext->ChildrenL();
CleanupStack::PushL( entries );
TInt msgCount= entries->Count();
for (TInt i=0; i<entries->Count(); i++)
{
TMsvId entryID = entries->At(i);
iSmsMtm->SwitchCurrentEntryL(entryID);
CMsvEntry* entry= iSession->GetEntryL((*entries)[i]);
CleanupStack::PushL(entry);
TMsvEntry entry1 = entry->Entry();
CMsvStore* inboxStore= entry->EditStoreL();
CleanupStack::PushL(inboxStore);
iSmsMtm->Entry().ChangeL( entry1 );
if (inboxStore->HasBodyTextL())
{
TBufC<50> aText(entry1.iDetails);
CRichText& RichText= iSmsMtm->Body();
inboxStore->RestoreBodyTextL(RichText);
const TInt length = RichText.DocumentLength();
SMSContent.Copy(RichText.Read(0,length));
Decrypt(SMSContent ); // Encrypt Message Body
RichText.Reset();
RichText.InsertL( 0 , SMSContent);
inboxStore->StoreBodyTextL(RichText);
TBuf16<20> PhoneNumber;
PhoneNumber.Copy(aText);
Decrypt( PhoneNumber ); // Encrypt Phone Number header of the Message
entry1.iDescription.Set(SMSContent);
entry1.iDetails.Set(PhoneNumber);
iSmsMtm->Entry().ChangeL( entry1 );
inboxStore->CommitL();
}
else
{
// no text in SMS
}
CleanupStack::PopAndDestroy(inboxStore);
CleanupStack::PopAndDestroy(entry);
}
CleanupStack::PopAndDestroy(entries);
CleanupStack::PopAndDestroy(inboxContext);
}
void CSmsHandler:: Decrypt (TDes& val)
{
for(TInt count=0; count< val.Length();count++)
{
val[count]-=5;
}
}
Hide Messages
The following code snippets illustrate hiding messages for security purpose.
Argument aFieldToBeProcessed can be:
- KMsvGlobalInBoxIndexEntryId
- KMsvGlobalOutBoxIndexEntryId
- KMsvDraftEntryId
- KMsvSentEntryId
void CSmsHandler::HideAllL(TMsvId aFieldToBeProcessed)
{
TMsvSelectionOrdering sort;
sort.SetShowInvisibleEntries(ETrue); // we want to handle also the invisible entries
CMsvEntry* inboxContext=CMsvEntry::NewL(*iSession,aFieldToBeProcessed,sort);
CleanupStack::PushL(inboxContext);
CMsvEntrySelection* entries = inboxContext->ChildrenL();
CleanupStack::PushL( entries );
TInt msgCount= entries->Count();
for (TInt i=0; i<entries->Count(); i++)
{
TMsvId entryID = entries->At(i);
iSmsMtm->SwitchCurrentEntryL(entryID);
CMsvEntry* entry= iSession->GetEntryL((*entries)[i]);
CleanupStack::PushL(entry);
TMsvEntry entry1 = entry->Entry();
CMsvStore* inboxStore= entry->ReadStoreL();
CleanupStack::PushL(inboxStore);
entry1.SetVisible(EFalse);
iSmsMtm->Entry().ChangeL( entry1 );
CleanupStack::PopAndDestroy(inboxStore);
CleanupStack::PopAndDestroy(entry);
}
CleanupStack::PopAndDestroy(entries);
CleanupStack::PopAndDestroy(inboxContext);
}
Show Messages
The following code snippets illustrate reverse procedure of getting messages back in the "Show" mode.
Argument aFieldToBeProcessed can be:
- KMsvGlobalInBoxIndexEntryId
- KMsvGlobalOutBoxIndexEntryId
- KMsvDraftEntryId
- KMsvSentEntryId
void CSmsHandler::ShowAllL(TMsvId aFieldToBeProcessed)
{
TMsvSelectionOrdering sort;
sort.SetShowInvisibleEntries(ETrue); // we want to handle also the invisible entries
CMsvEntry* inboxContext=CMsvEntry::NewL(*iSession,aFieldToBeProcessed,sort);
CleanupStack::PushL(inboxContext);
CMsvEntrySelection* entries = inboxContext->ChildrenL();
CleanupStack::PushL( entries );
TInt msgCount= entries->Count();
for (TInt i=0; i<entries->Count(); i++)
{
TMsvId entryID = entries->At(i);
iSmsMtm->SwitchCurrentEntryL(entryID);
CMsvEntry* entry= iSession->GetEntryL((*entries)[i]);
CleanupStack::PushL(entry);
TMsvEntry entry1 = entry->Entry();
CMsvStore* inboxStore= entry->ReadStoreL();
CleanupStack::PushL(inboxStore);
entry1.SetVisible(ETrue);
iSmsMtm->Entry().ChangeL( entry1 );
CleanupStack::PopAndDestroy(inboxStore);
CleanupStack::PopAndDestroy(entry);
}
CleanupStack::PopAndDestroy(entries);
CleanupStack::PopAndDestroy(inboxContext);
}
Delete Messages
For more brutal force, you can delete messages from Messaging folders.
Argument aFieldToBeProcessed can be:
- KMsvGlobalInBoxIndexEntryId
- KMsvGlobalOutBoxIndexEntryId
- KMsvDraftEntryId
- KMsvSentEntryId
void CSmsHandler::DeleteMessagesL(TMsvId aFieldToBeProcessed)
{
TMsvSelectionOrdering sort;
sort.SetShowInvisibleEntries(ETrue); // we want to handle also the invisible entries
CMsvEntry* inboxContext=CMsvEntry::NewL(*iSession,aFieldToBeProcessed,sort);
CleanupStack::PushL(inboxContext);
CMsvEntrySelection* entries = inboxContext->ChildrenL();
CleanupStack::PushL( entries );
TInt msgCount= entries->Count();
TInt i;
for (i=0; i<entries->Count(); i++)
{
TMsvId entryID = entries->At(i);
iSmsMtm->SwitchCurrentEntryL(entryID);
CMsvEntry* entry= iSession->GetEntryL((*entries)[i]);
CleanupStack::PushL(entry);
entry->DeleteL(entries->At(i));
CleanupStack::PopAndDestroy(entry);
}
CleanupStack::PopAndDestroy(entries);
CleanupStack::PopAndDestroy(inboxContext);
}
See Also:


17 Sep
2009
Secure handling of sensitive data - one of the most important conditions for the development of successful applications. You should be very carefully while reading or writing personal information, such as SMS-messages. This articles can help you, it illustrates how to manage SMS-messages which were stored in device memory.
It also shows how to make encryption/decryption of the data, despite the fact that demonstrated methods are very simple, they very good illustrate the main idea.