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 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|ECB|PKCS7'** to create a **Cipher** instance for encryption. The key type is **AES128**, block cipher mode is **ECB**, and the padding mode is **PKCS7**. 23 242. 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**). 25 263. 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. 27 28**Decrypting a Message** 29 301. Call [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 for decryption. The key type is **AES128**, block cipher mode is **ECB**, and the padding mode is **PKCS7**. 31 322. 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**). 33 343. 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**Destroying Objects** 37 38Call [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. 39 40 41```c++ 42#include "CryptoArchitectureKit/crypto_common.h" 43#include "CryptoArchitectureKit/crypto_sym_cipher.h" 44#include <string.h> 45 46static OH_Crypto_ErrCode doTestAesEcb() 47{ 48 OH_CryptoSymKeyGenerator *genCtx = nullptr; 49 OH_CryptoSymCipher *encCtx = nullptr; 50 OH_CryptoSymCipher *decCtx = nullptr; 51 OH_CryptoSymKey *keyCtx = nullptr; 52 char *plainText = const_cast<char *>("this is test"); 53 Crypto_DataBlob input = {.data = (uint8_t *)(plainText), .len = strlen(plainText)}; 54 Crypto_DataBlob encData = {.data = nullptr, .len = 0}; 55 Crypto_DataBlob decData = {.data = nullptr, .len = 0}; 56 57 // Generate a symmetric key randomly. 58 OH_Crypto_ErrCode ret; 59 ret = OH_CryptoSymKeyGenerator_Create("AES128", &genCtx); 60 if (ret != CRYPTO_SUCCESS) { 61 goto end; 62 } 63 ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx); 64 if (ret != CRYPTO_SUCCESS) { 65 goto end; 66 } 67 68 // Encrypt data. 69 ret = OH_CryptoSymCipher_Create("AES128|ECB|PKCS7", &encCtx); 70 if (ret != CRYPTO_SUCCESS) { 71 goto end; 72 } 73 ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, nullptr); // If ECB is used, set params to null. 74 if (ret != CRYPTO_SUCCESS) { 75 goto end; 76 } 77 ret = OH_CryptoSymCipher_Final(encCtx, &input, &encData); 78 if (ret != CRYPTO_SUCCESS) { 79 goto end; 80 } 81 82 // Decrypt data. 83 ret = OH_CryptoSymCipher_Create("AES128|ECB|PKCS7", &decCtx); 84 if (ret != CRYPTO_SUCCESS) { 85 goto end; 86 } 87 ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, nullptr); // If ECB is used, set params to null. 88 if (ret != CRYPTO_SUCCESS) { 89 goto end; 90 } 91 ret = OH_CryptoSymCipher_Final(decCtx, &encData, &decData); 92 if (ret != CRYPTO_SUCCESS) { 93 goto end; 94 } 95 96end: 97 OH_CryptoSymCipher_Destroy(encCtx); 98 OH_CryptoSymCipher_Destroy(decCtx); 99 OH_CryptoSymKeyGenerator_Destroy(genCtx); 100 OH_CryptoSymKey_Destroy(keyCtx); 101 OH_Crypto_FreeDataBlob(&encData); 102 OH_Crypto_FreeDataBlob(&decData); 103 return ret; 104} 105``` 106