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 (**OH_CryptoSymKey**). That is, convert a piece of external or internal binary data into a key 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 #include <string.h> 76 77 static OH_Crypto_ErrCode testConvertHmacKey() { 78 const char *algName = "HMAC"; 79 OH_CryptoSymKeyGenerator *ctx = nullptr; 80 OH_CryptoSymKey *convertKeyCtx = nullptr; 81 Crypto_DataBlob out = {.data = nullptr, .len = 0}; 82 OH_Crypto_ErrCode ret; 83 84 char *arr = const_cast<char *>("12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh"); 85 Crypto_DataBlob convertBlob = {.data = (uint8_t *)(arr), .len = strlen(arr)}; 86 ret = OH_CryptoSymKeyGenerator_Create(algName, &ctx); 87 if (ret != CRYPTO_SUCCESS) { 88 return ret; 89 } 90 ret = OH_CryptoSymKeyGenerator_Convert(ctx, &convertBlob, &convertKeyCtx); 91 if (ret != CRYPTO_SUCCESS) { 92 OH_CryptoSymKeyGenerator_Destroy(ctx); 93 return ret; 94 } 95 ret = OH_CryptoSymKey_GetKeyData(convertKeyCtx, &out); 96 OH_CryptoSymKeyGenerator_Destroy(ctx); 97 OH_CryptoSymKey_Destroy(convertKeyCtx); 98 if (ret != CRYPTO_SUCCESS) { 99 return ret; 100 } 101 OH_Crypto_FreeDataBlob(&out); 102 return ret; 103 } 104 ``` 105