• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
10target_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. Call [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. Call [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. Call [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
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. Call [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.
58
592. Call [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**).
60
613. Call [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.
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}; // Binary data of the symmetric key.
73    OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("SM4_128", &ctx); // Create a symmetric key generator.
74    if (ret != CRYPTO_SUCCESS) {
75        return ret;
76    }
77    ret = OH_CryptoSymKeyGenerator_Generate(ctx, &keyCtx); // Randomly generate a symmetric key object.
78    if (ret != CRYPTO_SUCCESS) {
79        OH_CryptoSymKeyGenerator_Destroy(ctx);
80        return ret;
81    }
82    ret = OH_CryptoSymKey_GetKeyData(keyCtx, &out); // Obtain the binary data of the symmetric key object.
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