目录

Converting Binary Data into a Symmetric Key (ArkTS)

This topic uses 3DES and HMAC as an example to describe how to convert binary data into a symmetric key (SymKey). That is, convert a piece of external or internal binary data into a key object for subsequent operations, such as encryption and decryption.

Converting Binary Data into a 3DES Key

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

  1. Obtain the 3DES binary key data and encapsulate it into a DataBlob object.

  2. Call cryptoFramework.createSymKeyGenerator with the string parameter '3DES192' to create a symmetric key generator (SymKeyGenerator) object for a 192-bit 3DES key.

  3. Call SymKeyGenerator.convertKey to convert the binary data into a symmetric key (SymKey).

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

Converting Binary Data into an HMAC Key

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

  1. Obtain the HMAC binary key data and encapsulate it into a DataBlob object.

  2. Call cryptoFramework.createSymKeyGenerator with the string parameter 'HMAC' to create a symmetric key generator (SymKeyGenerator) for the HMAC algorithm, which supports key lengths ranging from [1, 4096] bytes.

  3. Call SymKeyGenerator.convertKey to convert the binary data into a symmetric key (SymKey).

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