1# Randomly Generating a Symmetric Key (C/C++) 2 3 4The following uses AES and SM4 as an example to describe how to randomly generate a symmetric key (**OH_CryptoSymKey**). 5 6The symmetric key object can be used for subsequent encryption and decryption. 7 8## Adding the Dynamic Library in the CMake Script 9```txt 10 target_link_libraries(entry PUBLIC libohcrypto.so) 11``` 12 13## Randomly Generating an AES Key 14 15For details about the algorithm specifications, see [AES](crypto-sym-key-generation-conversion-spec.md#aes). 16 171. Use [OH_CryptoSymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkeygenerator_create) with the string parameter **'AES256'** to create a symmetric key generator (**OH_CryptoSymKeyGenerator**) object for a 256-bit AES key. 182. Use [OH_CryptoSymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkeygenerator_generate) to randomly generate a symmetric key object (**OH_CryptoSymKey**). 193. 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. 20 21**Example** 22 23 24```c++ 25#include "CryptoArchitectureKit/crypto_common.h" 26#include "CryptoArchitectureKit/crypto_sym_key.h" 27 28static OH_Crypto_ErrCode testGenerateSymKey() 29{ 30 OH_CryptoSymKeyGenerator *ctx = nullptr; 31 OH_CryptoSymKey *keyCtx = nullptr; 32 Crypto_DataBlob out = {.data = nullptr, .len = 0}; 33 OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("AES256", &ctx); 34 if (ret != CRYPTO_SUCCESS) { 35 return ret; 36 } 37 ret = OH_CryptoSymKeyGenerator_Generate(ctx, &keyCtx); 38 if (ret != CRYPTO_SUCCESS) { 39 OH_CryptoSymKeyGenerator_Destroy(ctx); 40 return ret; 41 } 42 ret = OH_CryptoSymKey_GetKeyData(keyCtx, &out); 43 OH_CryptoSymKeyGenerator_Destroy(ctx); 44 OH_CryptoSymKey_Destroy(keyCtx); 45 if (ret != CRYPTO_SUCCESS) { 46 return ret; 47 } 48 OH_Crypto_FreeDataBlob(&out); 49 return ret; 50} 51``` 52 53## Randomly Generating an SM4 Key 54 55For details about the algorithm specifications, see [SM4](crypto-sym-key-generation-conversion-spec.md#sm4). 56 571. Use [OH_CryptoSymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkeygenerator_create) with the string parameter **'SM4_128'** to create a symmetric key generator (**OH_CryptoSymKeyGenerator**) object for a 128-bit SM4 key. 582. Use [OH_CryptoSymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/_crypto_sym_key_api.md#oh_cryptosymkeygenerator_generate) to randomly generate a symmetric key object (**OH_CryptoSymKey**). 593. 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. 60 61**Example** 62 63 64```c++ 65#include "CryptoArchitectureKit/crypto_common.h" 66#include "CryptoArchitectureKit/crypto_sym_key.h" 67 68static OH_Crypto_ErrCode testGenerateSM4Key() 69{ 70 OH_CryptoSymKeyGenerator *ctx = nullptr; 71 OH_CryptoSymKey *keyCtx = nullptr; 72 Crypto_DataBlob out = {.data = nullptr, .len = 0}; 73 OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("SM4_128", &ctx); 74 if (ret != CRYPTO_SUCCESS) { 75 return ret; 76 } 77 ret = OH_CryptoSymKeyGenerator_Generate(ctx, &keyCtx); 78 if (ret != CRYPTO_SUCCESS) { 79 OH_CryptoSymKeyGenerator_Destroy(ctx); 80 return ret; 81 } 82 ret = OH_CryptoSymKey_GetKeyData(keyCtx, &out); 83 OH_CryptoSymKeyGenerator_Destroy(ctx); 84 OH_CryptoSymKey_Destroy(keyCtx); 85 if (ret != CRYPTO_SUCCESS) { 86 return ret; 87 } 88 OH_Crypto_FreeDataBlob(&out); 89 return ret; 90} 91``` 92