Symbian3/SDK/Source/GUID-3385079E-84F4-534F-B937-BD3A568D71BC.dita
changeset 8 ae94777fff8f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Symbian3/SDK/Source/GUID-3385079E-84F4-534F-B937-BD3A568D71BC.dita	Fri Jun 11 12:39:03 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" 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"><p> <xref href="GUID-3385079E-84F4-534F-B937-BD3A568D71BC.dita#GUID-3385079E-84F4-534F-B937-BD3A568D71BC/GUID-29272282-2325-55CD-B583-E9E50D344C14">Encrypting some data with a password and writing it to a file</xref>  </p> </li>
+<li id="GUID-72DFC0FF-5E9E-581E-9589-544E349413D1"><p> <xref href="GUID-3385079E-84F4-534F-B937-BD3A568D71BC.dita#GUID-3385079E-84F4-534F-B937-BD3A568D71BC/GUID-432A4D99-0AD1-5E1E-8E09-ED47FFC0C353">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"><p> <xref href="GUID-598B6698-3518-50CA-98DE-D85CB255BA2D.dita">Secure
+stream example code</xref>  </p> </li>
+<li id="GUID-95E469DF-F6D4-5F1A-9A6F-A3759DC51308"><p> <xref href="GUID-36BCDD1F-3713-5DF0-8D8A-CF093694B636.dita">Secure
+store example code</xref>  </p> </li>
+</ul>
+<section id="GUID-29272282-2325-55CD-B583-E9E50D344C14"><title>Encrypting
+data with a password and writing it to a file</title> <ul>
+<li id="GUID-92C03164-30E8-5B3E-91FA-667D344DC8A6"><p> <xref href="GUID-9E3ADEB4-C000-525D-B220-2BAFF5DC6B0C.dita">Supporting
+code for this example</xref>  </p> </li>
+</ul> <codeblock id="GUID-A2587573-A051-57DF-BC36-76585CB14F99" xml:space="preserve">
+void CPBEExample::WriteEncryptedDataL(const TDesC8&amp; aInput, const TDesC&amp; aFilename, const TDesC&amp; 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-&gt;NewEncryptLC();
+
+         // Create a buffer of appropriate size to hold the ciphertext
+         HBufC8* ciphertextTemp = HBufC8::NewLC(encryptor-&gt;MaxFinalOutputLength(aInput.Length()));
+         TPtr8 ciphertext = ciphertextTemp-&gt;Des();         
+
+         // Encrypt the input data into the ciphertext buffer
+         encryptor-&gt;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 &lt;&lt; encryption-&gt;EncryptionData();
+
+         // Store the ciphertext
+         writeStream &lt;&lt; ciphertext;
+
+         // Commit the stream
+         writeStream.CommitL();
+
+         // Free memory (writeStream, encryption, encryptor, ciphertextTemp)
+         CleanupStack::PopAndDestroy(4, &amp;writeStream);
+         }
+            </codeblock> </section>
+<section id="GUID-432A4D99-0AD1-5E1E-8E09-ED47FFC0C353"><title>Reading data
+from a file and decrypting it</title> <ul>
+<li id="GUID-70128A46-1AAB-59E8-9F03-68885091D54B"><p> <xref href="GUID-9E3ADEB4-C000-525D-B220-2BAFF5DC6B0C.dita">Supporting
+code for this example</xref>  </p> </li>
+</ul> <codeblock id="GUID-110E47B5-D63D-5041-ADC9-5A8DA1305712" xml:space="preserve">
+HBufC8* CPBEExample::ReadEncryptedDataLC(const TDesC&amp; aFilename, const TDesC&amp; 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-&gt;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-&gt;MaxFinalOutputLength(ciphertext-&gt;Length()));
+         TPtr8 plaintext = plaintextBuf-&gt;Des();
+
+         // Decrypt the data
+         decryptor-&gt;ProcessFinalL(*ciphertext, plaintext);
+
+         // Free memory (readStream, encryptionData, encryption, decryptor, ciphertext, plaintext)
+         CleanupStack::Pop(plaintextBuf); // don't free this
+         CleanupStack::PopAndDestroy(5, &amp;readStream);
+         CleanupStack::PushL(plaintextBuf);
+
+         // Return plaintext to the caller
+         return plaintextBuf;
+         }
+        </codeblock> </section>
+</conbody></concept>
\ No newline at end of file