1# Converting Binary Data into a Symmetric Key Pair (C/C++) 2 3<!--Kit: Crypto Architecture Kit--> 4<!--Subsystem: Security--> 5<!--Owner: @zxz--3--> 6<!--Designer: @lanming--> 7<!--Tester: @PAFT--> 8<!--Adviser: @zengyawen--> 9 10This 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. 11 12## Adding the Dynamic Library in the CMake Script 13```txt 14target_link_libraries(entry PUBLIC libohcrypto.so) 15``` 16 17## Converting Binary Data into a 3DES Key 18 19For details, see [3DES](crypto-sym-key-generation-conversion-spec.md#3des). 20 211. Obtain the 3DES binary key data and encapsulate it into a [Crypto_DataBlob](../../reference/apis-crypto-architecture-kit/capi-cryptocommonapi-crypto-datablob.md) object. 22 232. Call [OH_CryptoSymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-key-h.md#oh_cryptosymkeygenerator_create) with the string parameter **'3DES192'** to create a symmetric key generator (**OH_CryptoSymKeyGenerator**) object for a 192-bit 3DES key. 24 253. Call [OH_CryptoSymKeyGenerator_Convert](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-key-h.md#oh_cryptosymkeygenerator_convert) to convert the binary data into a symmetric key object (**OH_CryptoSymKey**). 26 274. Call [OH_CryptoSymKey_GetKeyData](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-key-h.md#oh_cryptosymkey_getkeydata) to obtain the binary data of the key object. 28 29Example: Convert binary data into a 3DES key. 30 31 ```c++ 32 #include "CryptoArchitectureKit/crypto_common.h" 33 #include "CryptoArchitectureKit/crypto_sym_key.h" 34 35 static OH_Crypto_ErrCode doTestDataCovertSymKey() { 36 const char *algName = "3DES192"; 37 OH_CryptoSymKeyGenerator *ctx = nullptr; 38 OH_CryptoSymKey *convertKeyCtx = nullptr; 39 Crypto_DataBlob out = {.data = nullptr, .len = 0}; 40 OH_Crypto_ErrCode ret; 41 uint8_t arr[] = {0xba, 0x3d, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, 0xad, 0x47, 0xfc, 0x5a, 42 0x46, 0x39, 0xee, 0x7c, 0xba, 0x3b, 0xc2, 0x71, 0xab, 0xa0, 0x30, 0x72}; 43 Crypto_DataBlob convertBlob = {.data = arr, .len = sizeof(arr)}; 44 ret = OH_CryptoSymKeyGenerator_Create(algName, &ctx); 45 if (ret != CRYPTO_SUCCESS) { 46 return ret; 47 } 48 ret = OH_CryptoSymKeyGenerator_Convert(ctx, &convertBlob, &convertKeyCtx); 49 if (ret != CRYPTO_SUCCESS) { 50 OH_CryptoSymKeyGenerator_Destroy(ctx); 51 return ret; 52 } 53 ret = OH_CryptoSymKey_GetKeyData(convertKeyCtx, &out); 54 OH_CryptoSymKeyGenerator_Destroy(ctx); 55 OH_CryptoSymKey_Destroy(convertKeyCtx); 56 if (ret != CRYPTO_SUCCESS) { 57 return ret; 58 } 59 OH_Crypto_FreeDataBlob(&out); 60 return ret; 61 } 62 ``` 63 64## Converting Binary Data into an HMAC Key 65 66For details, see [HMAC](crypto-sym-key-generation-conversion-spec.md#hmac). 67 681. Obtain the HMAC binary key and encapsulate it into a [Crypto_DataBlob](../../reference/apis-crypto-architecture-kit/capi-cryptocommonapi-crypto-datablob.md) object. 69 702. Call [OH_CryptoSymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-key-h.md#oh_cryptosymkeygenerator_create) with the string parameter **'HMAC'** to create a symmetric key generator (**OH_CryptoSymKeyGenerator**) object for a 32768-bit HMAC key. 71 723. Call [OH_CryptoSymKeyGenerator_Convert](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-key-h.md#oh_cryptosymkeygenerator_convert) to convert the binary data into a symmetric key object (**OH_CryptoSymKey**). 73 744. Call [OH_CryptoSymKey_GetKeyData](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-key-h.md#oh_cryptosymkey_getkeydata) to obtain the binary data of the key object. 75 76Example: Convert binary data into an HMAC key. 77 78 ```c++ 79 #include "CryptoArchitectureKit/crypto_common.h" 80 #include "CryptoArchitectureKit/crypto_sym_key.h" 81 #include <string.h> 82 83 static OH_Crypto_ErrCode testConvertHmacKey() { 84 const char *algName = "HMAC"; 85 OH_CryptoSymKeyGenerator *ctx = nullptr; 86 OH_CryptoSymKey *convertKeyCtx = nullptr; 87 Crypto_DataBlob out = {.data = nullptr, .len = 0}; 88 OH_Crypto_ErrCode ret; 89 90 char *arr = const_cast<char *>("12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh"); 91 Crypto_DataBlob convertBlob = {.data = (uint8_t *)(arr), .len = strlen(arr)}; 92 ret = OH_CryptoSymKeyGenerator_Create(algName, &ctx); 93 if (ret != CRYPTO_SUCCESS) { 94 return ret; 95 } 96 ret = OH_CryptoSymKeyGenerator_Convert(ctx, &convertBlob, &convertKeyCtx); 97 if (ret != CRYPTO_SUCCESS) { 98 OH_CryptoSymKeyGenerator_Destroy(ctx); 99 return ret; 100 } 101 ret = OH_CryptoSymKey_GetKeyData(convertKeyCtx, &out); 102 OH_CryptoSymKeyGenerator_Destroy(ctx); 103 OH_CryptoSymKey_Destroy(convertKeyCtx); 104 if (ret != CRYPTO_SUCCESS) { 105 return ret; 106 } 107 OH_Crypto_FreeDataBlob(&out); 108 return ret; 109 } 110 ``` 111