1# Converting Binary Data into a Symmetric Key Pair (C/C++)
2
3
4This 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.
5
6## Adding the Dynamic Library in the CMake Script
7```txt
8   target_link_libraries(entry PUBLIC libohcrypto.so)
9```
10
11## Converting Binary Data into a 3DES Key
12
13For details about the algorithm specifications, see [3DES](crypto-sym-key-generation-conversion-spec.md#3des).
14
151. 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.
16
172. 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.
18
193. 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**).
20
214. 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.
22
23- Example: Convert binary data into a 3DES key.
24
25  ```c++
26  #include "CryptoArchitectureKit/crypto_common.h"
27  #include "CryptoArchitectureKit/crypto_sym_key.h"
28
29  static OH_Crypto_ErrCode doTestDataCovertSymKey() {
30      const char *algName = "3DES192";
31      OH_CryptoSymKeyGenerator *ctx = nullptr;
32      OH_CryptoSymKey *convertKeyCtx = nullptr;
33      Crypto_DataBlob out = {.data = nullptr, .len = 0};
34      OH_Crypto_ErrCode ret;
35      uint8_t arr[] = {0xba, 0x3d, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, 0xad, 0x47, 0xfc, 0x5a,
36                      0x46, 0x39, 0xee, 0x7c, 0xba, 0x3b, 0xc2, 0x71, 0xab, 0xa0, 0x30, 0x72};
37      Crypto_DataBlob convertBlob = {.data = arr, .len = sizeof(arr)};
38      ret = OH_CryptoSymKeyGenerator_Create(algName, &ctx);
39      if (ret != CRYPTO_SUCCESS) {
40          return ret;
41      }
42      ret = OH_CryptoSymKeyGenerator_Convert(ctx, &convertBlob, &convertKeyCtx);
43      if (ret != CRYPTO_SUCCESS) {
44          OH_CryptoSymKeyGenerator_Destroy(ctx);
45          return ret;
46      }
47      ret = OH_CryptoSymKey_GetKeyData(convertKeyCtx, &out);
48      OH_CryptoSymKeyGenerator_Destroy(ctx);
49      OH_CryptoSymKey_Destroy(convertKeyCtx);
50      if (ret != CRYPTO_SUCCESS) {
51          return ret;
52      }
53      OH_Crypto_FreeDataBlob(&out);
54      return ret;
55  }
56  ```
57
58## Converting Binary Data into an HMAC Key
59
60For details about the algorithm specifications, see [HMAC](crypto-sym-key-generation-conversion-spec.md#hmac).
61
621. Obtain the HMAC binary key and encapsulate it into a [Crypto_DataBlob](../../reference/apis-crypto-architecture-kit/_crypto_common_api.md#crypto_datablob) object.
63
642. 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.
65
663. 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**).
67
684. 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.
69
70- Example: Convert binary data into an HMAC key.
71
72  ```c++
73  #include "CryptoArchitectureKit/crypto_common.h"
74  #include "CryptoArchitectureKit/crypto_sym_key.h"
75
76  static OH_Crypto_ErrCode testConvertHmacKey() {
77      const char *algName = "HMAC";
78      OH_CryptoSymKeyGenerator *ctx = nullptr;
79      OH_CryptoSymKey *convertKeyCtx = nullptr;
80      Crypto_DataBlob out = {.data = nullptr, .len = 0};
81      OH_Crypto_ErrCode ret;
82      uint8_t arr[] = "12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh";
83      Crypto_DataBlob convertBlob = {.data = arr, .len = sizeof(arr)};
84      ret = OH_CryptoSymKeyGenerator_Create(algName, &ctx);
85      if (ret != CRYPTO_SUCCESS) {
86          return ret;
87      }
88      ret = OH_CryptoSymKeyGenerator_Convert(ctx, &convertBlob, &convertKeyCtx);
89      if (ret != CRYPTO_SUCCESS) {
90          OH_CryptoSymKeyGenerator_Destroy(ctx);
91          return ret;
92      }
93      ret = OH_CryptoSymKey_GetKeyData(convertKeyCtx, &out);
94      OH_CryptoSymKeyGenerator_Destroy(ctx);
95      OH_CryptoSymKey_Destroy(convertKeyCtx);
96      if (ret != CRYPTO_SUCCESS) {
97          return ret;
98      }
99      OH_Crypto_FreeDataBlob(&out);
100      return ret;
101  }
102  ```
103