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);
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.