1e41f4b71Sopenharmony_ci# Encryption and Decryption with a 3DES Symmetric Key (ECB Mode) (C/C++) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [3DES](crypto-sym-encrypt-decrypt-spec.md#3des). 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci## Adding the Dynamic Library in the CMake Script 7e41f4b71Sopenharmony_ci```txt 8e41f4b71Sopenharmony_ci target_link_libraries(entry PUBLIC libohcrypto.so) 9e41f4b71Sopenharmony_ci``` 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## How to Develop 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci**Encryption** 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci1. 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 192-bit 3DES symmetric key (**OH_CryptoSymKey**). 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci In addition to the example in this topic, [3DES](crypto-sym-key-generation-conversion-spec.md#3des) and [Converting Binary Data into a Symmetric Key](crypto-convert-binary-data-to-sym-key-ndk.md) may help you better understand how to generate a 3DES symmetric key pair. Note that the input parameters in the reference documents may be different from those in the example below. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci2. Use [OH_CryptoSymCipher_Create](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_create) with the string parameter **'3DES192|ECB|PKCS7'** to create a **Cipher** instance. The key type is **3DES192**, block cipher mode is **ECB**, and the padding mode is **PKCS7**. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci3. 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**). 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci When ECB mode is used, pass in **null** in **params**. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci4. 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. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci - If a small amount of data is to be encrypted, you can use **OH_CryptoSymCipher_Final()** immediately after **OH_CryptoSymCipher_Init()**. 30e41f4b71Sopenharmony_ci - 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. 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci5. Use [OH_CryptoSymCipher_Final](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_final) to generate the ciphertext. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci - If data has been passed in by **OH_CryptoSymCipher_Update()**, pass in **null** in the **data** parameter of **OH_CryptoSymCipher_Final**. 35e41f4b71Sopenharmony_ci - The output of **OH_CryptoSymCipher_Final** may be **null**. To avoid exceptions, always check whether the result is **null** before accessing specific data. 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci**Decryption** 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci1. 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**). When ECB mode is used, pass in **null** in **params**. 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci2. 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. 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci3. Use [OH_CryptoSymCipher_Final](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_final) to generate the plaintext. 46e41f4b71Sopenharmony_ci**Example** 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci```c++ 50e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_common.h" 51e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_sym_cipher.h" 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_cistatic OH_Crypto_ErrCode doTest3DesEcb() 54e41f4b71Sopenharmony_ci{ 55e41f4b71Sopenharmony_ci OH_CryptoSymKeyGenerator *genCtx = nullptr; 56e41f4b71Sopenharmony_ci OH_CryptoSymCipher *encCtx = nullptr; 57e41f4b71Sopenharmony_ci OH_CryptoSymCipher *decCtx = nullptr; 58e41f4b71Sopenharmony_ci OH_CryptoSymKey *keyCtx = nullptr; 59e41f4b71Sopenharmony_ci OH_CryptoSymCipherParams *params = nullptr; 60e41f4b71Sopenharmony_ci uint8_t plainText[] = "this is test"; 61e41f4b71Sopenharmony_ci Crypto_DataBlob input = {.data = reinterpret_cast<uint8_t *>(plainText), .len = sizeof(plainText)}; 62e41f4b71Sopenharmony_ci Crypto_DataBlob outUpdate = {.data = nullptr, .len = 0}; 63e41f4b71Sopenharmony_ci Crypto_DataBlob decUpdate = {.data = nullptr, .len = 0}; 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci // Generate a symmetric key randomly. 66e41f4b71Sopenharmony_ci OH_Crypto_ErrCode ret; 67e41f4b71Sopenharmony_ci ret = OH_CryptoSymKeyGenerator_Create("3DES192", &genCtx); 68e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 69e41f4b71Sopenharmony_ci goto end; 70e41f4b71Sopenharmony_ci } 71e41f4b71Sopenharmony_ci ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx); 72e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 73e41f4b71Sopenharmony_ci goto end; 74e41f4b71Sopenharmony_ci } 75e41f4b71Sopenharmony_ci // Create a params instance. 76e41f4b71Sopenharmony_ci ret = OH_CryptoSymCipherParams_Create(¶ms); 77e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 78e41f4b71Sopenharmony_ci goto end; 79e41f4b71Sopenharmony_ci } 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci // Encrypt data. 82e41f4b71Sopenharmony_ci ret = OH_CryptoSymCipher_Create("3DES192|ECB|PKCS7", &encCtx); 83e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 84e41f4b71Sopenharmony_ci goto end; 85e41f4b71Sopenharmony_ci } 86e41f4b71Sopenharmony_ci ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params); 87e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 88e41f4b71Sopenharmony_ci goto end; 89e41f4b71Sopenharmony_ci } 90e41f4b71Sopenharmony_ci ret = OH_CryptoSymCipher_Final(encCtx, &input, &outUpdate); 91e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 92e41f4b71Sopenharmony_ci goto end; 93e41f4b71Sopenharmony_ci } 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci // Decrypt data. 96e41f4b71Sopenharmony_ci ret = OH_CryptoSymCipher_Create("3DES192|ECB|PKCS7", &decCtx); 97e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 98e41f4b71Sopenharmony_ci goto end; 99e41f4b71Sopenharmony_ci } 100e41f4b71Sopenharmony_ci ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params); 101e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 102e41f4b71Sopenharmony_ci goto end; 103e41f4b71Sopenharmony_ci } 104e41f4b71Sopenharmony_ci ret = OH_CryptoSymCipher_Final(decCtx, &outUpdate, &decUpdate); 105e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 106e41f4b71Sopenharmony_ci goto end; 107e41f4b71Sopenharmony_ci } 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ciend: 110e41f4b71Sopenharmony_ci OH_CryptoSymCipherParams_Destroy(params); 111e41f4b71Sopenharmony_ci OH_CryptoSymCipher_Destroy(encCtx); 112e41f4b71Sopenharmony_ci OH_CryptoSymCipher_Destroy(decCtx); 113e41f4b71Sopenharmony_ci OH_CryptoSymKeyGenerator_Destroy(genCtx); 114e41f4b71Sopenharmony_ci OH_CryptoSymKey_Destroy(keyCtx); 115e41f4b71Sopenharmony_ci OH_Crypto_FreeDataBlob(&outUpdate); 116e41f4b71Sopenharmony_ci OH_Crypto_FreeDataBlob(&decUpdate); 117e41f4b71Sopenharmony_ci return ret; 118e41f4b71Sopenharmony_ci} 119e41f4b71Sopenharmony_ci``` 120