目录

Randomly Generating a Symmetric Key (ArkTS)

This topic uses AES and SM4 as an example to describe how to generate a symmetric key and obtain the binary data.

The symmetric key object may be used for subsequent encryption and decryption operations, and binary data may be used for storage or transmission.

Randomly Generating an AES Key

For the corresponding algorithm specifications, see Symmetric Key Generation and Conversion Specifications: AES.

  1. Call cryptoFramework.createSymKeyGenerator with the string parameter 'AES256' to create a symmetric key generator (SymKeyGenerator) object for a 256-bit AES key.

  2. Call SymKeyGenerator.generateSymKey to randomly generate a symmetric key (SymKey) object.

  3. Call SymKey.getEncoded to obtain the binary data of the key.

Randomly Generating an SM4 Key

For the corresponding algorithm specifications, see Symmetric Key Generation and Conversion Specifications: SM4.

  1. Call cryptoFramework.createSymKeyGenerator with the string parameter 'SM4_128' to create a symmetric key generator (SymKeyGenerator) object for a 128-bit SM4 key.

    If you need to use other algorithms, modify the string parameter.

  2. Call SymKeyGenerator.generateSymKey to randomly generate a symmetric key (SymKey) object.

  3. Call SymKey.getEncoded to obtain the binary data of the key.