problem with comparing str

Login to reply to this topic.
Tue, 2005-05-10 07:20
Anonymous
Forum posts: 2019
Hi all,

I'm using one example for sending sms which i found here.
can some1 tell whats wrong with this Compare here?


_LIT(KGDSMSTag, "GDSM");
if (smsMtm->Body().Read(0,4).Compare(KGDSMSTag)==0)
{
//never comes here even if sms has GDSM
}

Tue, 2005-05-10 09:02
Joined: 2004-07-28
Forum posts: 1379
problem with comparing str
The SMS will have to have the first four chars exactly as GDSM for that to work - case and all.

Try:

TPtrC aPtr = smsMtm->Body().Read(0,4);
// whats in aPtr at this point?

_LIT(KGDSMSTag, "GDSM");
if (aPtr.CompareC(KGDSMSTag)==0)
{
}

didster

Tue, 2005-05-10 10:43
chishti_hameed (not verified)
Forum posts: 2019
:)
thanks for ur reply didster..
I did copy   smsMtm->Body().Read(0,4); into richtext and did
fix all.

i've another Question: I'm getting error when trying to do follwoing.

------------------------------------------------------------
_LIT(Ktext, " :test string");
RWriteStream outputStream;
outputStream.WriteL(Ktext);

How this thing should be to make it work?
Tue, 2005-05-10 10:47
Joined: 2004-07-28
Forum posts: 1379
problem with comparing str
Nooooo.

Not as simple as that I'm affraid.

First, are you really trying to do what it looks like you are trying to do?  That is write :test string in _external_ format?  That is with various codes which tell the stream API whats in the file?  So it looks like a binary file if you open it with notepad/what ever?  And then you plan to read it back with RReadStream?

Or, are you making the mistake many people make and trying to use RWriteStream to write out the text in plain format?  That is a plain text file which can be human read in notepad?

didster

Tue, 2005-05-10 10:55
chishti_hameed (not verified)
Forum posts: 2019
...
well all what i want to do is  "encrypt"  "Ktext" .

I'm learning symbian and i dont understand all these types related
stuff Sad
Tue, 2005-05-10 10:57
chishti_hameed (not verified)
Forum posts: 2019
.
to encrypt i'm using code available in sdk sample "cipher"
Tue, 2005-05-10 11:00
chishti_hameed (not verified)
Forum posts: 2019
here is code:
// Set up the encryption engine with the user supplied password
   CBoundedSecurityBase* encrypter = Security::NewL();
   CleanupStack::PushL(encrypter);
   encrypter->SetL(KNullDesC, KPwd);

// Write the security data, used in the decryption process to verify the password
   RWriteStream outputStream;
   outputStream.WriteL(KtextToEncrypt);
   CleanupClosePushL(outputStream);
              // must use streaming api not WriteL
   outputStream << encrypter->SecurityData();

   // Write the encrypted data
   REncryptStream encryptedStream;
   encryptedStream.AttachLC(outputStream, *encrypter, KNullDesC8);
   encryptedStream << KtextToEncrypt;
   encryptedStream.Close();
   encryptedStream.Pop();

   // Clean up
   CleanupStack::PopAndDestroy(); // close outputStream
   CleanupStack::PopAndDestroy(encrypter);
Tue, 2005-05-10 11:07
Joined: 2004-07-28
Forum posts: 1379
problem with comparing str
Ok.

Well, the stream API is the way to go.  Basically, RWriteStream provides a generic way of writting data to a "stream".  That stream can be anything really, memory, a file etc etc.  But the API to write to it, is the same.  It also provides the code that puts the data into external format so it can be read back in reliably, and things like that.

You can't just construct a RWriteStream and then call WriteL, as you have done.  You must "connect" it to something, so it knows where to put the data.

Lets go for the plain text file to start with, not encrypted.  You'd write your data out like this:

Code:

_LIT(Ktext, " :test string");
_LIT(KDefaultFileName, "C:\\afile.dat");

RFs aFs;
User::LeaveIfError(aFs.Connect());
CleanupClosePushL(aFs);
aFs.MkDirAll(KDefaultFileName);

RFileWriteStream rs;
User::LeaveIfError(rs.Replace(aFs, KDefaultFileName, EFileWrite));
CleanupClosePushL(rs);

rs << Ktext();
rs.CommitL();

CleanupStack::PopAndDestroy(&rs);
CleanupStack::PopAndDestroy(&aFs);


Here, we use a RFileWriteStream to write the data to a file.  
RWriteStream comes into play when the object on the right side of << (in this case, KText) is one of your own.  In this case, you provide a ExternalizeL and InternalizeL method, which the system will call, passing in the stream to write to.  In this case, it will be a RFileWriteStream (RWriteStream is a base of RFileWriteStream), but it could be any of the stream classes - RDesWriteStream for example to write to memory.

As for encrypting it, simple.  The great thing about the stream API is you can "insert" things, so data gets passed from one block to the next etc.  Here is an example:

Code:

_LIT(Ktext, " :test string");
_LIT(KDefaultFileName, "C:\\afile.dat");
_LIT(KPass, "-@@dlka");

RFs aFs;
User::LeaveIfError(aFs.Connect());
CleanupClosePushL(aFs);

aFs.MkDirAll(KDefaultFileName);

RFileWriteStream rs;
User::LeaveIfError(rs.Replace(aFs, KDefaultFileName, EFileWrite));
CleanupClosePushL(rs);

CBoundedSecurityBase* encrypter = Security::NewL();
CleanupStack::PushL(encrypter);

encrypter->SetL(KNullDesC, KPass);

rs << encrypter->SecurityData();

rs.CommitL();

REncryptStream encryptedFileStream;
encryptedFileStream.AttachLC(rs, *encrypter, KNullDesC8);

encryptedFileStream << Ktext();
encryptedFileStream.CommitL();

CleanupStack::PopAndDestroy(&encryptedFileStream);
CleanupStack::PopAndDestroy(encrypter);
CleanupStack::PopAndDestroy(&rs);
CleanupStack::PopAndDestroy(&aFs);


I hope that clears it up abit.  I haven't shown you how to load stuff back, but hopefully you can figure that out (thing RFileReadStream...)

didster

Tue, 2005-05-10 11:22
chishti_hameed (not verified)
Forum posts: 2019
.
OK its much clear now Smiley

BUNDLE OF THANKS Smiley
Tue, 2005-05-10 11:44
chishti_hameed (not verified)
Forum posts: 2019
btw
does "Ktext();" has now encrypted data here?

rs << Ktext();
rs.CommitL();

Can i simply see it (encrypted) in info dlg?
Tue, 2005-05-10 11:56
Joined: 2004-07-28
Forum posts: 1379
problem with comparing str
Don't get your question....

I think you mean "does Ktext get encrypted" - no, it doesn't.

What gets put into the stream is KText encrypted.

If you want to see it, encrypted, open the file C:\\afile.dat in a hex editor.

Of course, you could read it back out again, but it will be decrypted on the fly as it comes back out.

didster

Tue, 2005-05-10 12:03
chishti_hameed (not verified)
Forum posts: 2019
:P
so is there any way to see encrypted data without a hex editor? Smiley

i guess one way is just opening file somewhere again and reading it
(just reading without doing any decryption) Huh?
Tue, 2005-05-10 12:04
Joined: 2004-07-28
Forum posts: 1379
problem with comparing str
You could do that, Y I don't know, but you could.

You'll get more than the encrypted data back though - there will be:

<stream id><varous markers><SecurityData><your text encrypted><end markers>

In the file.

didster

Tue, 2005-05-10 12:09
chishti_hameed (not verified)
Forum posts: 2019
..
anyother better way doing this?

or may be i should use smth else than symbian's encryption Api?
Tue, 2005-05-10 13:37
Joined: 2004-07-28
Forum posts: 1379
problem with comparing str
I'm not sure I see the point of a "better" way.  Why would you want to view/show the user the encrypted data?

Well, you could use a RDesWriteStream and write the results (only the call of rs << Ktext()) to a descriptor.

Or, as you say you could use a 3rd pary encryption API.

didster

Tue, 2005-05-10 13:50
chishti_hameed (not verified)
Forum posts: 2019
ans
Q: ""Why would you want to view/show the user the encrypted data?""

Ans: i want to send encrypted data.
----------------------------------------------------------------------------------
  • Login to reply to this topic.