目录

Encryption and Decryption with an AES Symmetric Key (ArkTS)

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

Using an AES Symmetric Key (GCM Mode) for Encryption and Decryption

Encryption

  1. Call cryptoFramework.createSymKeyGenerator to generate a 128-bit AES symmetric key (SymKey). Then call SymKeyGenerator.convertKey to generate the symmetric key.

    To generate an AES symmetric key, you can refer to the following example and read it together with Symmetric Key Generation and Conversion Specifications: AES and Randomly Generating a Symmetric Key. Note that the reference documents may differ from the current example in input parameters.

  2. Call cryptoFramework.createCipher with the string parameter 'AES128|GCM' to create a Cipher instance with the symmetric key type AES128 and the block mode GCM, which is used to complete the encryption operation.

  3. Call Cipher.init to set the mode to encryption (cryptoFramework.CryptoMode.ENCRYPT_MODE), specify the encryption key (SymKey) and the encryption parameters corresponding to the GCM mode (GcmParamsSpec), and initialize the encryption Cipher instance.

  4. Call Cipher.update to update the data (plaintext).

    A single update call has no length limit. You can call update based on the data volume.

  5. Call Cipher.doFinal to obtain the encrypted data. Note that because the data has already been passed in through update, pass null for data here. The doFinal output may be null. Before accessing specific data, check whether the result is null to avoid exceptions.

  6. Read GcmParamsSpec.authTag as the authentication information for decryption.

    In GCM mode, during a single encryption process, concatenating the result of each update and the final doFinal yields "ciphertext + authTag", where authTag is the last 16 bytes and the rest is ciphertext. If null is passed to the data parameter of doFinal, the doFinal result is the authTag.

Decryption

  1. Call cryptoFramework.createCipher with the string parameter 'AES128|GCM' to create a Cipher instance with the symmetric key type AES128 and the block mode GCM, which is used to complete the decryption operation.

  2. Call Cipher.init to set the mode to decryption (cryptoFramework.CryptoMode.DECRYPT_MODE), specify the decryption key (SymKey) and the decryption parameters corresponding to the GCM mode (GcmParamsSpec), and initialize the decryption Cipher instance.

  3. Call Cipher.update to update the data (ciphertext).

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

If AES (GCM mode) decryption fails and returns error code 17630001, see Failed to Call doFinal During Decryption Using AES-GCM.

Using an AES Symmetric Key (CCM Mode) for Encryption and Decryption

Encryption

  1. Call cryptoFramework.createSymKeyGenerator and SymKeyGenerator.convertKey to generate a 128-bit AES symmetric key (SymKey).

    For details about how to generate an AES symmetric key, you can refer to the example below, together with Symmetric Key Generation and Conversion Specifications: AES and Randomly Generating a Symmetric Key for understanding. The reference documents may have differences in input parameters compared with the current example. Note the difference.

  2. Call cryptoFramework.createCipher with the specified string parameter 'AES128|CCM' to create a Cipher instance with the symmetric key algorithm AES128 and the block mode CCM, used to complete the encryption operation.

  3. Call Cipher.init to set the mode to encryption (cryptoFramework.CryptoMode.ENCRYPT_MODE), specify the key (SymKey) and the encryption parameters (CcmParamsSpec) corresponding to the CCM mode, and initialize the encryption Cipher instance.

  4. Call Cipher.update to update the data (plaintext).

    The current single update has no length limit. You can decide how to call update based on the data volume.

    The CCM mode does not support segmented encryption and decryption.

  5. Call Cipher.doFinal to get the data after encryption.

    • Since data has been passed through update, pass null here.
    • The doFinal output may be null. Before accessing the specific data, check whether the result is null to avoid exceptions.
  6. Read CcmParamsSpec.authTag as the authentication information for decryption.

    In CCM mode, during a one-time encryption process, concatenate the result of each update and the final doFinal to obtain "ciphertext + authTag", where authTag is the 12 bytes at the end. The remaining part is all ciphertext. If the data parameter of doFinal is passed as null, the result of doFinal is authTag.

Decryption

  1. Call cryptoFramework.createCipher with the string parameter 'AES128|CCM' to create a Cipher instance with AES128 symmetric key and CCM block mode, used to complete the decryption operation.

  2. Call Cipher.init to set the mode to decryption (cryptoFramework.CryptoMode.DECRYPT_MODE), specify the key (SymKey) and the decryption parameters corresponding to CCM mode (CcmParamsSpec), and initialize the decryption Cipher instance.

  3. Call Cipher.doFinal to obtain the data after decryption.

If AES (CCM mode) decryption fails and returns error code 17630001, see Failed to Call doFinal During Decryption Using AES-CCM.

Using an AES Symmetric Key (CCM Mode) with AeadParamsSpec for Encryption and Decryption

Starting from API version 26.0.0, encryption and decryption with an AES symmetric key (CCM mode) supports the AeadParamsSpec parameter.

Use the AeadParamsSpec parameter when you do not want to explicitly save the authTag or when you need to specify the length of the authTag.

Key generation

Call cryptoFramework.createSymKeyGenerator and SymKeyGenerator.convertKey to generate a 128-bit AES symmetric key (SymKey).

For details about how to generate an AES symmetric key, refer to the following example together with the symmetric key generation and conversion specifications: AES and Randomly Generating a Symmetric Key. Note that the reference documents may differ from the current example in input parameters.

Encryption

  1. Call cryptoFramework.createCipher with the string parameter 'AES128|CCM' to create a Cipher instance with the AES128 symmetric key and the CCM block mode, which is used to perform the encryption operation.

  2. Call Cipher.init to set the mode to encryption (cryptoFramework.CryptoMode.ENCRYPT_MODE), specify the key (SymKey) and the encryption parameters corresponding to the CCM mode (AeadParamsSpec), and initialize the encryption Cipher instance.

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

    When AeadParamsSpec is used, in CCM mode you can call only one of update and doFinal for encryption, and each method can be called only once.

    When a Cipher instance is initialized with AeadParamsSpec, the authentication tag (authTag) is automatically appended to the end of the ciphertext and does not need to be saved separately.

    In CCM mode with the AeadParamsSpec structure, tagLen supports 4, 6, 8, 10, 12, 14, and 16. If it is not passed, the default value is 12. The unit is byte.

Decryption

  1. Call cryptoFramework.createCipher with the string parameter 'AES128|CCM' to create a Cipher instance with the symmetric key type AES128 and the block mode CCM, which is used to complete the decryption operation.

  2. Call Cipher.init to set the mode to decryption (cryptoFramework.CryptoMode.DECRYPT_MODE), specify the key (SymKey) and the decryption parameters (AeadParamsSpec) corresponding to the CCM mode, and initialize the decryption Cipher instance.

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

If AES (CCM mode) decryption fails and returns error code 17630001, see Failed to Call doFinal During Decryption Using AES-CCM

Using an AES Symmetric Key (CBC Mode) for Encryption and Decryption

Encryption

  1. Call cryptoFramework.createSymKeyGenerator and SymKeyGenerator.convertKey to generate a 128-bit AES symmetric key (SymKey).

    For how to generate an AES symmetric key, you can refer to the example below, and understand it together with Symmetric Key Generation and Conversion Specifications: AES and Randomly Generating a Symmetric Key. The reference documents may have differences in input parameters from the current example, so please note the difference when reading.

  2. Call cryptoFramework.createCipher, specify the string parameter 'AES128|CBC|PKCS7', and create a Cipher instance with the symmetric key type AES128, block mode CBC, and padding mode PKCS7, used to complete the encryption operation.

  3. Call Cipher.init, set the mode to encryption (cryptoFramework.CryptoMode.ENCRYPT_MODE), specify the encryption key (SymKey) and the encryption parameters corresponding to CBC mode (IvParamsSpec), and initialize the encryption Cipher instance.

  4. When the content to be encrypted is short, you can directly call Cipher.doFinal without calling update to obtain the data after encryption.

Decryption

  1. Call cryptoFramework.createCipher, specify the string parameter 'AES128|CBC|PKCS7', and create a Cipher instance with the symmetric key type AES128, block mode CBC, and padding mode PKCS7, used to complete the decryption operation.

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

  3. When the decrypted content is short, you can skip calling update and directly call Cipher.doFinal to obtain the data after decryption.

If AES (CBC mode) decryption fails and returns error code 17630001, see Failed to Call doFinal During Decryption Using AES-CBC.

Using an AES Symmetric Key (ECB Mode) for Encryption and Decryption

Encryption

  1. Call cryptoFramework.createSymKeyGenerator and SymKeyGenerator.convertKey to generate a 128-bit AES symmetric key (SymKey).

    To learn how to generate an AES symmetric key, refer to the following example together with Symmetric Key Generation and Conversion Specifications: AES and Randomly Generating a Symmetric Key. The reference documents may differ from the current example in input parameters, so note the difference when reading them.

  2. Call cryptoFramework.createCipher with the string parameter 'AES128|ECB|PKCS7' to create a Cipher instance with the symmetric key type AES128, block mode ECB, and padding mode PKCS7, which is used to complete the encryption operation.

  3. Call Cipher.init to set the mode to encryption (cryptoFramework.CryptoMode.ENCRYPT_MODE), specify the encryption key (SymKey), and leave the ECB mode Params empty to initialize the encryption Cipher instance.

  4. When the content to encrypt is short, you can call Cipher.doFinal directly without calling update to obtain the encrypted data.

Decryption

  1. Call cryptoFramework.createCipher with the string parameter 'AES128|ECB|PKCS7' to create a Cipher instance with the symmetric key type AES128, block mode ECB, and padding mode PKCS7, which is used to complete the decryption operation.

  2. Call Cipher.init to set the mode to decryption (cryptoFramework.CryptoMode.DECRYPT_MODE), specify the decryption key (SymKey), and leave the ECB mode Params empty to initialize the decryption Cipher instance.

  3. If the content to decrypt is short, you can skip update and directly call Cipher.doFinal to obtain the data after decryption.

If AES (ECB mode) decryption fails and returns error code 17630001, see Failed to Call doFinal During Decryption Using AES-ECB.

Using an AES Symmetric Key (GCM Mode) for Segmented Encryption and Decryption

Encryption

  1. Call cryptoFramework.createSymKeyGenerator and SymKeyGenerator.convertKey to generate a 128-bit AES symmetric key (SymKey).

    To learn how to generate an AES symmetric key, you can refer to the following example and read Symmetric Key Generation and Conversion Specifications: AES and Randomly Generating a Symmetric Key for understanding. The reference documents may have differences in input parameters from the current example, so note the difference when reading.

  2. Call cryptoFramework.createCipher and specify the string parameter 'AES128|GCM|PKCS7' to create a Cipher instance with the symmetric key type AES128, the block mode GCM, and the padding mode PKCS7, which is used to complete the encryption operation.

  3. Call Cipher.init to set the mode to encryption (cryptoFramework.CryptoMode.ENCRYPT_MODE), specify the encryption key (SymKey) and the encryption parameters corresponding to the GCM mode (GcmParamsSpec), and initialize the encryption Cipher instance.

  4. Set the amount of data passed in at a time to 20 bytes, and call Cipher.update multiple times to update the data (plaintext).

    • There is no limit on the length of a single update. You can determine how to call update based on the amount of data.

    • You are advised to check whether the result of each update is null, and when the result is not null, extract the data from it and concatenate it to form the complete ciphertext. This is because in different modes, the result of update may be affected differently.

      1. For example, in ECB and CBC modes, encryption is always performed with a block as the basic unit, and the encrypted block result generated by this update is output. That is, when the current update operation fills a complete block, the ciphertext is output; if it does not fill a block, this update outputs null, and the unencrypted data is concatenated with the data input next time before being grouped and output. Finally, in doFinal, the unencrypted data is padded according to the specified padding mode, and the remaining encryption result is output. The update operation in the decryption process follows the same principle.

      2. For stream cipher modes such as CTR and OFB, the ciphertext length equals the plaintext length.

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

    • Because data has already been passed in through update, pass null here.
    • Before accessing the doFinal output result, check whether the result is null to avoid exceptions.
  6. Read GcmParamsSpec.authTag as the decryption authentication information.

    In GCM mode, during a single encryption process, concatenating the result of each update and the final doFinal yields "ciphertext + authTag", where authTag is the last 16 bytes. The remaining part is all ciphertext. If the data parameter of doFinal is null, the result of doFinal is the authTag.

Decryption

  1. Call cryptoFramework.createCipher with the string parameter 'AES128|GCM|PKCS7' to create a Cipher instance with the symmetric key type AES128, block mode GCM, and padding mode PKCS7, used to complete the decryption operation.

  2. Call Cipher.init to set the mode to decryption (cryptoFramework.CryptoMode.DECRYPT_MODE), specify the decryption key (SymKey) and the decryption parameters corresponding to GCM mode (GcmParamsSpec), and initialize the decryption Cipher instance.

  3. Set the amount of data passed in at a time to 20 bytes, and call Cipher.update multiple times to update the data (ciphertext).

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

If AES (GCM mode) decryption fails and error code 17630001 is returned, see Failed to Call doFinal During Decryption Using AES-GCM.

Using an AES Symmetric Key (XTS Mode) for Encryption and Decryption

Starting from API version 26.0.0, AES symmetric key encryption and decryption supports the XTS block mode.

Key Generation

Call cryptoFramework.createSymKeyGenerator and SymKeyGenerator.generateSymKey to generate a symmetric key (SymKey) with the AES algorithm and a key length of 256 bits.

For how to generate an AES symmetric key, you can refer to the example below, and combine the symmetric key generation and conversion specifications: AES and randomly generate symmetric key for understanding. The reference document and the current example may have differences in input parameters, please read and note the difference.

When encrypting and decrypting with an AES symmetric key (XTS mode), the symmetric key length must be twice that of a standard AES key. Only AES-128 and AES-256 cipher instances can be created for encryption and decryption.

In the example code, an AES256 symmetric key is created, and an AES128 cipher instance is used for encryption and decryption.

Encryption

  1. Call cryptoFramework.createCipher and specify the string parameter 'AES128|XTS' to create a Cipher instance with the symmetric key type AES128 and the block mode XTS, 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 corresponding to the XTS mode (IvParamsSpec), and initialize the encryption Cipher instance.

  3. When the content to be encrypted is short, you can directly call Cipher.doFinal without calling update to get the data after encryption.

Decryption

  1. Call cryptoFramework.createCipher with the string parameter 'AES128|XTS' to create a Cipher instance with the symmetric key type AES128 and the block mode XTS, which is used to complete the decryption operation.

  2. Call Cipher.init to set the mode to decryption (cryptoFramework.CryptoMode.DECRYPT_MODE), specify the decryption key (SymKey) and the decryption parameters (IvParamsSpec) corresponding to the XTS mode, and initialize the decryption Cipher instance.

  3. When the content to decrypt is short, you can skip calling update and directly call Cipher.doFinal to obtain the decrypted data.