• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Encryption and Decryption with an AES Symmetric Key (CBC Mode) (C/C++)
2
3<!--Kit: Crypto Architecture Kit-->
4<!--Subsystem: Security-->
5<!--Owner: @zxz--3-->
6<!--Designer: @lanming-->
7<!--Tester: @PAFT-->
8<!--Adviser: @zengyawen-->
9
10For details, see [AES](crypto-sym-encrypt-decrypt-spec.md#aes).
11
12## Adding the Dynamic Library in the CMake Script
13```txt
14target_link_libraries(entry PUBLIC libohcrypto.so)
15```
16
17## How to Develop
18
19**Creating an Object**
20
21Call [OH_CryptoSymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-key-h.md#oh_cryptosymkeygenerator_create) and [OH_CryptoSymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-key-h.md#oh_cryptosymkeygenerator_generate) to generate a symmetric key (**OH_CryptoSymKey**) with the key algorithm being AES and the key length being 128 bits.
22
23   In addition to the example in this topic, [AES](crypto-sym-key-generation-conversion-spec.md#aes) and [Randomly Generating a Symmetric Key](crypto-generate-sym-key-randomly-ndk.md) may help you better understand how to generate an AES symmetric key. Note that the input parameters in the reference documents may be different from those in the example below.
24
25**Encrypting a Message**
26
271. Call [OH_CryptoSymCipher_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-cipher-h.md#oh_cryptosymcipher_create) with the string parameter **'AES128|CBC|PKCS7'** to create a **Cipher** instance for encryption. The key type is **AES128**, block cipher mode is **CBC**, and the padding mode is **PKCS7**.
28
292. Call [OH_CryptoSymCipherParams_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-cipher-h.md#oh_cryptosymcipherparams_create) to create a parameter object and call [OH_CryptoSymCipherParams_SetParam](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-cipher-h.md#oh_cryptosymcipherparams_setparam) to set encryption parameters.
30
313. Call [OH_CryptoSymCipher_Init](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-cipher-h.md#oh_cryptosymcipher_init) to initialize the **Cipher** instance. Specifically, set **mode** to **CRYPTO_ENCRYPT_MODE**, and specify the key for encryption (**OH_CryptoSymKey**) and the encryption parameter instance (**OH_CryptoSymCipherParams**) corresponding to the CBC mode.
32
334. When the content to be encrypted is short, call [OH_CryptoSymCipher_Final](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-cipher-h.md#oh_cryptosymcipher_final) to obtain the encrypted data, without calling [OH_CryptoSymCipher_Update](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-cipher-h.md#oh_cryptosymcipher_update).
34
35**Decrypting a Message**
36
371. Call [OH_CryptoSymCipher_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-cipher-h.md#oh_cryptosymcipher_create) with the string parameter **'AES128|CBC|PKCS7'** to create a **Cipher** instance for decryption. The key type is **AES128**, block cipher mode is **CBC**, and the padding mode is **PKCS7**.
38
392. Call [OH_CryptoSymCipher_Init](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-cipher-h.md#oh_cryptosymcipher_init) to initialize the **Cipher** instance. Specifically, set **mode** to **CRYPTO_DECRYPT_MODE**, and specify the decryption key (**OH_CryptoSymKey**) and the decryption parameter instance (**OH_CryptoSymCipherParams**) corresponding to the CBC mode.
40
413. When the content to be decrypted is short, call [OH_CryptoSymCipher_Final](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-cipher-h.md#oh_cryptosymcipher_final) to obtain the encrypted data, without calling [OH_CryptoSymCipher_Update](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-cipher-h.md#oh_cryptosymcipher_update).
42
43**Destroying Objects**
44
45Call [OH_CryptoSymKeyGenerator_Destroy](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-key-h.md#oh_cryptosymkeygenerator_destroy), [OH_CryptoSymCipher_Destroy](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-cipher-h.md#oh_cryptosymcipher_destroy), [OH_CryptoSymKey_Destroy](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-key-h.md#oh_cryptosymkey_destroy), and [OH_Crypto_FreeDataBlob](../../reference/apis-crypto-architecture-kit/capi-crypto-common-h.md#oh_crypto_freedatablob) to release the allocated memory and destroy the object.
46
47```c++
48#include "CryptoArchitectureKit/crypto_common.h"
49#include "CryptoArchitectureKit/crypto_sym_cipher.h"
50#include <string.h>
51
52static OH_Crypto_ErrCode doTestAesCbc()
53{
54    OH_CryptoSymKeyGenerator *genCtx = nullptr;
55    OH_CryptoSymCipher *encCtx = nullptr;
56    OH_CryptoSymCipher *decCtx = nullptr;
57    OH_CryptoSymKey *keyCtx = nullptr;
58    OH_CryptoSymCipherParams *params = nullptr;
59    Crypto_DataBlob encData = {.data = nullptr, .len = 0};
60    Crypto_DataBlob decData = {.data = nullptr, .len = 0};
61    char *plainText = const_cast<char *>("this is test!");
62    Crypto_DataBlob msgBlob = {.data = (uint8_t *)(plainText), .len = strlen(plainText)};
63    uint8_t iv[16] = {1, 2, 4, 12, 3, 4, 2, 3, 3, 2, 0, 4, 3, 1, 0, 10}; // The iv value here is for reference only. You can use secure random numbers to generate it.
64    Crypto_DataBlob ivBlob = {.data = iv, .len = sizeof(iv)};
65    // Generate a symmetric key.
66    OH_Crypto_ErrCode ret;
67    ret = OH_CryptoSymKeyGenerator_Create("AES128", &genCtx);
68    if (ret != CRYPTO_SUCCESS) {
69        goto end;
70    }
71    ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx);
72    if (ret != CRYPTO_SUCCESS) {
73        goto end;
74    }
75
76    // Create a cipher parameter object.
77    ret = OH_CryptoSymCipherParams_Create(&params);
78    if (ret != CRYPTO_SUCCESS) {
79        goto end;
80    }
81    // Set parameters.
82    ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_IV_DATABLOB, &ivBlob); // You only need to set iv if CBC mode is used.
83    if (ret != CRYPTO_SUCCESS) {
84        goto end;
85    }
86
87    // Encrypt the message.
88    ret = OH_CryptoSymCipher_Create("AES128|CBC|PKCS7", &encCtx);
89    if (ret != CRYPTO_SUCCESS) {
90        goto end;
91    }
92    ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params);
93    if (ret != CRYPTO_SUCCESS) {
94        goto end;
95    }
96    ret = OH_CryptoSymCipher_Final(encCtx, &msgBlob, &encData);
97    if (ret != CRYPTO_SUCCESS) {
98        goto end;
99    }
100
101    // Decrypt the message.
102    ret = OH_CryptoSymCipher_Create("AES128|CBC|PKCS7", &decCtx);
103    if (ret != CRYPTO_SUCCESS) {
104        goto end;
105    }
106    ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params); // The params value must be the same as that used in encryption.
107    if (ret != CRYPTO_SUCCESS) {
108        goto end;
109    }
110    ret = OH_CryptoSymCipher_Final(decCtx, &encData, &decData);
111    if (ret != CRYPTO_SUCCESS) {
112        goto end;
113    }
114
115end:
116    OH_CryptoSymCipherParams_Destroy(params);
117    OH_CryptoSymCipher_Destroy(encCtx);
118    OH_CryptoSymCipher_Destroy(decCtx);
119    OH_CryptoSymKeyGenerator_Destroy(genCtx);
120    OH_CryptoSymKey_Destroy(keyCtx);
121    OH_Crypto_FreeDataBlob(&encData);
122    OH_Crypto_FreeDataBlob(&decData);
123    return ret;
124}
125```
126