Symbian3/SDK/Source/GUID-598B6698-3518-50CA-98DE-D85CB255BA2D.dita
changeset 8 ae94777fff8f
equal deleted inserted replaced
7:51a74ef9ed63 8:ae94777fff8f
       
     1 <?xml version="1.0" encoding="utf-8"?>
       
     2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
       
     3 <!-- This component and the accompanying materials are made available under the terms of the License 
       
     4 "Eclipse Public License v1.0" which accompanies this distribution, 
       
     5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
       
     6 <!-- Initial Contributors:
       
     7     Nokia Corporation - initial contribution.
       
     8 Contributors: 
       
     9 -->
       
    10 <!DOCTYPE concept
       
    11   PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
       
    12 <concept xml:lang="en" id="GUID-598B6698-3518-50CA-98DE-D85CB255BA2D"><title>Secure stream encryption example code</title><prolog><metadata><keywords/></metadata></prolog><conbody><p>This example covers: </p> <ul><li id="GUID-3F36B509-4E9F-5F4C-A374-993714418DEF"><p> <xref href="GUID-598B6698-3518-50CA-98DE-D85CB255BA2D.dita#GUID-598B6698-3518-50CA-98DE-D85CB255BA2D/GUID-C27DAD65-463E-5CC6-A7D1-58F5DB3F279E">Encrypting some data with a password and writing it to a file</xref>  </p> </li> <li id="GUID-BE3A3FB7-E5C6-5488-800A-6BDBCD693C99"><p> <xref href="GUID-598B6698-3518-50CA-98DE-D85CB255BA2D.dita#GUID-598B6698-3518-50CA-98DE-D85CB255BA2D/GUID-75B9241A-82D2-52F1-BDCE-B8E329F72BDE">Reading the data back from the file and decrypting it with the same password.</xref>  </p> </li> </ul> <p>Also, see: </p> <ul><li id="GUID-B8CC38F9-AAD9-5FCF-BE9E-F0526443AEFC"><p> <xref href="GUID-3385079E-84F4-534F-B937-BD3A568D71BC.dita">PBE example code</xref>  </p> </li> <li id="GUID-9952F1C6-982E-54EE-B8E2-29B99141106B"><p> <xref href="GUID-36BCDD1F-3713-5DF0-8D8A-CF093694B636.dita">Secure store example code</xref>  </p> </li> </ul> <section id="GUID-C27DAD65-463E-5CC6-A7D1-58F5DB3F279E"><title>Encrypting data with a password and writing it to a file</title> <ul><li id="GUID-0CF095D9-2387-5DAA-B599-9255C142D5AB"><p> <xref href="GUID-267D1E20-002E-53C7-8AE5-E063206C1562.dita">Supporting code for this example</xref>  </p> </li> </ul> <codeblock id="GUID-D41E94E7-F29F-57F4-9DC2-5BFE018BC7C8" xml:space="preserve">
       
    13 void CSecureStreamExample::WriteEncryptedDataL(const TDesC8&amp; aInput, const TDesC&amp; aFilename, const TDesC&amp; aPassword)
       
    14          {
       
    15          // Open a stream to the output file
       
    16          RFileWriteStream writeStream;
       
    17          User::LeaveIfError(writeStream.Replace(iFs, aFilename, EFileShareExclusive | EFileWrite));
       
    18          CleanupClosePushL(writeStream);
       
    19                 
       
    20          // Create a CPBEncryptElement object, passing details of the encryption we
       
    21          // are using and the user's password
       
    22          CPBEncryptElement* encryption = CPBEncryptElement::NewLC(aPassword, ECipherDES_CBC);
       
    23                 
       
    24          // Store encryption data as the first thing in the stream
       
    25          writeStream &lt;&lt; encryption-&gt;EncryptionData();
       
    26                 
       
    27          // Create an REncryptStream - this wraps the write stream and encrypts
       
    28          // everything written to it
       
    29          REncryptStream encStream;
       
    30          encStream.OpenL(writeStream, *encryption);
       
    31          CleanupClosePushL(encStream);
       
    32                 
       
    33          // Now we can simply write the plaintext to the encrypt stream - it gets
       
    34          // encrypted automatically
       
    35          encStream &lt;&lt; aInput;
       
    36                 
       
    37          // Commit the stream
       
    38          encStream.CommitL();
       
    39                 
       
    40          // Free memory (writeStream, encryption, encStream)
       
    41          CleanupStack::PopAndDestroy(3, &amp;writeStream);
       
    42          }
       
    43             </codeblock> </section> <section id="GUID-75B9241A-82D2-52F1-BDCE-B8E329F72BDE"><title>Reading data from a file and decrypting it</title> <ul><li id="GUID-952B8F2D-DBA3-5338-BC7D-CA835EA9BA29"><p> <xref href="GUID-267D1E20-002E-53C7-8AE5-E063206C1562.dita">Supporting code for this example</xref>  </p> </li> </ul> <codeblock id="GUID-93A9C97B-E085-5171-A524-2E34BF726000" xml:space="preserve">
       
    44 HBufC8* CSecureStreamExample::ReadEncryptedDataLC(const TDesC&amp; aFilename, const TDesC&amp; aPassword)
       
    45          {
       
    46          // Open a stream to the input file
       
    47          RFileReadStream readStream;
       
    48          User::LeaveIfError(readStream.Open(iFs, aFilename, EFileRead));
       
    49          CleanupClosePushL(readStream);
       
    50 
       
    51          // Read the encryption data from the stream
       
    52          CPBEncryptionData* encryptionData = CPBEncryptionData::NewLC(readStream);
       
    53          
       
    54          // Recreate the CPBEncryptElement object, using the encryption data from the
       
    55          // file and the user's password
       
    56          CPBEncryptElement* encryption = CPBEncryptElement::NewLC(*encryptionData, aPassword);
       
    57 
       
    58          // Create an RDecryptStream based on the read stream - this decrypts
       
    59          // everything we read from it
       
    60          RDecryptStream decStream;
       
    61          decStream.OpenL(readStream, *encryption);
       
    62          CleanupClosePushL(decStream);
       
    63     
       
    64          // Now we can read the plaintext straight from the stream
       
    65          HBufC8* plaintext = HBufC8::NewL(decStream, KMaxTInt);
       
    66              
       
    67          // Free memory (readStream, encryptionData, encryption, decStream)
       
    68          CleanupStack::PopAndDestroy(4, &amp;readStream);
       
    69          CleanupStack::PushL(plaintext);
       
    70     
       
    71          // Return plaintext to the caller
       
    72          return plaintext;
       
    73          }
       
    74         </codeblock> </section> </conbody></concept>