目录

Encryption and Decryption with a ChaCha20 Symmetric Key Pair (ArkTS)

Starting from API version 22, the algorithm library supports this algorithm.

Using a ChaCha20 Symmetric Key for Encryption and Decryption

Creating an Object

Call cryptoFramework.createSymKeyGenerator and SymKeyGenerator.convertKey to generate a symmetric key (SymKey) using ChaCha20.

For details about how to generate a ChaCha20 symmetric key, refer to the example below, together with Randomly Generating a Symmetric Key. Note that the reference documents and the example may differ in input parameters. Pay attention to the differences.

Encryption

  1. Call cryptoFramework.createCipher and specify the string parameter 'ChaCha20' to create a Cipher instance of the symmetric key for encryption.

  2. 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.

  3. Call Cipher.update to pass in the data to be encrypted (plaintext).

  4. Call Cipher.doFinal to obtain the encrypted data.

    Since data has been passed in through update(), pass null for data here.

    The output of doFinal() may be null. Before accessing the specific data, check whether the result is null to avoid exceptions.

Decryption

  1. Call cryptoFramework.createCipher and specify the string parameter 'ChaCha20' to create a Cipher instance of the symmetric key for 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.

  3. Call Cipher.update to pass in the data to be decrypted (ciphertext).

  4. Call Cipher.doFinal to obtain the decrypted data.

Using a ChaCha20 Symmetric Key (Poly1305 Mode) for Encryption/Decryption

Creating an Object

Call cryptoFramework.createSymKeyGenerator and SymKeyGenerator.convertKey to generate a symmetric key (SymKey) using ChaCha20.

For details about how to generate a ChaCha20 symmetric key, refer to the example below, together with Randomly Generating a Symmetric Key. Note that the reference documents and the example may differ in input parameters. Pay attention to the differences.

Encryption

  1. Call cryptoFramework.createCipher with the string parameter 'ChaCha20|Poly1305' to create a Cipher instance with the ChaCha20 symmetric key type and Poly1305 mode, used to complete the encryption operation.

  2. Call Cipher.init to set the mode to encryption (cryptoFramework.CryptoMode.ENCRYPT_MODE), specify the encryption key (SymKey) and the encryption parameters (Poly1305ParamsSpec) for Poly1305 mode, and initialize the Cipher instance for encryption.

  3. Call Cipher.update to pass in the data to be encrypted (plaintext).

  4. Call Cipher.doFinal to obtain the encrypted data.

    Since data has been passed in through update(), pass null for data here.

    The output of doFinal() may be null. Before accessing the specific data, check whether the result is null to avoid exceptions.

  5. Read Poly1305ParamsSpec.authTag as the authentication information for decryption.

    In Poly1305 mode, extract the last 16 bytes from the encrypted data as the authentication information for decryption initialization.

Decryption

  1. Call cryptoFramework.createCipher with the string parameter 'ChaCha20|Poly1305' to create a Cipher instance with the ChaCha20 symmetric key type and Poly1305 mode, used to complete the encryption operation.

  2. Call Cipher.init to set the mode to decryption (cryptoFramework.CryptoMode.DECRYPT_MODE), specify the decryption key (SymKey) and the decryption parameters (Poly1305ParamsSpec) for Poly1305 mode, and initialize the Cipher instance for decryption.

  3. Call Cipher.update to pass in the data to be decrypted (ciphertext).

  4. Call Cipher.doFinal to obtain the decrypted data.

If ChaCha20 (Poly1305 mode) decryption fails with error code 17630001, see doFinal Call Failure During ChaCha20-Poly1305 Decryption.