|
1 /** |
|
2 @page pbe_example_code PBE example code |
|
3 |
|
4 This example covers: |
|
5 -# @ref pbe_example_1 "Encrypting some data with a password and writing it to a file" |
|
6 -# @ref pbe_example_2 "Reading the data back from the file and decrypting it with the same password". |
|
7 |
|
8 <i>This example code is really for reference only - it would be better to use:</i> |
|
9 - @ref secure_stream_example_code |
|
10 - @ref secure_store_example_code |
|
11 |
|
12 <hr> |
|
13 |
|
14 - @ref pbe_example_code_support "Supporting code for this example". |
|
15 |
|
16 @anchor pbe_example_1 |
|
17 @code |
|
18 /* |
|
19 * Encrypt data with password and write it to a file. |
|
20 */ |
|
21 void CPBEExample::WriteEncryptedDataL(const TDesC8& aInput, const TDesC& aFilename, const TDesC& aPassword) |
|
22 { |
|
23 // Open a stream to the output file |
|
24 RFileWriteStream writeStream; |
|
25 User::LeaveIfError(writeStream.Replace(iFs, aFilename, EFileShareExclusive | EFileWrite)); |
|
26 CleanupClosePushL(writeStream); |
|
27 |
|
28 // Create a CPBEncryptElement object, passing details of the encryption we |
|
29 // are using and the user's password |
|
30 CPBEncryptElement* encryption = CPBEncryptElement::NewLC(aPassword, ECipherDES_CBC); |
|
31 |
|
32 // This is used to create a CPBEncryptor object |
|
33 CPBEncryptor* encryptor = encryption->NewEncryptLC(); |
|
34 |
|
35 // Create a buffer of appropriate size to hold the ciphertext |
|
36 HBufC8* ciphertextTemp = HBufC8::NewLC(encryptor->MaxFinalOutputLength(aInput.Length())); |
|
37 TPtr8 ciphertext = ciphertextTemp->Des(); |
|
38 |
|
39 // Encrypt the input data into the ciphertext buffer |
|
40 encryptor->ProcessFinalL(aInput, ciphertext); |
|
41 |
|
42 // Store encryption data. This contains details of the encryption used (e.g., |
|
43 // cipher, key size) as well as things like the salt. This must be stored |
|
44 // along with the encrypted data, otherwise it is not possible to decrypt it |
|
45 // again! |
|
46 writeStream << encryption->EncryptionData(); |
|
47 |
|
48 // Store the ciphertext |
|
49 writeStream << ciphertext; |
|
50 |
|
51 // Commit the stream |
|
52 writeStream.CommitL(); |
|
53 |
|
54 // Free memory (writeStream, encryption, encryptor, ciphertextTemp) |
|
55 CleanupStack::PopAndDestroy(4, &writeStream); |
|
56 } |
|
57 @endcode |
|
58 |
|
59 @anchor pbe_example_2 |
|
60 @code |
|
61 /* |
|
62 * Read data from file and decrypt it. |
|
63 */ |
|
64 HBufC8* CPBEExample::ReadEncryptedDataLC(const TDesC& aFilename, const TDesC& aPassword) |
|
65 { |
|
66 // Open a stream to the input file |
|
67 RFileReadStream readStream; |
|
68 User::LeaveIfError(readStream.Open(iFs, aFilename, EFileRead)); |
|
69 CleanupClosePushL(readStream); |
|
70 |
|
71 // Read the encryption data from the file |
|
72 CPBEncryptionData* encryptionData = CPBEncryptionData::NewLC(readStream); |
|
73 |
|
74 // Recreate the CPBEncryptElement object, using the encryption data from the |
|
75 // file and the user's password. This will leave with KErrBadPassphrase if |
|
76 // the password is wrong. |
|
77 CPBEncryptElement* encryption = CPBEncryptElement::NewLC(*encryptionData, aPassword); |
|
78 |
|
79 // This is used to create a CPBDecryptor object |
|
80 CPBDecryptor* decryptor = encryption->NewDecryptLC(); |
|
81 |
|
82 // Read the ciphertext |
|
83 HBufC8* ciphertext = HBufC8::NewLC(readStream, KMaxTInt); |
|
84 |
|
85 // Allocate a buffer for the plaintext (this will be returned to the caller) |
|
86 HBufC8* plaintextBuf = HBufC8::NewLC(decryptor->MaxFinalOutputLength(ciphertext->Length())); |
|
87 TPtr8 plaintext = plaintextBuf->Des(); |
|
88 |
|
89 // Decrypt the data |
|
90 decryptor->ProcessFinalL(*ciphertext, plaintext); |
|
91 |
|
92 // Free memory (readStream, encryptionData, encryption, decryptor, ciphertext, plaintext) |
|
93 CleanupStack::Pop(plaintextBuf); // don't free this |
|
94 CleanupStack::PopAndDestroy(5, &readStream); |
|
95 CleanupStack::PushL(plaintextBuf); |
|
96 |
|
97 // Return plaintext to the caller |
|
98 return plaintextBuf; |
|
99 } |
|
100 |
|
101 @endcode |
|
102 |
|
103 */ |