harmony 鸿蒙Randomly Generating a Symmetric Key (ArkTS)

  • 2025-06-12
  • 浏览 (5)

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 (SymKey) object created can be used for subsequent encryption and decryption operations, and the binary data can be used for key storage and transfer.

Randomly Generating an AES Key

For details about the algorithm specifications, see 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.

  4. Example: Randomly generate a 256-bit AES key (using promise-based APIs).

  import { cryptoFramework } from '@kit.CryptoArchitectureKit';

  function testGenerateAesKey() {
    // Create a SymKeyGenerator instance.
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES256');
    // Use SymKeyGenerator to randomly generate a symmetric key.
    let promiseSymKey = symKeyGenerator.generateSymKey();
    promiseSymKey.then(key => {
      // Obtain the binary data of the symmetric key and output a 256-bit key, which is of 32 bytes.
      let encodedKey = key.getEncoded();
      console.info('key hex:' + encodedKey.data);
    });
  }
  • Example using synchronous API generateSymKeySync: “`ts import { cryptoFramework } from ‘@kit.CryptoArchitectureKit’;

function testSyncGenerateAesKey() { // Create a SymKeyGenerator instance. let symKeyGenerator = cryptoFramework.createSymKeyGenerator(‘AES256’); // Use SymKeyGenerator to randomly generate a symmetric key. let promiseSymKey = symKeyGenerator.generateSymKeySync(); // Obtain the binary data of the symmetric key and output a 256-bit key, which is of 32 bytes. let encodedKey = promiseSymKey.getEncoded(); console.info(‘key hex:’ + encodedKey.data); }



## Randomly Generating an SM4 Key

For details about the algorithm specifications, see [SM4](crypto-sym-key-generation-conversion-spec.md#sm4).

1. Call [cryptoFramework.createSymKeyGenerator](https://m.seaxiang.com/blog/GRG9o4) 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](https://m.seaxiang.com/blog/GRG9o4) to randomly generate a symmetric key (**SymKey**) object.

3. Call [SymKey.getEncoded](https://m.seaxiang.com/blog/GRG9o4) to obtain the binary data of the key.

- Example: Randomly generate a 128-bit SM4 key (using promise-based APIs).

  ```ts
  import { cryptoFramework } from '@kit.CryptoArchitectureKit';

  function testGenerateSM4Key() {
    // Create a SymKeyGenerator instance.
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator('SM4_128');
    // Use SymKeyGenerator to randomly generate a symmetric key.
    let promiseSymKey = symKeyGenerator.generateSymKey();
    promiseSymKey.then(key => {
      // Obtain the binary data of the symmetric key and output a 128-bit byte stream, which is of 16 bytes.
      let encodedKey = key.getEncoded();
      console.info('key hex:' + encodedKey.data);
    });
  }
  • Example using synchronous API generateSymKeySync: “`ts import { cryptoFramework } from ‘@kit.CryptoArchitectureKit’;

function testSyncGenerateSm4Key() { // Create a SymKeyGenerator instance. let symKeyGenerator = cryptoFramework.createSymKeyGenerator(‘SM4_128’); // Use SymKeyGenerator to randomly generate a symmetric key. let promiseSymKey = symKeyGenerator.generateSymKeySync(); // Obtain the binary data of the symmetric key and output a 128-bit byte stream, which is of 16 bytes. let encodedKey = promiseSymKey.getEncoded(); console.info(‘key hex:’ + encodedKey.data); } “`

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Crypto Architecture Kit

harmony 鸿蒙Encryption and Decryption with a 3DES Symmetric Key (ECB Mode) (C/C++)

harmony 鸿蒙Encryption and Decryption with a 3DES Symmetric Key (ECB Mode) (ArkTS)

harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (CBC Mode) (C/C++)

harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (CBC Mode) (ArkTS)

harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (CCM Mode) (C/C++)

harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (CCM Mode) (ArkTS)

harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (ECB Mode) (C/C++)

harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (ECB Mode) (ArkTS)

harmony 鸿蒙Encryption and Decryption by Segment with an AES Symmetric Key (GCM Mode) (C/C++)

0  赞