|
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 id="GUID-3385079E-84F4-534F-B937-BD3A568D71BC" xml:lang="en"><title>Password |
|
13 Based Encryption (PBE) example code</title><prolog><metadata><keywords/></metadata></prolog><conbody> |
|
14 <p>This example covers: </p> |
|
15 <ul> |
|
16 <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> |
|
17 <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> |
|
18 </ul> |
|
19 <p> <i>This example code is for reference only - it would be better to use:</i> </p> |
|
20 <ul> |
|
21 <li id="GUID-450F0BEB-EA72-5A93-8722-CB10DF2DD0FC"><p> <xref href="GUID-598B6698-3518-50CA-98DE-D85CB255BA2D.dita">Secure |
|
22 stream example code</xref> </p> </li> |
|
23 <li id="GUID-95E469DF-F6D4-5F1A-9A6F-A3759DC51308"><p> <xref href="GUID-36BCDD1F-3713-5DF0-8D8A-CF093694B636.dita">Secure |
|
24 store example code</xref> </p> </li> |
|
25 </ul> |
|
26 <section id="GUID-29272282-2325-55CD-B583-E9E50D344C14"><title>Encrypting |
|
27 data with a password and writing it to a file</title> <ul> |
|
28 <li id="GUID-92C03164-30E8-5B3E-91FA-667D344DC8A6"><p> <xref href="GUID-9E3ADEB4-C000-525D-B220-2BAFF5DC6B0C.dita">Supporting |
|
29 code for this example</xref> </p> </li> |
|
30 </ul> <codeblock id="GUID-A2587573-A051-57DF-BC36-76585CB14F99" xml:space="preserve"> |
|
31 void CPBEExample::WriteEncryptedDataL(const TDesC8& aInput, const TDesC& aFilename, const TDesC& aPassword) |
|
32 { |
|
33 // Open a stream to the output file |
|
34 RFileWriteStream writeStream; |
|
35 User::LeaveIfError(writeStream.Replace(iFs, aFilename, EFileShareExclusive | EFileWrite)); |
|
36 CleanupClosePushL(writeStream); |
|
37 |
|
38 // Create a CPBEncryptElement object, passing details of the encryption we |
|
39 // are using and the user's password |
|
40 CPBEncryptElement* encryption = CPBEncryptElement::NewLC(aPassword, ECipherDES_CBC); |
|
41 |
|
42 // This is used to create a CPBEncryptor object |
|
43 CPBEncryptor* encryptor = encryption->NewEncryptLC(); |
|
44 |
|
45 // Create a buffer of appropriate size to hold the ciphertext |
|
46 HBufC8* ciphertextTemp = HBufC8::NewLC(encryptor->MaxFinalOutputLength(aInput.Length())); |
|
47 TPtr8 ciphertext = ciphertextTemp->Des(); |
|
48 |
|
49 // Encrypt the input data into the ciphertext buffer |
|
50 encryptor->ProcessFinalL(aInput, ciphertext); |
|
51 |
|
52 // Store encryption data. This contains details of the encryption used (e.g., |
|
53 // cipher, key size) as well as things like the salt. This must be stored |
|
54 // along with the encrypted data, otherwise it is not possible to decrypt it |
|
55 // again! |
|
56 writeStream << encryption->EncryptionData(); |
|
57 |
|
58 // Store the ciphertext |
|
59 writeStream << ciphertext; |
|
60 |
|
61 // Commit the stream |
|
62 writeStream.CommitL(); |
|
63 |
|
64 // Free memory (writeStream, encryption, encryptor, ciphertextTemp) |
|
65 CleanupStack::PopAndDestroy(4, &writeStream); |
|
66 } |
|
67 </codeblock> </section> |
|
68 <section id="GUID-432A4D99-0AD1-5E1E-8E09-ED47FFC0C353"><title>Reading data |
|
69 from a file and decrypting it</title> <ul> |
|
70 <li id="GUID-70128A46-1AAB-59E8-9F03-68885091D54B"><p> <xref href="GUID-9E3ADEB4-C000-525D-B220-2BAFF5DC6B0C.dita">Supporting |
|
71 code for this example</xref> </p> </li> |
|
72 </ul> <codeblock id="GUID-110E47B5-D63D-5041-ADC9-5A8DA1305712" xml:space="preserve"> |
|
73 HBufC8* CPBEExample::ReadEncryptedDataLC(const TDesC& aFilename, const TDesC& aPassword) |
|
74 { |
|
75 // Open a stream to the input file |
|
76 RFileReadStream readStream; |
|
77 User::LeaveIfError(readStream.Open(iFs, aFilename, EFileRead)); |
|
78 CleanupClosePushL(readStream); |
|
79 |
|
80 // Read the encryption data from the file |
|
81 CPBEncryptionData* encryptionData = CPBEncryptionData::NewLC(readStream); |
|
82 |
|
83 // Recreate the CPBEncryptElement object, using the encryption data from the |
|
84 // file and the user's password. This will leave with KErrBadPassphrase if |
|
85 // the password is wrong. |
|
86 CPBEncryptElement* encryption = CPBEncryptElement::NewLC(*encryptionData, aPassword); |
|
87 |
|
88 // This is used to create a CPBDecryptor object |
|
89 CPBDecryptor* decryptor = encryption->NewDecryptLC(); |
|
90 |
|
91 // Read the ciphertext |
|
92 HBufC8* ciphertext = HBufC8::NewLC(readStream, KMaxTInt); |
|
93 |
|
94 // Allocate a buffer for the plaintext (this will be returned to the caller) |
|
95 HBufC8* plaintextBuf = HBufC8::NewLC(decryptor->MaxFinalOutputLength(ciphertext->Length())); |
|
96 TPtr8 plaintext = plaintextBuf->Des(); |
|
97 |
|
98 // Decrypt the data |
|
99 decryptor->ProcessFinalL(*ciphertext, plaintext); |
|
100 |
|
101 // Free memory (readStream, encryptionData, encryption, decryptor, ciphertext, plaintext) |
|
102 CleanupStack::Pop(plaintextBuf); // don't free this |
|
103 CleanupStack::PopAndDestroy(5, &readStream); |
|
104 CleanupStack::PushL(plaintextBuf); |
|
105 |
|
106 // Return plaintext to the caller |
|
107 return plaintextBuf; |
|
108 } |
|
109 </codeblock> </section> |
|
110 </conbody></concept> |