×
Namespaces

Variants
Actions

Encrypt-Decrypt Messages

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Code ExampleArticle
Created: kiran10182 (28 May 2007)
Last edited: hamishwillee (30 Jan 2012)

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:
  • 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:

This page was last modified on 30 January 2012, at 04:29.
43 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×