• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Encryption and Decryption with a 3DES Symmetric Key (ECB Mode) (C/C++)
2
3For details about the algorithm specifications, see [3DES](crypto-sym-encrypt-decrypt-spec.md#3des).
4
5
6## Adding the Dynamic Library in the CMake Script
7```txt
8target_link_libraries(entry PUBLIC libohcrypto.so)
9```
10
11## How to Develop
12
13**Creating an Object**
14
15Call [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**).
16
17In 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.
18
19
20**Encrypting a Message**
21
221. Call [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 for encryption. The key type is **3DES192**, block cipher mode is **ECB**, and the padding mode is **PKCS7**.
23
242. 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**).
25
26   If ECB mode is used, set **params** to **null**.
27
283. 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.
29
30   - If a small amount of data is to be encrypted, you can use **OH_CryptoSymCipher_Final()** immediately after **OH_CryptoSymCipher_Init()**.
31   - 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.
32
334. Call [OH_CryptoSymCipher_Final](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_final) to generate the ciphertext.
34
35   - If **OH_CryptoSymCipher_Update** is used to pass in data, set **data** to **null**. If **OH_CryptoSymCipher_Final** is used to pass in data, pass in the plaintext via **data**.
36   - The output of **OH_CryptoSymCipher_Final** may be **null**. To avoid exceptions, always check whether the result is **null** before accessing specific data.
37
38
39**Decryption**
40
411. Call [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 for decryption. The key type is **3DES192**, block cipher mode is **ECB**, and the padding mode is **PKCS7**.
42
432. 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 **null** to **params**.
44
453. 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.
46
47   - If a small amount of data is to be encrypted, you can use **OH_CryptoSymCipher_Final()** immediately after **OH_CryptoSymCipher_Init()**.
48   - 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.
49   - You can determine the method to use based on the data size. For example, if the data size is greater than 20 bytes, use **OH_CryptoSymCipher_Update**.
50
514. Call [OH_CryptoSymCipher_Final](../../reference/apis-crypto-architecture-kit/_crypto_sym_cipher_api.md#oh_cryptosymcipher_final) to generate the plaintext.
52
53   - If **OH_CryptoSymCipher_Update** is used to pass in data, set **data** to **null**. If **OH_CryptoSymCipher_Final** is used to pass in data, pass in the ciphertext via **data**.
54   - The output of **OH_CryptoSymCipher_Final** may be **null**. To avoid exceptions, always check whether the result is **null** before accessing specific data.
55
56**Destroying Objects**
57
58Call [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), [OH_CryptoSymKey_Destroy](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkey_destroy), and [OH_Crypto_FreeDataBlob](../../reference/apis-crypto-architecture-kit/_crypto_common_api.md#oh_crypto_freedatablob) to release the allocated memory and destroy objects.
59
60
61```c++
62#include "CryptoArchitectureKit/crypto_common.h"
63#include "CryptoArchitectureKit/crypto_sym_cipher.h"
64#include <string.h>
65
66static OH_Crypto_ErrCode doTest3DesEcb()
67{
68    OH_CryptoSymKeyGenerator *genCtx = nullptr;
69    OH_CryptoSymCipher *encCtx = nullptr;
70    OH_CryptoSymCipher *decCtx = nullptr;
71    OH_CryptoSymKey *keyCtx = nullptr;
72    char *plainText = const_cast<char *>("this is test!");
73    Crypto_DataBlob input = {.data = (uint8_t *)(plainText), .len = strlen(plainText)};
74    Crypto_DataBlob encData = {.data = nullptr, .len = 0};
75    Crypto_DataBlob decData = {.data = nullptr, .len = 0};
76
77    // Generate a symmetric key randomly.
78    OH_Crypto_ErrCode ret;
79    ret = OH_CryptoSymKeyGenerator_Create("3DES192", &genCtx);
80    if (ret != CRYPTO_SUCCESS) {
81        goto end;
82    }
83    ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx);
84    if (ret != CRYPTO_SUCCESS) {
85        goto end;
86    }
87
88    // Encrypt data.
89    ret = OH_CryptoSymCipher_Create("3DES192|ECB|PKCS7", &encCtx);
90    if (ret != CRYPTO_SUCCESS) {
91        goto end;
92    }
93    ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, nullptr);
94    if (ret != CRYPTO_SUCCESS) {
95        goto end;
96    }
97    ret = OH_CryptoSymCipher_Final(encCtx, &input, &encData);
98    if (ret != CRYPTO_SUCCESS) {
99        goto end;
100    }
101
102    // Decrypt data.
103    ret = OH_CryptoSymCipher_Create("3DES192|ECB|PKCS7", &decCtx);
104    if (ret != CRYPTO_SUCCESS) {
105        goto end;
106    }
107    ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, nullptr);
108    if (ret != CRYPTO_SUCCESS) {
109        goto end;
110    }
111    ret = OH_CryptoSymCipher_Final(decCtx, &encData, &decData);
112    if (ret != CRYPTO_SUCCESS) {
113        goto end;
114    }
115
116end:
117    OH_CryptoSymCipher_Destroy(encCtx);
118    OH_CryptoSymCipher_Destroy(decCtx);
119    OH_CryptoSymKeyGenerator_Destroy(genCtx);
120    OH_CryptoSymKey_Destroy(keyCtx);
121    OH_Crypto_FreeDataBlob(&encData);
122    OH_Crypto_FreeDataBlob(&decData);
123    return ret;
124}
125```
126