×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Regular Contributor
    Join Date
    Mar 2007
    Posts
    165

    Iam totally in confusion .......Pls help me

    Pls see the code.Iam opening the ContactsDataBase and getting the FirstName field text from the records and encrypting the FirstName.
    If only one contact is present in the PhoneBook,its encrypting successfully.But when more than 1 contact is present in the PhoneBook only 1contact is encrypting and the emulator crashes with "user 42" Panic error.

    // Read name of the default database
    _LIT( KOrgContactFile,"" );
    TBuf<KMaxDatabasePathAndNameLength> orgContactFile( KOrgContactFile );
    CContactDatabase::GetDefaultNameL( orgContactFile );
    orgContactFile.LowerCase();
    if ( iContactDb )
    {
    if ( !iDbName.Compare( orgContactFile ) )
    {
    return;
    }
    iContactDb->CloseTables();
    delete iContactDb;
    iContactDb = NULL;
    }
    //Open the Default DataBase
    TRAPD(err, iContactDb = CContactDatabase::OpenL( orgContactFile );
    // Check if database already exist
    if ( err != KErrNotFound )//If the DataBase already Exists then Read Contacts From the DataBase
    {
    iContactDb->SetDbViewContactType( KUidContactCard );
    TFieldType aFieldType1( KUidContactFieldFamilyName );//FirstName
    TFieldType aFieldType2( KUidContactFieldGivenName );//LastName
    CContactDatabase::TSortPref sortPref1( aFieldType1 );
    CContactDatabase::TSortPref sortPref2( aFieldType2 );
    // Sort contacts by Family and Given Name
    CArrayFixFlat<CContactDatabase::TSortPref>* aSortOrder =
    new (ELeave) CArrayFixFlat<CContactDatabase::TSortPref>(2);
    CleanupStack::PushL( aSortOrder );
    aSortOrder->AppendL( sortPref1 );
    aSortOrder->AppendL( sortPref2 );
    // The database takes ownership of the sort order array passed in
    iContactDb->SortL( aSortOrder );
    // The caller does not take ownership of this object.
    // so do not push it onto the CleanupStack
    const CContactIdArray* contacts = iContactDb->SortedItemsL();
    // Go through each contact item and
    const TInt nc( contacts->Count() );
    for(TInt i=0; i <= nc-1; i++)
    {
    CContactItem* contact = NULL;
    // The caller takes ownership of the returned object.
    // So push it onto the CleanupStack
    contact = iContactDb->OpenContactL( (*contacts)[i] );
    CleanupStack::PushL( contact );
    //This is used to get the count of Fields in a Record
    CContactItemFieldSet& fieldSet = contact->CardFields();
    //Now get the Text of Fields in a Record
    HBufC* firstNameBuf = NULL;
    HBufC* lastNameBuf = NULL;
    // Get first name
    TInt findpos( fieldSet.Find( KUidContactFieldGivenName ) );
    // Check that the first name field is actually there.
    CContactTextField* firstName;
    // Check that the first name field is actually there.
    if ( (findpos > -1) || (findpos >= fieldSet.Count()) )
    {
    CContactItemField& firstNameField = fieldSet[findpos];
    firstName = firstNameField.TextStorage();

    //Get the FirstName Text to firstNameText
    TPtrC firstNameText = firstName->Text();
    TInt firstNameTextLength = firstNameText.Length();

    //Store the FirstNameText to unsigned char
    unsigned char *encryptdata = new unsigned char;
    for( TInt item=0;item<firstNameTextLength;item++)
    {
    encryptdata[item] = (unsigned char)firstNameText[item];
    }

    unsigned char *DKey = new unsigned char [16]; //to store Key

    //Generate Key
    KeyGen(DKey, 16);

    //Passing the Key and Encrypting the FirstNameText
    Encrypt(encryptdata,dwDataLength,DKey);

    TBuf8<100>DataEncryption;
    DataEncryption.Copy(encryptdata);
    TBuf<100>DataAfterEncryption;
    DataAfterEncryption.Copy(DataEncryption);

    //Set the Encrypted text to FirstName
    firstName->SetTextL(DataAfterEncryption);

    //Allocate the FirstName Text to firstNameBuf
    firstNameBuf = firstName->Text().AllocLC();
    }

  2. #2
    Nokia Developer Champion
    Join Date
    Mar 2006
    Location
    Helsinki, Finland
    Posts
    8,344

    Smile Re: Iam totally in confusion .......Pls help me

    I have done it before few months.

    Check this Wiki Article: Encrypt-Decrypt Contact Database

    I hope it will help you at some extent.

    Kiran.

  3. #3
    Regular Contributor
    Join Date
    Mar 2007
    Posts
    165

    Re: Iam totally in confusion .......Pls help me

    Hai Kiran,
    Thanks for ur help.Iam able to get all the fields text and Encrypt .But in my application Iam using my own AES Algorithm to encrypt the data.

    Its Prototype:
    int Encrypt(unsigned char pbData[], unsigned long dwDataLen, const unsigned char*pbIV);

    So I try to get the Text from TPtrc to unsigned char*.Upto some extent its working but the emulator crashes with "User 42" Panic Error.

    CContactDatabase *contactDB = CContactDatabase::OpenL();
    CleanupStack::PushL(contactDB);
    TContactIter iter(*contactDB);
    TContactItemId aContactId;
    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();

    TInt FieldTextLength = name.Length();
    unsigned char *encryptdata = new unsigned char;
    for( TInt item=0;item<FieldTextLength;item++)
    {
    encryptdata[item] = (unsigned char)name[item];
    }

    Encrypt(encryptdata,dwDataLength,DKey);

    TBuf8<70>DataAfterEncryption;
    DataAfterEncryption.Copy(encryptdata);
    TBuf16<70> aValue;
    aValue.Copy(DataAfterEncryption);
    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);















  4. #4
    Nokia Developer Champion
    Join Date
    Mar 2006
    Location
    Helsinki, Finland
    Posts
    8,344

    Smile Re: Iam totally in confusion .......Pls help me

    Hi Jennie,
    Quote Originally Posted by jennie View Post
    ...Upto some extent its working but the emulator crashes with "User 42" Panic Error.
    Clean/Re-Build the project. Also debug your application. Finally deploy it on real device and then see the behavior.

    PS: Please take a backup of your contacts.

    Kiran.

  5. #5
    Regular Contributor
    Join Date
    Mar 2007
    Posts
    165

    Re: Iam totally in confusion .......Pls help me

    Yes Kiran,I Debug the code.But not understanding Why the emulator crashes.While Decrypting also Iam observing the same behaviour.

  6. #6
    Regular Contributor
    Join Date
    Mar 2007
    Posts
    165

    Re: Iam totally in confusion .......Pls help me

    Hai Kiran,
    I changed the line
    unsigned char *encryptdata = new unsigned char;

    to

    unsigned char *encryptdata = new unsigned char[100];

    now the "User 42" error is not occuring.Encryption and decryption is happenning.But after decryption the contact name is appended with some extra characters.
    (Suppose if the contact name is Raj Rani after decryption the name is becoming "RajXp Ranipppppppppp").Pls help me to solve this.

  7. #7
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,689

    Re: Iam totally in confusion .......Pls help me

    You should preserve the length of contact fields. Try Copy(encryptdata,FieldTextLength).

Similar Threads

  1. Replies: 15
    Last Post: 2006-08-28, 13:57
  2. File I/O confusion, please help...
    By alex80it in forum Symbian
    Replies: 2
    Last Post: 2006-02-07, 09:14
  3. pls help
    By viraj_turakhia in forum Symbian Tools & SDKs
    Replies: 0
    Last Post: 2004-12-13, 08:33
  4. pls help me pls..... (everybody read this).....need it urgently
    By Never_Die in forum Bluetooth Technology
    Replies: 0
    Last Post: 2004-11-30, 07:52

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×