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 9 target_link_libraries(entry PUBLIC libohcrypto.so) 10``` 11 12## How to Develop 13 14 15**Encryption** 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|CBC|PKCS7'** to create a **Cipher** instance. The key type is **AES128**, block cipher mode is **CBC**, and the padding mode is **PKCS7**. 22 233. Use [OH_CryptoSymCipherParams_Create](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipherparams_create) to create a symmetric cipher parameter instance, and use [OH_CryptoSymCipherParams_SetParam](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipherparams_setparam) to set cipher parameters. 24 254. 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**) and the encryption parameter instance (**OH_CryptoSymCipherParams**) corresponding to the CBC mode. 26 275. 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. 28 296. Use [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), and [OH_CryptoSymCipherParams_Destroy](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipherparams_destroy) to destroy the instances created. 30 31 32**Decryption** 33 34 351. 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**) and the decryption parameter instance (**OH_CryptoSymCipherParams**) corresponding to the CBC mode. 36 372. 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. 38 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 doTestAesCbc() 47{ 48 OH_CryptoSymKeyGenerator *genCtx = nullptr; 49 OH_CryptoSymCipher *encCtx = nullptr; 50 OH_CryptoSymCipher *decCtx = nullptr; 51 OH_CryptoSymKey *keyCtx = nullptr; 52 OH_CryptoSymCipherParams *params = nullptr; 53 Crypto_DataBlob outUpdate = {.data = nullptr, .len = 0}; 54 Crypto_DataBlob decUpdate = {.data = nullptr, .len = 0}; 55 char *plainText = const_cast<char *>("this is test!"); 56 Crypto_DataBlob msgBlob = {.data = (uint8_t *)(plainText), .len = strlen(plainText)}; 57 uint8_t iv[16] = {1, 2, 4, 12, 3, 4, 2, 3, 3, 2, 0, 4, 3, 1, 0, 10}; // iv is generated from an array of secure random numbers. 58 Crypto_DataBlob ivBlob = {.data = iv, .len = sizeof(iv)}; 59 // Generate a symmetric key. 60 OH_Crypto_ErrCode ret; 61 ret = OH_CryptoSymKeyGenerator_Create("AES128", &genCtx); 62 if (ret != CRYPTO_SUCCESS) { 63 goto end; 64 } 65 ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx); 66 if (ret != CRYPTO_SUCCESS) { 67 goto end; 68 } 69 70 // Set parameters. 71 ret = OH_CryptoSymCipherParams_Create(¶ms); 72 if (ret != CRYPTO_SUCCESS) { 73 goto end; 74 } 75 ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_IV_DATABLOB, &ivBlob); 76 if (ret != CRYPTO_SUCCESS) { 77 goto end; 78 } 79 80 // Encrypt data. 81 ret = OH_CryptoSymCipher_Create("AES128|CBC|PKCS7", &encCtx); 82 if (ret != CRYPTO_SUCCESS) { 83 goto end; 84 } 85 ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params); 86 if (ret != CRYPTO_SUCCESS) { 87 goto end; 88 } 89 ret = OH_CryptoSymCipher_Final(encCtx, &msgBlob, &outUpdate); 90 if (ret != CRYPTO_SUCCESS) { 91 goto end; 92 } 93 94 // Decrypt data. 95 ret = OH_CryptoSymCipher_Create("AES128|CBC|PKCS7", &decCtx); 96 if (ret != CRYPTO_SUCCESS) { 97 goto end; 98 } 99 ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params); 100 if (ret != CRYPTO_SUCCESS) { 101 goto end; 102 } 103 ret = OH_CryptoSymCipher_Final(decCtx, &outUpdate, &decUpdate); 104 if (ret != CRYPTO_SUCCESS) { 105 goto end; 106 } 107 108end: 109 OH_CryptoSymCipherParams_Destroy(params); 110 OH_CryptoSymCipher_Destroy(encCtx); 111 OH_CryptoSymCipher_Destroy(decCtx); 112 OH_CryptoSymKeyGenerator_Destroy(genCtx); 113 OH_CryptoSymKey_Destroy(keyCtx); 114 OH_Crypto_FreeDataBlob(&outUpdate); 115 OH_Crypto_FreeDataBlob(&decUpdate); 116 return ret; 117} 118``` 119