diff -r 51a74ef9ed63 -r ae94777fff8f Symbian3/SDK/Source/GUID-3385079E-84F4-534F-B937-BD3A568D71BC.dita --- /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 @@ + + + + + +Password +Based Encryption (PBE) example code +

This example covers:

+ +

This example code is for reference only - it would be better to use:

+ +
Encrypting +data with a password and writing it to a file
    +
  • Supporting +code for this example

  • +
+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); + } +
+
Reading data +from a file and decrypting it
    +
  • Supporting +code for this example

  • +
+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; + } +
+
\ No newline at end of file