Symbian3/PDK/Source/GUID-3385079E-84F4-534F-B937-BD3A568D71BC-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1.dita
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Symbian3/PDK/Source/GUID-3385079E-84F4-534F-B937-BD3A568D71BC-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1.dita Fri Jul 16 17:23:46 2010 +0100
@@ -0,0 +1,110 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
+<!-- This component and the accompanying materials are made available under the terms of the License
+"Eclipse Public License v1.0" which accompanies this distribution,
+and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
+<!-- Initial Contributors:
+ Nokia Corporation - initial contribution.
+Contributors:
+-->
+<!DOCTYPE concept
+ PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
+<concept id="GUID-3385079E-84F4-534F-B937-BD3A568D71BC-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1" xml:lang="en"><title>Password
+Based Encryption (PBE) example code</title><prolog><metadata><keywords/></metadata></prolog><conbody>
+<p>This example covers: </p>
+<ul>
+<li id="GUID-79A33AFC-B814-586D-93BC-D152C08DC7FD-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1-2-2-1"><p> <xref href="GUID-3385079E-84F4-534F-B937-BD3A568D71BC-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1.dita#GUID-3385079E-84F4-534F-B937-BD3A568D71BC-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1/GUID-29272282-2325-55CD-B583-E9E50D344C14-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1-2-5">Encrypting some data with a password and writing it to a file</xref> </p> </li>
+<li id="GUID-72DFC0FF-5E9E-581E-9589-544E349413D1-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1-2-2-2"><p> <xref href="GUID-3385079E-84F4-534F-B937-BD3A568D71BC-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1.dita#GUID-3385079E-84F4-534F-B937-BD3A568D71BC-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1/GUID-432A4D99-0AD1-5E1E-8E09-ED47FFC0C353-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1-2-6">Reading the data back from the file and decrypting it with the same password.</xref> </p> </li>
+</ul>
+<p> <i>This example code is for reference only - it would be better to use:</i> </p>
+<ul>
+<li id="GUID-450F0BEB-EA72-5A93-8722-CB10DF2DD0FC-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1-2-4-1"><p> <xref href="GUID-598B6698-3518-50CA-98DE-D85CB255BA2D-GENID-1-12-1-26-1-1-9-1-12-1-5-1.dita">Secure
+stream example code</xref> </p> </li>
+<li id="GUID-95E469DF-F6D4-5F1A-9A6F-A3759DC51308-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1-2-4-2"><p> <xref href="GUID-36BCDD1F-3713-5DF0-8D8A-CF093694B636-GENID-1-12-1-26-1-1-9-1-12-1-7-1.dita">Secure
+store example code</xref> </p> </li>
+</ul>
+<section id="GUID-29272282-2325-55CD-B583-E9E50D344C14-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1-2-5"><title>Encrypting
+data with a password and writing it to a file</title> <ul>
+<li id="GUID-92C03164-30E8-5B3E-91FA-667D344DC8A6-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1-2-5-2-1"><p> <xref href="GUID-9E3ADEB4-C000-525D-B220-2BAFF5DC6B0C-GENID-1-12-1-26-1-1-9-1-12-1-4-1.dita">Supporting
+code for this example</xref> </p> </li>
+</ul> <codeblock id="GUID-A2587573-A051-57DF-BC36-76585CB14F99-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1-2-5-3" xml:space="preserve">
+void CPBEExample::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);
+
+ // This is used to create a CPBEncryptor object
+ CPBEncryptor* encryptor = encryption->NewEncryptLC();
+
+ // Create a buffer of appropriate size to hold the ciphertext
+ HBufC8* ciphertextTemp = HBufC8::NewLC(encryptor->MaxFinalOutputLength(aInput.Length()));
+ TPtr8 ciphertext = ciphertextTemp->Des();
+
+ // Encrypt the input data into the ciphertext buffer
+ encryptor->ProcessFinalL(aInput, ciphertext);
+
+ // Store encryption data. This contains details of the encryption used (e.g.,
+ // cipher, key size) as well as things like the salt. This must be stored
+ // along with the encrypted data, otherwise it is not possible to decrypt it
+ // again!
+ writeStream << encryption->EncryptionData();
+
+ // Store the ciphertext
+ writeStream << ciphertext;
+
+ // Commit the stream
+ writeStream.CommitL();
+
+ // Free memory (writeStream, encryption, encryptor, ciphertextTemp)
+ CleanupStack::PopAndDestroy(4, &writeStream);
+ }
+ </codeblock> </section>
+<section id="GUID-432A4D99-0AD1-5E1E-8E09-ED47FFC0C353-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1-2-6"><title>Reading data
+from a file and decrypting it</title> <ul>
+<li id="GUID-70128A46-1AAB-59E8-9F03-68885091D54B-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1-2-6-2-1"><p> <xref href="GUID-9E3ADEB4-C000-525D-B220-2BAFF5DC6B0C-GENID-1-12-1-26-1-1-9-1-12-1-4-1.dita">Supporting
+code for this example</xref> </p> </li>
+</ul> <codeblock id="GUID-110E47B5-D63D-5041-ADC9-5A8DA1305712-GENID-1-12-1-26-1-1-11-1-1-6-1-5-1-3-1-2-6-3" xml:space="preserve">
+HBufC8* CPBEExample::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 file
+ CPBEncryptionData* encryptionData = CPBEncryptionData::NewLC(readStream);
+
+ // Recreate the CPBEncryptElement object, using the encryption data from the
+ // file and the user's password. This will leave with KErrBadPassphrase if
+ // the password is wrong.
+ CPBEncryptElement* encryption = CPBEncryptElement::NewLC(*encryptionData, aPassword);
+
+ // This is used to create a CPBDecryptor object
+ CPBDecryptor* decryptor = encryption->NewDecryptLC();
+
+ // Read the ciphertext
+ HBufC8* ciphertext = HBufC8::NewLC(readStream, KMaxTInt);
+
+ // Allocate a buffer for the plaintext (this will be returned to the caller)
+ HBufC8* plaintextBuf = HBufC8::NewLC(decryptor->MaxFinalOutputLength(ciphertext->Length()));
+ TPtr8 plaintext = plaintextBuf->Des();
+
+ // Decrypt the data
+ decryptor->ProcessFinalL(*ciphertext, plaintext);
+
+ // Free memory (readStream, encryptionData, encryption, decryptor, ciphertext, plaintext)
+ CleanupStack::Pop(plaintextBuf); // don't free this
+ CleanupStack::PopAndDestroy(5, &readStream);
+ CleanupStack::PushL(plaintextBuf);
+
+ // Return plaintext to the caller
+ return plaintextBuf;
+ }
+ </codeblock> </section>
+</conbody></concept>
\ No newline at end of file