diff -r 000000000000 -r 2c201484c85f crypto/weakcrypto/docs/secure_stream_example_code.dox --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/crypto/weakcrypto/docs/secure_stream_example_code.dox Wed Jul 08 11:25:26 2009 +0100 @@ -0,0 +1,90 @@ +/** + @page secure_stream_example_code Secure stream example code + + This example covers: + -# @ref stream_example_1 "Encrypting some data with a password and writing it to a file" + -# @ref stream_example_2 "Reading the data back from the file and decrypting it with the same password". + + Also, see: + - @ref pbe_example_code + - @ref secure_store_example_code + +
+ + @ref secure_stream_example_code_support "Supporting code for this example" + +@anchor stream_example_1 +@code +/* + * Encrypt data with password and write it to a file. + */ +void CSecureStreamExample::WriteEncryptedDataL(const TDesC8& aInput, const TDesC& aFilename, const TDesC& aPassword) + { + // Open a stream to the output file + RFileWriteStream writeStream; + User::LeaveIfError(writeStream.Replace(iFs, aFilename, EFileShareExclusive | EFileWrite)); + CleanupClosePushL(writeStream); + + // Create a CPBEncryptElement object, passing details of the encryption we + // are using and the user's password + CPBEncryptElement* encryption = CPBEncryptElement::NewLC(aPassword, ECipherDES_CBC); + + // Store encryption data as the first thing in the stream + writeStream << encryption->EncryptionData(); + + // Create an REncryptStream - this wraps the write stream and encrypts + // everything written to it + REncryptStream encStream; + encStream.OpenL(writeStream, *encryption); + CleanupClosePushL(encStream); + + // Now we can simply write the plaintext to the encrypt stream - it gets + // encrypted automatically + encStream << aInput; + + // Commit the stream + encStream.CommitL(); + + // Free memory (writeStream, encryption, encStream) + CleanupStack::PopAndDestroy(3, &writeStream); + } +@endcode + +@anchor stream_example_2 +@code +/* + * Read data from file and decrypt it. + */ +HBufC8* CSecureStreamExample::ReadEncryptedDataLC(const TDesC& aFilename, const TDesC& aPassword) + { + // Open a stream to the input file + RFileReadStream readStream; + User::LeaveIfError(readStream.Open(iFs, aFilename, EFileRead)); + CleanupClosePushL(readStream); + + // Read the encryption data from the stream + CPBEncryptionData* encryptionData = CPBEncryptionData::NewLC(readStream); + + // Recreate the CPBEncryptElement object, using the encryption data from the + // file and the user's password + CPBEncryptElement* encryption = CPBEncryptElement::NewLC(*encryptionData, aPassword); + + // Create an RDecryptStream based on the read stream - this decrypts + // everything we read from it + RDecryptStream decStream; + decStream.OpenL(readStream, *encryption); + CleanupClosePushL(decStream); + + // Now we can read the plaintext straight from the stream + HBufC8* plaintext = HBufC8::NewL(decStream, KMaxTInt); + + // Free memory (readStream, encryptionData, encryption, decStream) + CleanupStack::PopAndDestroy(4, &readStream); + CleanupStack::PushL(plaintext); + + // Return plaintext to the caller + return plaintext; + } +@endcode + +*/ \ No newline at end of file