1# Randomly Generating a Symmetric Key (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 10The following uses AES and SM4 as an example to describe how to randomly generate a symmetric key (**OH_CryptoSymKey**). 11 12The symmetric key (**OH_CryptoSymKey**) object created can be used for subsequent encryption and decryption operations, and the binary data can be used for key storage or transfer. 13 14## Adding the Dynamic Library in the CMake Script 15```txt 16target_link_libraries(entry PUBLIC libohcrypto.so) 17``` 18 19## Randomly Generating an AES Key 20 21For details about the algorithm specifications, see [AES](crypto-sym-key-generation-conversion-spec.md#aes). 22 231. Call [OH_CryptoSymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-key-h.md#oh_cryptosymkeygenerator_create) with the string parameter **'AES256'** to create a symmetric key generator (**OH_CryptoSymKeyGenerator**) object for a 256-bit AES key. 24 252. Call [OH_CryptoSymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-key-h.md#oh_cryptosymkeygenerator_generate) to randomly generate a symmetric key object (**OH_CryptoSymKey**). 26 273. 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 29```c++ 30#include "CryptoArchitectureKit/crypto_common.h" 31#include "CryptoArchitectureKit/crypto_sym_key.h" 32 33static OH_Crypto_ErrCode testGenerateSymKey() 34{ 35 OH_CryptoSymKeyGenerator *ctx = nullptr; 36 OH_CryptoSymKey *keyCtx = nullptr; 37 Crypto_DataBlob out = {.data = nullptr, .len = 0}; 38 OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("AES256", &ctx); 39 if (ret != CRYPTO_SUCCESS) { 40 return ret; 41 } 42 ret = OH_CryptoSymKeyGenerator_Generate(ctx, &keyCtx); 43 if (ret != CRYPTO_SUCCESS) { 44 OH_CryptoSymKeyGenerator_Destroy(ctx); 45 return ret; 46 } 47 ret = OH_CryptoSymKey_GetKeyData(keyCtx, &out); 48 OH_CryptoSymKeyGenerator_Destroy(ctx); 49 OH_CryptoSymKey_Destroy(keyCtx); 50 if (ret != CRYPTO_SUCCESS) { 51 return ret; 52 } 53 OH_Crypto_FreeDataBlob(&out); 54 return ret; 55} 56``` 57 58## Randomly Generating an SM4 Key 59 60For details about the algorithm specifications, see [SM4](crypto-sym-key-generation-conversion-spec.md#sm4). 61 621. Call [OH_CryptoSymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-key-h.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. 63 642. Call [OH_CryptoSymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/capi-crypto-sym-key-h.md#oh_cryptosymkeygenerator_generate) to randomly generate a symmetric key object (**OH_CryptoSymKey**). 65 663. 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. 67 68```c++ 69#include "CryptoArchitectureKit/crypto_common.h" 70#include "CryptoArchitectureKit/crypto_sym_key.h" 71 72static OH_Crypto_ErrCode testGenerateSM4Key() 73{ 74 OH_CryptoSymKeyGenerator *ctx = nullptr; 75 OH_CryptoSymKey *keyCtx = nullptr; 76 Crypto_DataBlob out = {.data = nullptr, .len = 0}; // Binary data of the symmetric key. 77 OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("SM4_128", &ctx); // Create a symmetric key generator. 78 if (ret != CRYPTO_SUCCESS) { 79 return ret; 80 } 81 ret = OH_CryptoSymKeyGenerator_Generate(ctx, &keyCtx); // Randomly generate a symmetric key object. 82 if (ret != CRYPTO_SUCCESS) { 83 OH_CryptoSymKeyGenerator_Destroy(ctx); 84 return ret; 85 } 86 ret = OH_CryptoSymKey_GetKeyData(keyCtx, &out); // Obtain the binary data of the symmetric key object. 87 OH_CryptoSymKeyGenerator_Destroy(ctx); 88 OH_CryptoSymKey_Destroy(keyCtx); 89 if (ret != CRYPTO_SUCCESS) { 90 return ret; 91 } 92 OH_Crypto_FreeDataBlob(&out); 93 return ret; 94} 95``` 96