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 (**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.
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.
18
192. 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**).
20
213. 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**Example**
23
24
25```c++
26#include "CryptoArchitectureKit/crypto_common.h"
27#include "CryptoArchitectureKit/crypto_sym_key.h"
28
29static OH_Crypto_ErrCode testGenerateSymKey()
30{
31    OH_CryptoSymKeyGenerator *ctx = nullptr;
32    OH_CryptoSymKey *keyCtx = nullptr;
33    Crypto_DataBlob out = {.data = nullptr, .len = 0};
34    OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("AES256", &ctx);
35    if (ret != CRYPTO_SUCCESS) {
36        return ret;
37    }
38    ret = OH_CryptoSymKeyGenerator_Generate(ctx, &keyCtx);
39    if (ret != CRYPTO_SUCCESS) {
40        OH_CryptoSymKeyGenerator_Destroy(ctx);
41        return ret;
42    }
43    ret = OH_CryptoSymKey_GetKeyData(keyCtx, &out);
44    OH_CryptoSymKeyGenerator_Destroy(ctx);
45    OH_CryptoSymKey_Destroy(keyCtx);
46    if (ret != CRYPTO_SUCCESS) {
47        return ret;
48    }
49    OH_Crypto_FreeDataBlob(&out);
50    return ret;
51}
52```
53
54## Randomly Generating an SM4 Key
55
56For details about the algorithm specifications, see [SM4](crypto-sym-key-generation-conversion-spec.md#sm4).
57
581. 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.
59
602. 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**).
61
623. 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.
63**Example**
64
65
66```c++
67#include "CryptoArchitectureKit/crypto_common.h"
68#include "CryptoArchitectureKit/crypto_sym_key.h"
69
70static OH_Crypto_ErrCode testGenerateSM4Key()
71{
72    OH_CryptoSymKeyGenerator *ctx = nullptr;
73    OH_CryptoSymKey *keyCtx = nullptr;
74    Crypto_DataBlob out = {.data = nullptr, .len = 0};
75    OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("SM4_128", &ctx);
76    if (ret != CRYPTO_SUCCESS) {
77        return ret;
78    }
79    ret = OH_CryptoSymKeyGenerator_Generate(ctx, &keyCtx);
80    if (ret != CRYPTO_SUCCESS) {
81        OH_CryptoSymKeyGenerator_Destroy(ctx);
82        return ret;
83    }
84    ret = OH_CryptoSymKey_GetKeyData(keyCtx, &out);
85    OH_CryptoSymKeyGenerator_Destroy(ctx);
86    OH_CryptoSymKey_Destroy(keyCtx);
87    if (ret != CRYPTO_SUCCESS) {
88        return ret;
89    }
90    OH_Crypto_FreeDataBlob(&out);
91    return ret;
92}
93```
94