1# Encryption and Decryption with an SM4 Symmetric Key (CBC Mode) (C/C++) 2 3 4For details about the algorithm specifications, see [SM4](crypto-sym-encrypt-decrypt-spec.md#sm4). 5 6## Adding the Dynamic Library in the CMake Script 7```txt 8 target_link_libraries(entry PUBLIC libohcrypto.so) 9``` 10 11**Encryption** 12 13 141. 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 SM4 symmetric key (**OH_CryptoSymKey**). 15 16 In addition to the example in this topic, [SM4](crypto-sym-key-generation-conversion-spec.md#sm4) and [Randomly Generating a Symmetric Key](crypto-generate-sym-key-randomly-ndk.md) may help you better understand how to generate an SM4 symmetric key. Note that the input parameters in the reference documents may be different from those in the example below. 17 182. Use [OH_CryptoSymCipher_Create](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_create) with the string parameter **'SM4_128|CBC|PKCS7'** to create a **Cipher** instance. The key type is **SM4_128**, block cipher mode is **CBC**, and the padding mode is **PKCS7**. 19 203. 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. 21 224. 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. 23 245. Use [OH_CryptoSymCipher_Update](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_update) to update the data (plaintext) to be encrypted. 25 26 - If a small amount of data is to be encrypted, you can use **OH_CryptoSymCipher_Final()** immediately after **OH_CryptoSymCipher_Init()**. 27 - If a large amount of data is to be encrypted, you can call **OH_CryptoSymCipher_Update()** multiple times to pass in the data by segment. 28 296. Use [OH_CryptoSymCipher_Final](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_final) to generate the ciphertext. 30 31 - If data has been passed in by **OH_CryptoSymCipher_Update()**, pass in **null** in the **data** parameter of **OH_CryptoSymCipher_Final**. 32 - The output of **OH_CryptoSymCipher_Final** may be **null**. To avoid exceptions, always check whether the result is **null** before accessing specific data. 33 347. 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. 35 36 37**Decryption** 38 39 401. Use [OH_CryptoSymCipher_Init](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_init) initializes 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. 41 422. Use [OH_CryptoSymCipher_Update](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_update) to update the data (ciphertext) to be decrypted. 43 443. Use [OH_CryptoSymCipher_Final](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_final) to generate the plaintext. 45 46**Example** 47 48```c++ 49#include "CryptoArchitectureKit/crypto_common.h" 50#include "CryptoArchitectureKit/crypto_sym_cipher.h" 51#include <string.h> 52 53static OH_Crypto_ErrCode doTestSm4Cbc() 54{ 55 OH_CryptoSymKeyGenerator *genCtx = nullptr; 56 OH_CryptoSymCipher *encCtx = nullptr; 57 OH_CryptoSymCipher *decCtx = nullptr; 58 OH_CryptoSymKey *keyCtx = nullptr; 59 OH_CryptoSymCipherParams *params = nullptr; 60 Crypto_DataBlob outUpdate = {.data = nullptr, .len = 0}; 61 Crypto_DataBlob decUpdate = {.data = nullptr, .len = 0}; 62 63 char *plainText = const_cast<char *>("this is test!"); 64 Crypto_DataBlob msgBlob = {.data = (uint8_t *)(plainText), .len = strlen(plainText)}; 65 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. 66 Crypto_DataBlob ivBlob = {.data = iv, .len = sizeof(iv)}; 67 // Generate a symmetric key. 68 OH_Crypto_ErrCode ret; 69 ret = OH_CryptoSymKeyGenerator_Create("SM4_128", &genCtx); 70 if (ret != CRYPTO_SUCCESS) { 71 goto end; 72 } 73 ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx); 74 if (ret != CRYPTO_SUCCESS) { 75 goto end; 76 } 77 78 // Set parameters. 79 ret = OH_CryptoSymCipherParams_Create(¶ms); 80 if (ret != CRYPTO_SUCCESS) { 81 goto end; 82 } 83 ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_IV_DATABLOB, &ivBlob); 84 if (ret != CRYPTO_SUCCESS) { 85 goto end; 86 } 87 88 // Encrypt data. 89 ret = OH_CryptoSymCipher_Create("SM4_128|CBC|PKCS7", &encCtx); 90 if (ret != CRYPTO_SUCCESS) { 91 goto end; 92 } 93 ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params); 94 if (ret != CRYPTO_SUCCESS) { 95 goto end; 96 } 97 ret = OH_CryptoSymCipher_Final(encCtx, &msgBlob, &outUpdate); 98 if (ret != CRYPTO_SUCCESS) { 99 goto end; 100 } 101 102 // Decrypt data. 103 ret = OH_CryptoSymCipher_Create("SM4_128|CBC|PKCS7", &decCtx); 104 if (ret != CRYPTO_SUCCESS) { 105 goto end; 106 } 107 ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params); 108 if (ret != CRYPTO_SUCCESS) { 109 goto end; 110 } 111 ret = OH_CryptoSymCipher_Final(decCtx, &outUpdate, &decUpdate); 112 if (ret != CRYPTO_SUCCESS) { 113 goto end; 114 } 115 116 // Release resources. 117end: 118 OH_CryptoSymCipherParams_Destroy(params); 119 OH_CryptoSymCipher_Destroy(encCtx); 120 OH_CryptoSymCipher_Destroy(decCtx); 121 OH_CryptoSymKeyGenerator_Destroy(genCtx); 122 OH_CryptoSymKey_Destroy(keyCtx); 123 OH_Crypto_FreeDataBlob(&outUpdate); 124 OH_Crypto_FreeDataBlob(&decUpdate); 125 return ret; 126} 127``` 128