diff -r 5072524fcc79 -r 80ef3a206772 Symbian3/PDK/Source/GUID-D2D17EF9-FFC6-5FBD-A992-55746A12B625-GENID-1-12-1-26-1-1-9-1-5-1-6-1.dita --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Symbian3/PDK/Source/GUID-D2D17EF9-FFC6-5FBD-A992-55746A12B625-GENID-1-12-1-26-1-1-9-1-5-1-6-1.dita Fri Jul 16 17:23:46 2010 +0100 @@ -0,0 +1,103 @@ + + + + + +Basic +encryption and decryption using a symmetric cipher +

This example demonstrates how to encrypt and decrypt a small string of +data using a symmetric cipher. Firstly, an encryption operation is performed +on the data. Following this a decryption operation is performed on the encrypted +data. Finally a check is made on the decrypted data for equality with the +original data which tests that the encrypt and decrypt operations have maintained +data integrity.

+#include <legacyselector.h> +#include <cryptosymmetriccipherapi.h> +#include <keys.h> + +using namespace CryptoSpi; + +//Create a new CCryptoParams object to encapsulate the secret key string and key type +CCryptoParams* params = CCryptoParams::NewLC(); + +//Add the secret key to the CCryptoParams object by calling the CCryptoParams::AddL() method, +//passing in the key string and appropriate key parameter UID +params->AddL( _L8("12345678"), KSymmetricKeyParameterUid); + +//Create a key object (CKey) by passing in an instance of TKeyProperty and the previously +//created CCryptoParams object containing the secret key. +TKeyProperty keyProperty = {KDesUid, KNullUid, KSymmetricKey, KNonEmbeddedKeyUid}; +CKey* key=CKey::NewL(keyProperty, *params); +CleanupStack::PushL(key); +//Create and initialize a pointer for the Symmetric cipher implementation object +CSymmetricCipher* impl = NULL; + +//Create a symmetric cipher passing in the appropriate CKey object +TRAPD(err, CSymmetricCipherFactory::CreateSymmetricCipherL + (impl, + KDesUid, + *key, + KCryptoModeEncryptUid, // This can be either encrypt or decrypt + KOperationModeECBUid, // Use "electronic code book" mode + KPaddingModeNoneUid, // This is the type of padding to be used + NULL)); + +//Check the return value and for non NULL +if (impl && (err == KErrNone)) + { + //Create a buffer holding the data to be encrypted + TBuf8<8> srcData(_L8("12345678")); + + //Create a buffer to hold the encrypted data. + //MaxFinalOutputLength() returns the number of bytes the buffer passed to ProcessFinalL() + //needs to be capable of holding + TInt maxOutputLength = impl->MaxFinalOutputLength(srcData.Length()); + HBufC8* encrypted = HBufC8::NewLC(maxOutputLength); + TPtr8 encryptedPtr = encrypted->Des(); + + //Perform the encryption operation + //ProcessFinalL() pads the input buffer if required + TRAP(err, impl->ProcessFinalL(srcData, encryptedPtr)); + + if (err == KErrNone) + { + //Switch to decrypt mode + impl->SetCryptoModeL(KCryptoModeDecryptUid); + + //Create a buffer for the decrypted data + maxOutputLength = encryptedPtr.Length(); + HBufC8* output = HBufC8::NewLC(impl->MaxFinalOutputLength(maxOutputLength)); + TPtr8 outputPtr = output->Des(); + + //Perform the decryption operation + TRAP(err, impl->ProcessFinalL(encryptedPtr, outputPtr)); + + if (err == KErrNone) + { + //Check that the original source data matches the data that + //has been encrypted then decrypted + if (!outputPtr.Compare(srcData)) + { + // Descriptors are the same - successful encrypt/decrypt check + } + } + + CleanupStack::PopAndDestroy(output); + } + + CleanupStack::PopAndDestroy(encrypted); + } + +//Destroy the symmetric implementation object +delete impl; +impl = NULL; + +CleanupStack::PopAndDestroy(key); +CleanupStack::PopAndDestroy(params); +
\ No newline at end of file