1e41f4b71Sopenharmony_ci# Converting Binary Data into a Symmetric Key Pair (C/C++)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciThis topic uses 3DES and HMAC as an example to describe how to convert binary data into a symmetric key. That is, convert a piece of external or internal binary data into an **OH_CryptoSymKey** object for subsequent operations, such as encryption and decryption.
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## Converting Binary Data into a 3DES Key
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [3DES](crypto-sym-key-generation-conversion-spec.md#3des).
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci1. Obtain the 3DES binary key data and encapsulate it into a [Crypto_DataBlob](../../reference/apis-crypto-architecture-kit/_crypto_common_api.md#crypto_datablob) object.
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci2. Use [OH_CryptoSymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkeygenerator_create) with the string parameter **'3DES192'** to create a symmetric key generator (**OH_CryptoSymKeyGenerator**) object for a 192-bit 3DES key.
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci3. Use [OH_CryptoSymKeyGenerator_Convert](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkeygenerator_convert) to convert the binary data into a symmetric key object (**OH_CryptoSymKey**).
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci4. Use [OH_CryptoSymKey_GetKeyData](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkey_getkeydata) to obtain the binary data of the key object.
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci- Example: Convert binary data into a 3DES key.
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci  ```c++
26e41f4b71Sopenharmony_ci  #include "CryptoArchitectureKit/crypto_common.h"
27e41f4b71Sopenharmony_ci  #include "CryptoArchitectureKit/crypto_sym_key.h"
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci  static OH_Crypto_ErrCode doTestDataCovertSymKey() {
30e41f4b71Sopenharmony_ci      const char *algName = "3DES192";
31e41f4b71Sopenharmony_ci      OH_CryptoSymKeyGenerator *ctx = nullptr;
32e41f4b71Sopenharmony_ci      OH_CryptoSymKey *convertKeyCtx = nullptr;
33e41f4b71Sopenharmony_ci      Crypto_DataBlob out = {.data = nullptr, .len = 0};
34e41f4b71Sopenharmony_ci      OH_Crypto_ErrCode ret;
35e41f4b71Sopenharmony_ci      uint8_t arr[] = {0xba, 0x3d, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, 0xad, 0x47, 0xfc, 0x5a,
36e41f4b71Sopenharmony_ci                      0x46, 0x39, 0xee, 0x7c, 0xba, 0x3b, 0xc2, 0x71, 0xab, 0xa0, 0x30, 0x72};
37e41f4b71Sopenharmony_ci      Crypto_DataBlob convertBlob = {.data = arr, .len = sizeof(arr)};
38e41f4b71Sopenharmony_ci      ret = OH_CryptoSymKeyGenerator_Create(algName, &ctx);
39e41f4b71Sopenharmony_ci      if (ret != CRYPTO_SUCCESS) {
40e41f4b71Sopenharmony_ci          return ret;
41e41f4b71Sopenharmony_ci      }
42e41f4b71Sopenharmony_ci      ret = OH_CryptoSymKeyGenerator_Convert(ctx, &convertBlob, &convertKeyCtx);
43e41f4b71Sopenharmony_ci      if (ret != CRYPTO_SUCCESS) {
44e41f4b71Sopenharmony_ci          OH_CryptoSymKeyGenerator_Destroy(ctx);
45e41f4b71Sopenharmony_ci          return ret;
46e41f4b71Sopenharmony_ci      }
47e41f4b71Sopenharmony_ci      ret = OH_CryptoSymKey_GetKeyData(convertKeyCtx, &out);
48e41f4b71Sopenharmony_ci      OH_CryptoSymKeyGenerator_Destroy(ctx);
49e41f4b71Sopenharmony_ci      OH_CryptoSymKey_Destroy(convertKeyCtx);
50e41f4b71Sopenharmony_ci      if (ret != CRYPTO_SUCCESS) {
51e41f4b71Sopenharmony_ci          return ret;
52e41f4b71Sopenharmony_ci      }
53e41f4b71Sopenharmony_ci      OH_Crypto_FreeDataBlob(&out);
54e41f4b71Sopenharmony_ci      return ret;
55e41f4b71Sopenharmony_ci  }
56e41f4b71Sopenharmony_ci  ```
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci## Converting Binary Data into an HMAC Key
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [HMAC](crypto-sym-key-generation-conversion-spec.md#hmac).
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci1. Obtain the HMAC binary key and encapsulate it into a [Crypto_DataBlob](../../reference/apis-crypto-architecture-kit/_crypto_common_api.md#crypto_datablob) object.
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci2. Use [OH_CryptoSymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkeygenerator_create) with the string parameter **'HMAC'** to create a symmetric key generator (**OH_CryptoSymKeyGenerator**) object for an HMAC key of [1, 32768] bits.
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci3. Use [OH_CryptoSymKeyGenerator_Convert](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkeygenerator_convert) to convert the binary data into a symmetric key object (**OH_CryptoSymKey**).
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci4. Use [OH_CryptoSymKey_GetKeyData](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkey_getkeydata) to obtain the binary data of the key object.
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci- Example: Convert binary data into an HMAC key.
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci  ```c++
73e41f4b71Sopenharmony_ci  #include "CryptoArchitectureKit/crypto_common.h"
74e41f4b71Sopenharmony_ci  #include "CryptoArchitectureKit/crypto_sym_key.h"
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci  static OH_Crypto_ErrCode testConvertHmacKey() {
77e41f4b71Sopenharmony_ci      const char *algName = "HMAC";
78e41f4b71Sopenharmony_ci      OH_CryptoSymKeyGenerator *ctx = nullptr;
79e41f4b71Sopenharmony_ci      OH_CryptoSymKey *convertKeyCtx = nullptr;
80e41f4b71Sopenharmony_ci      Crypto_DataBlob out = {.data = nullptr, .len = 0};
81e41f4b71Sopenharmony_ci      OH_Crypto_ErrCode ret;
82e41f4b71Sopenharmony_ci      uint8_t arr[] = "12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh";
83e41f4b71Sopenharmony_ci      Crypto_DataBlob convertBlob = {.data = arr, .len = sizeof(arr)};
84e41f4b71Sopenharmony_ci      ret = OH_CryptoSymKeyGenerator_Create(algName, &ctx);
85e41f4b71Sopenharmony_ci      if (ret != CRYPTO_SUCCESS) {
86e41f4b71Sopenharmony_ci          return ret;
87e41f4b71Sopenharmony_ci      }
88e41f4b71Sopenharmony_ci      ret = OH_CryptoSymKeyGenerator_Convert(ctx, &convertBlob, &convertKeyCtx);
89e41f4b71Sopenharmony_ci      if (ret != CRYPTO_SUCCESS) {
90e41f4b71Sopenharmony_ci          OH_CryptoSymKeyGenerator_Destroy(ctx);
91e41f4b71Sopenharmony_ci          return ret;
92e41f4b71Sopenharmony_ci      }
93e41f4b71Sopenharmony_ci      ret = OH_CryptoSymKey_GetKeyData(convertKeyCtx, &out);
94e41f4b71Sopenharmony_ci      OH_CryptoSymKeyGenerator_Destroy(ctx);
95e41f4b71Sopenharmony_ci      OH_CryptoSymKey_Destroy(convertKeyCtx);
96e41f4b71Sopenharmony_ci      if (ret != CRYPTO_SUCCESS) {
97e41f4b71Sopenharmony_ci          return ret;
98e41f4b71Sopenharmony_ci      }
99e41f4b71Sopenharmony_ci      OH_Crypto_FreeDataBlob(&out);
100e41f4b71Sopenharmony_ci      return ret;
101e41f4b71Sopenharmony_ci  }
102e41f4b71Sopenharmony_ci  ```
103