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
 
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);
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:
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:
Forum posts: 1379
Try:
TPtrC aPtr = smsMtm->Body().Read(0,4);
// whats in aPtr at this point?
_LIT(KGDSMSTag, "GDSM");
if (aPtr.CompareC(KGDSMSTag)==0)
{
}
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?
Forum posts: 1379
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
I'm learning symbian and i dont understand all these types related
stuff
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);
Forum posts: 1379
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:
_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:
_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
BUNDLE OF THANKS
rs << Ktext();
rs.CommitL();
Can i simply see it (encrypted) in info dlg?
Forum posts: 1379
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
i guess one way is just opening file somewhere again and reading it
(just reading without doing any decryption)
Forum posts: 1379
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
or may be i should use smth else than symbian's encryption Api?
Forum posts: 1379
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
Ans: i want to send encrypted data.
----------------------------------------------------------------------------------