1# Encryption and Decryption with an AES Symmetric Key (ECB 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 about the algorithm specifications, 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|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**. 28 292. 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**). 30 313. If 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). 32 33**Decrypting a Message** 34 351. Call [OH_CryptoSymCipher_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-cipher-h.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**. 36 372. 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 key for decryption (**OH_CryptoSymKey**). 38 393. If 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 decrypted data, without calling [OH_CryptoSymCipher_Update](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-cipher-h.md#oh_cryptosymcipher_update). 40 41**Destroying Objects** 42 43Call [OH_CryptoSymKeyGenerator_Destroy](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-key-h.md#oh_cryptosymkeygenerator_destroy) to destroy the key generator. Call [OH_CryptoSymCipher_Destroy](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-cipher-h.md#oh_cryptosymcipher_destroy) to destroy the cipher object. 44 45```c++ 46#include "CryptoArchitectureKit/crypto_common.h" 47#include "CryptoArchitectureKit/crypto_sym_cipher.h" 48#include <string.h> 49 50static OH_Crypto_ErrCode doTestAesEcb() 51{ 52 OH_CryptoSymKeyGenerator *genCtx = nullptr; 53 OH_CryptoSymCipher *encCtx = nullptr; 54 OH_CryptoSymCipher *decCtx = nullptr; 55 OH_CryptoSymKey *keyCtx = nullptr; 56 char *plainText = const_cast<char *>("this is test"); 57 Crypto_DataBlob input = {.data = (uint8_t *)(plainText), .len = strlen(plainText)}; 58 Crypto_DataBlob encData = {.data = nullptr, .len = 0}; 59 Crypto_DataBlob decData = {.data = nullptr, .len = 0}; 60 61 // Generate a symmetric key randomly. 62 OH_Crypto_ErrCode ret; 63 ret = OH_CryptoSymKeyGenerator_Create("AES128", &genCtx); 64 if (ret != CRYPTO_SUCCESS) { 65 goto end; 66 } 67 ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx); 68 if (ret != CRYPTO_SUCCESS) { 69 goto end; 70 } 71 72 // Encrypt the message. 73 ret = OH_CryptoSymCipher_Create("AES128|ECB|PKCS7", &encCtx); 74 if (ret != CRYPTO_SUCCESS) { 75 goto end; 76 } 77 ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, nullptr); // If ECB is used, set params to null. 78 if (ret != CRYPTO_SUCCESS) { 79 goto end; 80 } 81 ret = OH_CryptoSymCipher_Final(encCtx, &input, &encData); 82 if (ret != CRYPTO_SUCCESS) { 83 goto end; 84 } 85 86 // Decrypt the message. 87 ret = OH_CryptoSymCipher_Create("AES128|ECB|PKCS7", &decCtx); 88 if (ret != CRYPTO_SUCCESS) { 89 goto end; 90 } 91 ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, nullptr); // If ECB is used, set params to null. 92 if (ret != CRYPTO_SUCCESS) { 93 goto end; 94 } 95 ret = OH_CryptoSymCipher_Final(decCtx, &encData, &decData); 96 if (ret != CRYPTO_SUCCESS) { 97 goto end; 98 } 99 100end: 101 OH_CryptoSymCipher_Destroy(encCtx); 102 OH_CryptoSymCipher_Destroy(decCtx); 103 OH_CryptoSymKeyGenerator_Destroy(genCtx); 104 OH_CryptoSymKey_Destroy(keyCtx); 105 OH_Crypto_FreeDataBlob(&encData); 106 OH_Crypto_FreeDataBlob(&decData); 107 return ret; 108} 109``` 110