目录

Encryption and Decryption with an RC4 Symmetric Key (ArkTS)

The Crypto framework supports encryption and decryption with an RC4 symmetric key since API version 26.0.0.

For the corresponding algorithm specifications, see Symmetric Key Encryption and Decryption Algorithm Specifications: RC4.

RC4 is a stream cipher algorithm and does not require a block cipher mode or padding.

Encryption

  1. Call cryptoFramework.createSymKeyGenerator, SymKeyGenerator.generateSymKey, or SymKeyGenerator.convertKey to generate a symmetric key (SymKey) with the RC4 algorithm and a key length ranging from 8 bits to 4096 bits (for example, RC4_128).

    For details about how to generate an RC4 symmetric key, refer to the example below, together with Symmetric Key Generation and Conversion Specifications: RC4 and Converting Binary Data into a Symmetric Key. Note that the reference documents may differ from the current example in input parameters. Pay attention to these differences when reading.

  2. Call cryptoFramework.createCipher with a string parameter (for example, 'RC4') to create a Cipher instance of the RC4 symmetric key type for encryption.

  3. Call Cipher.init to initialize the Cipher instance. Specifically, set the mode to cryptoFramework.CryptoMode.ENCRYPT_MODE (encryption) and key to SymKey (the key used for encryption). RC4 is a stream cipher. If there are no encryption/decryption parameters such as IV, null will be passed.

  4. To encrypt a small amount of data, simply call Cipher.doFinal.

Decryption

  1. Call cryptoFramework.createCipher with a string parameter (for example, 'RC4') to create a Cipher instance of the RC4 symmetric key type for decryption.

  2. Call Cipher.init to initialize the Cipher instance. Specifically, set the mode to cryptoFramework.CryptoMode.DECRYPT_MODE (decryption) and key to SymKey (the key used for decryption). If there is no encryption or decryption parameter, null will be passed.

  3. To decrypt a small amount of data, simply call Cipher.doFinal.