目录

Encryption and Decryption with an RC2 Symmetric Key (CBC Mode) (ArkTS)

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

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

Encryption

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

    For details on how to generate an RC2 symmetric key, refer to the following example and also see Symmetric Key Generation and Conversion Specifications: RC2 and Randomly Generating a Symmetric Key/Converting Binary Data to a Symmetric Key. The referenced 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, 'RC2|CBC|PKCS7') to create a Cipher instance with the symmetric key type of RC2, the block mode of CBC, and the padding mode of PKCS7, which is used to perform encryption.

  3. Call Cipher.init to initialize the Cipher instance. Specifically, set the mode to cryptoFramework.CryptoMode.ENCRYPT_MODE (encryption), key to SymKey (the key for encryption), and parameter to IvParamsSpec (IV length: 8 bytes) corresponding to the CBC mode.

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

Decryption

  1. Call cryptoFramework.createCipher with a string parameter (for example, 'RC2|CBC|PKCS7') to create a Cipher instance with the symmetric key type of RC2, the block mode of CBC, and the padding mode of PKCS7, which is used to perform decryption.

  2. Call Cipher.init to initialize the Cipher instance. Specifically, set the mode to cryptoFramework.CryptoMode.DECRYPT_MODE (decryption), key to SymKey (the key for decryption), and parameter to IvParamsSpec corresponding to the CBC mode.

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