• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Encryption and Decryption with an AES Symmetric Key (CBC Mode) (C/C++)
2
3
4For details about the algorithm specifications, see [AES](crypto-sym-encrypt-decrypt-spec.md#aes).
5
6
7## Adding the Dynamic Library in the CMake Script
8```txt
9target_link_libraries(entry PUBLIC libohcrypto.so)
10```
11
12## How to Develop
13
14**Creating an Object**
15
16Call [OH_CryptoSymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkeygenerator_create) and [OH_CryptoSymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkeygenerator_generate) to generate a 128-bit AES symmetric key (**OH_CryptoSymKey**).
17
18In 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.
19
20**Encrypting a Message**
21
221. Call [OH_CryptoSymCipher_Create](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.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**.
23
242. Call [OH_CryptoSymCipherParams_Create](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipherparams_create) to create a symmetric cipher parameter instance, and call [OH_CryptoSymCipherParams_SetParam](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipherparams_setparam) to set cipher parameters.
25
263. Call [OH_CryptoSymCipher_Init](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.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.
27
284. If a small amount of data is to be encrypted, use [OH_CryptoSymCipher_Final](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_final) to generate the encrypted data. If a large amount of data is to be encrypted, you can call [OH_CryptoSymCipher_Update](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_update) multiple times to pass in the data by segment, and then use **OH_CryptoSymCipher_Final** to generate the ciphertext.
29
30**Decrypting a Message**
31
321. Call [OH_CryptoSymCipher_Create](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.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**.
33
342. Call [OH_CryptoSymCipher_Init](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_init) to initialize the **Cipher** instance. Specifically, set **mode** to **CRYPTO_DECRYPT_MODE**, and specify the key for decryption (**OH_CryptoSymKey**) and the decryption parameter instance (**OH_CryptoSymCipherParams**) corresponding to the CBC mode.
35
363. If a small amount of data is to be decrypted, use [OH_CryptoSymCipher_Final](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_final) to generate the decrypted data. If a large amount of data is to be decrypted, you can call [OH_CryptoSymCipher_Update](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_update) multiple times to pass in the data by segment, and then use **OH_CryptoSymCipher_Final** to generate the plaintext.
37
38**Destroying Objects**
39
40Call [OH_CryptoSymKeyGenerator_Destroy](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkeygenerator_destroy), [OH_CryptoSymCipher_Destroy](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_destroy), [OH_CryptoSymKey_Destroy](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkey_destroy), and [OH_Crypto_FreeDataBlob](../../reference/apis-crypto-architecture-kit/_crypto_common_api.md#oh_crypto_freedatablob) to release the allocated memory and destroy objects.
41
42```c++
43#include "CryptoArchitectureKit/crypto_common.h"
44#include "CryptoArchitectureKit/crypto_sym_cipher.h"
45#include <string.h>
46
47static OH_Crypto_ErrCode doTestAesCbc()
48{
49    OH_CryptoSymKeyGenerator *genCtx = nullptr;
50    OH_CryptoSymCipher *encCtx = nullptr;
51    OH_CryptoSymCipher *decCtx = nullptr;
52    OH_CryptoSymKey *keyCtx = nullptr;
53    OH_CryptoSymCipherParams *params = nullptr;
54    Crypto_DataBlob encData = {.data = nullptr, .len = 0};
55    Crypto_DataBlob decData = {.data = nullptr, .len = 0};
56    char *plainText = const_cast<char *>("this is test!");
57    Crypto_DataBlob msgBlob = {.data = (uint8_t *)(plainText), .len = strlen(plainText)};
58    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 a secure random number to generate the iv value.
59    Crypto_DataBlob ivBlob = {.data = iv, .len = sizeof(iv)};
60    // Generate a symmetric key.
61    OH_Crypto_ErrCode ret;
62    ret = OH_CryptoSymKeyGenerator_Create("AES128", &genCtx);
63    if (ret != CRYPTO_SUCCESS) {
64        goto end;
65    }
66    ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx);
67    if (ret != CRYPTO_SUCCESS) {
68        goto end;
69    }
70
71    // Create a ciper parameter object.
72    ret = OH_CryptoSymCipherParams_Create(&params);
73    if (ret != CRYPTO_SUCCESS) {
74        goto end;
75    }
76    // Set parameters.
77    ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_IV_DATABLOB, &ivBlob); // You only need to set iv if CBC mode is used.
78    if (ret != CRYPTO_SUCCESS) {
79        goto end;
80    }
81
82    // Encrypt data.
83    ret = OH_CryptoSymCipher_Create("AES128|CBC|PKCS7", &encCtx);
84    if (ret != CRYPTO_SUCCESS) {
85        goto end;
86    }
87    ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params);
88    if (ret != CRYPTO_SUCCESS) {
89        goto end;
90    }
91    ret = OH_CryptoSymCipher_Final(encCtx, &msgBlob, &encData);
92    if (ret != CRYPTO_SUCCESS) {
93        goto end;
94    }
95
96    // Decrypt data.
97    ret = OH_CryptoSymCipher_Create("AES128|CBC|PKCS7", &decCtx);
98    if (ret != CRYPTO_SUCCESS) {
99        goto end;
100    }
101    ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params); // The params value be the same as that used in encryption.
102    if (ret != CRYPTO_SUCCESS) {
103        goto end;
104    }
105    ret = OH_CryptoSymCipher_Final(decCtx, &encData, &decData);
106    if (ret != CRYPTO_SUCCESS) {
107        goto end;
108    }
109
110end:
111    OH_CryptoSymCipherParams_Destroy(params);
112    OH_CryptoSymCipher_Destroy(encCtx);
113    OH_CryptoSymCipher_Destroy(decCtx);
114    OH_CryptoSymKeyGenerator_Destroy(genCtx);
115    OH_CryptoSymKey_Destroy(keyCtx);
116    OH_Crypto_FreeDataBlob(&encData);
117    OH_Crypto_FreeDataBlob(&decData);
118    return ret;
119}
120```
121