1# Encryption and Decryption with an SM4 Symmetric Key (ECB 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 8target_link_libraries(entry PUBLIC libohcrypto.so) 9``` 10 11**Encryption** 12 13 141. Call [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. Call [OH_CryptoSymCipher_Create](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_create) with the string parameter **'SM4_128|ECB|PKCS7'** to create a **Cipher** instance for encryption. The key type is **SM4_128**, block cipher mode is **ECB**, and the padding mode is **PKCS7**. 19 203. 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**). 21 22 If ECB mode is used, set **params** to **null**. 23 244. Call [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 295. Call [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 346. Call [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 391. Call [OH_CryptoSymCipher_Create](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_create) with the string parameter **'SM4_128|ECB|PKCS7'** to create a **Cipher** instance for decryption. The key type is **SM4_128**, block cipher mode is **ECB**, and the padding mode is **PKCS7**. 40 412. 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**). When ECB mode is used, pass in **null** in **params**. 42 433. Call [OH_CryptoSymCipher_Update](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_update) to update the data (ciphertext) to be decrypted. 44 454. Call [OH_CryptoSymCipher_Final](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_final) to generate the plaintext. 46 47 48**Example** 49 50```c++ 51#include "CryptoArchitectureKit/crypto_common.h" 52#include "CryptoArchitectureKit/crypto_sym_cipher.h" 53#include <string.h> 54 55static OH_Crypto_ErrCode doTestSm4Ecb() { 56 OH_CryptoSymKeyGenerator *genCtx = nullptr; 57 OH_CryptoSymCipher *encCtx = nullptr; 58 OH_CryptoSymCipher *decCtx = nullptr; 59 OH_CryptoSymKey *keyCtx = nullptr; 60 OH_CryptoSymCipherParams *params = nullptr; 61 char *plainText = const_cast<char *>("this is test!"); 62 Crypto_DataBlob input = {.data = (uint8_t *)(plainText), .len = strlen(plainText)}; 63 Crypto_DataBlob outUpdate = {.data = nullptr, .len = 0}; 64 Crypto_DataBlob decUpdate = {.data = nullptr, .len = 0}; 65 66 // Generate a symmetric key randomly. 67 OH_Crypto_ErrCode ret; 68 ret = OH_CryptoSymKeyGenerator_Create("SM4_128", &genCtx); 69 if (ret != CRYPTO_SUCCESS) { 70 goto end; 71 } 72 ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx); 73 if (ret != CRYPTO_SUCCESS) { 74 goto end; 75 } 76 // Create a parameter instance. 77 ret = OH_CryptoSymCipherParams_Create(¶ms); 78 if (ret != CRYPTO_SUCCESS) { 79 goto end; 80 } 81 82 // Encrypt data. 83 ret = OH_CryptoSymCipher_Create("SM4_128|ECB|PKCS7", &encCtx); 84 if (ret != CRYPTO_SUCCESS) { 85 goto end; 86 } 87 ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params); 88 if (ret != CRYPTO_SUCCESS) { 89 goto end; 90 } 91 ret = OH_CryptoSymCipher_Final(encCtx, &input, &outUpdate); 92 if (ret != CRYPTO_SUCCESS) { 93 goto end; 94 } 95 96 // Decrypt data. 97 ret = OH_CryptoSymCipher_Create("SM4_128|ECB|PKCS7", &decCtx); 98 if (ret != CRYPTO_SUCCESS) { 99 goto end; 100 } 101 ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params); 102 if (ret != CRYPTO_SUCCESS) { 103 goto end; 104 } 105 ret = OH_CryptoSymCipher_Final(decCtx, &outUpdate, &decUpdate); 106 if (ret != CRYPTO_SUCCESS) { 107 goto end; 108 } 109 // Release resources. 110end: 111 OH_CryptoSymCipherParams_Destroy(params); 112 OH_CryptoSymCipher_Destroy(encCtx); 113 OH_CryptoSymCipher_Destroy(decCtx); 114 OH_CryptoSymKeyGenerator_Destroy(genCtx); 115 OH_CryptoSymKey_Destroy(keyCtx); 116 OH_Crypto_FreeDataBlob(&outUpdate); 117 OH_Crypto_FreeDataBlob(&decUpdate); 118 return ret; 119} 120``` 121