1# Encryption and Decryption with an AES Symmetric Key (ECB 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
9   target_link_libraries(entry PUBLIC libohcrypto.so)
10```
11
12## How to Develop
13
14**Encryption**
15
16
171. Use [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**).
18
19   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.
20
212. Use [OH_CryptoSymCipher_Create](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_create) with the string parameter **'AES128|ECB|PKCS7'** to create a **Cipher** instance. The key type is **AES128**, block cipher mode is **ECB**, and the padding mode is **PKCS7**.
22
233. Use [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**).
24
254. 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) after **OH_CryptoSymCipher_Init** 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.
26
275. Use [OH_CryptoSymKeyGenerator_Destroy](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkeygenerator_destroy) and [OH_CryptoSymCipher_Destroy](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_destroy) to destroy the instances created.
28
29**Decryption**
30
31
321. Use [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**).
33
342. 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 plaintext. 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.
35
36**Example**
37
38```c++
39#include "CryptoArchitectureKit/crypto_common.h"
40#include "CryptoArchitectureKit/crypto_sym_cipher.h"
41#include <string.h>
42
43static OH_Crypto_ErrCode doTestAesEcb()
44{
45    OH_CryptoSymKeyGenerator *genCtx = nullptr;
46    OH_CryptoSymCipher *encCtx = nullptr;
47    OH_CryptoSymCipher *decCtx = nullptr;
48    OH_CryptoSymKey *keyCtx = nullptr;
49    OH_CryptoSymCipherParams *params = nullptr;
50    char *plainText = const_cast<char *>("this is test");
51    Crypto_DataBlob input = {.data = (uint8_t *)(plainText), .len = strlen(plainText)};
52    Crypto_DataBlob outUpdate = {.data = nullptr, .len = 0};
53    Crypto_DataBlob decUpdate = {.data = nullptr, .len = 0};
54
55    // Generate a symmetric key randomly.
56    OH_Crypto_ErrCode ret;
57    ret = OH_CryptoSymKeyGenerator_Create("AES128", &genCtx);
58    if (ret != CRYPTO_SUCCESS) {
59        goto end;
60    }
61    ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx);
62    if (ret != CRYPTO_SUCCESS) {
63        goto end;
64    }
65    // Create a params instance.
66    ret = OH_CryptoSymCipherParams_Create(&params);
67    if (ret != CRYPTO_SUCCESS) {
68        goto end;
69    }
70
71    // Encrypt data.
72    ret = OH_CryptoSymCipher_Create("AES128|ECB|PKCS7", &encCtx);
73    if (ret != CRYPTO_SUCCESS) {
74        goto end;
75    }
76    ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params);
77    if (ret != CRYPTO_SUCCESS) {
78        goto end;
79    }
80    ret = OH_CryptoSymCipher_Final(encCtx, &input, &outUpdate);
81    if (ret != CRYPTO_SUCCESS) {
82        goto end;
83    }
84
85    // Decrypt data.
86    ret = OH_CryptoSymCipher_Create("AES128|ECB|PKCS7", &decCtx);
87    if (ret != CRYPTO_SUCCESS) {
88        goto end;
89    }
90    ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params);
91    if (ret != CRYPTO_SUCCESS) {
92        goto end;
93    }
94    ret = OH_CryptoSymCipher_Final(decCtx, &outUpdate, &decUpdate);
95    if (ret != CRYPTO_SUCCESS) {
96        goto end;
97    }
98
99end:
100    OH_CryptoSymCipherParams_Destroy(params);
101    OH_CryptoSymCipher_Destroy(encCtx);
102    OH_CryptoSymCipher_Destroy(decCtx);
103    OH_CryptoSymKeyGenerator_Destroy(genCtx);
104    OH_CryptoSymKey_Destroy(keyCtx);
105    OH_Crypto_FreeDataBlob(&outUpdate);
106    OH_Crypto_FreeDataBlob(&decUpdate);
107    return ret;
108}
109```
110