|
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-D2D17EF9-FFC6-5FBD-A992-55746A12B625-GENID-1-12-1-26-1-1-11-1-1-3-1-4-1-3-1" xml:lang="en"><title>Basic |
|
13 encryption and decryption using a symmetric cipher</title><prolog><metadata><keywords/></metadata></prolog><conbody> |
|
14 <p>This example demonstrates how to encrypt and decrypt a small string of |
|
15 data using a symmetric cipher. Firstly, an encryption operation is performed |
|
16 on the data. Following this a decryption operation is performed on the encrypted |
|
17 data. Finally a check is made on the decrypted data for equality with the |
|
18 original data which tests that the encrypt and decrypt operations have maintained |
|
19 data integrity. </p> |
|
20 <codeblock id="GUID-E9342EBD-213A-53CE-BA3E-93CC24D63319-GENID-1-12-1-26-1-1-11-1-1-3-1-4-1-3-1-2-2" xml:space="preserve">#include <legacyselector.h> |
|
21 #include <cryptosymmetriccipherapi.h> |
|
22 #include <keys.h> |
|
23 |
|
24 using namespace CryptoSpi; |
|
25 |
|
26 //Create a new CCryptoParams object to encapsulate the secret key string and key type |
|
27 CCryptoParams* params = CCryptoParams::NewLC(); |
|
28 |
|
29 //Add the secret key to the CCryptoParams object by calling the CCryptoParams::AddL() method, |
|
30 //passing in the key string and appropriate key parameter UID |
|
31 params->AddL( _L8("12345678"), KSymmetricKeyParameterUid); |
|
32 |
|
33 //Create a key object (CKey) by passing in an instance of TKeyProperty and the previously |
|
34 //created CCryptoParams object containing the secret key. |
|
35 TKeyProperty keyProperty = {KDesUid, KNullUid, KSymmetricKey, KNonEmbeddedKeyUid}; |
|
36 CKey* key=CKey::NewL(keyProperty, *params); |
|
37 CleanupStack::PushL(key); |
|
38 //Create and initialize a pointer for the Symmetric cipher implementation object |
|
39 CSymmetricCipher* impl = NULL; |
|
40 |
|
41 //Create a symmetric cipher passing in the appropriate CKey object |
|
42 TRAPD(err, CSymmetricCipherFactory::CreateSymmetricCipherL |
|
43 (impl, |
|
44 KDesUid, |
|
45 *key, |
|
46 KCryptoModeEncryptUid, // This can be either encrypt or decrypt |
|
47 KOperationModeECBUid, // Use "electronic code book" mode |
|
48 KPaddingModeNoneUid, // This is the type of padding to be used |
|
49 NULL)); |
|
50 |
|
51 //Check the return value and for non NULL |
|
52 if (impl && (err == KErrNone)) |
|
53 { |
|
54 //Create a buffer holding the data to be encrypted |
|
55 TBuf8<8> srcData(_L8("12345678")); |
|
56 |
|
57 //Create a buffer to hold the encrypted data. |
|
58 //MaxFinalOutputLength() returns the number of bytes the buffer passed to ProcessFinalL() |
|
59 //needs to be capable of holding |
|
60 TInt maxOutputLength = impl->MaxFinalOutputLength(srcData.Length()); |
|
61 HBufC8* encrypted = HBufC8::NewLC(maxOutputLength); |
|
62 TPtr8 encryptedPtr = encrypted->Des(); |
|
63 |
|
64 //Perform the encryption operation |
|
65 //ProcessFinalL() pads the input buffer if required |
|
66 TRAP(err, impl->ProcessFinalL(srcData, encryptedPtr)); |
|
67 |
|
68 if (err == KErrNone) |
|
69 { |
|
70 //Switch to decrypt mode |
|
71 impl->SetCryptoModeL(KCryptoModeDecryptUid); |
|
72 |
|
73 //Create a buffer for the decrypted data |
|
74 maxOutputLength = encryptedPtr.Length(); |
|
75 HBufC8* output = HBufC8::NewLC(impl->MaxFinalOutputLength(maxOutputLength)); |
|
76 TPtr8 outputPtr = output->Des(); |
|
77 |
|
78 //Perform the decryption operation |
|
79 TRAP(err, impl->ProcessFinalL(encryptedPtr, outputPtr)); |
|
80 |
|
81 if (err == KErrNone) |
|
82 { |
|
83 //Check that the original source data matches the data that |
|
84 //has been encrypted then decrypted |
|
85 if (!outputPtr.Compare(srcData)) |
|
86 { |
|
87 // Descriptors are the same - successful encrypt/decrypt check |
|
88 } |
|
89 } |
|
90 |
|
91 CleanupStack::PopAndDestroy(output); |
|
92 } |
|
93 |
|
94 CleanupStack::PopAndDestroy(encrypted); |
|
95 } |
|
96 |
|
97 //Destroy the symmetric implementation object |
|
98 delete impl; |
|
99 impl = NULL; |
|
100 |
|
101 CleanupStack::PopAndDestroy(key); |
|
102 CleanupStack::PopAndDestroy(params);</codeblock> |
|
103 </conbody></concept> |