目录

Encryption and Decryption with an SM4 Symmetric Key (ArkTS)

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

Encrypting and Decrypting with SM4 Symmetric Key (ECB Mode)

Encryption

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

    For details about how to generate an SM4 symmetric key, refer to the following example and also see Symmetric Key Generation and Conversion Specifications: SM4 and Randomly Generating 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 the string parameter 'SM4_128|ECB|PKCS7' to create a Cipher instance for encryption. The key type is SM4_128, block cipher mode is ECB, and the padding mode is PKCS7.

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

    If ECB mode is used, pass in null.

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

    • If a small amount of data is to be encrypted, you can use Cipher.doFinal immediately after Cipher.init.
    • If a large amount of data is to be encrypted, you can call Cipher.update multiple times to pass in the data by segment.
  5. Call Cipher.doFinal to obtain the encrypted data.

    • If data has been passed in by Cipher.update, pass in null in the data parameter of Cipher.doFinal.
    • The output of Cipher.doFinal may be null. To avoid exceptions, always check whether the result is null before accessing specific data.

Decryption

  1. Call cryptoFramework.createCipher with the string parameter 'SM4_128|ECB|PKCS7' to create a Cipher instance for decryption. The key type is SM4_128, block cipher mode is ECB, and the padding mode is PKCS7.

  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). The ECB mode has no encryption parameters, so pass in null.

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

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

If SM4 (ECB mode) decryption fails with error code 17630001, see Failed to Call doFinal During Decryption Using SM4-ECB/CBC.

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

Encryption

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

    For details about how to generate an SM4 symmetric key, refer to the following example and also see Symmetric Key Generation and Conversion Specifications: SM4 and Randomly Generating 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 the string parameter 'SM4_128|CBC|PKCS7' to create a Cipher instance with the symmetric key type of SM4_128, 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 (the encryption parameters for CBC mode).

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

    • If a small amount of data is to be encrypted, you can use Cipher.doFinal immediately after Cipher.init.
    • If a large amount of data is to be encrypted, you can call Cipher.update multiple times to pass in the data by segment.
  5. Call Cipher.doFinal to obtain the encrypted data.

    • If data has been passed in by Cipher.update, pass in null in the data parameter of Cipher.doFinal.
    • The output of Cipher.doFinal may be null. To avoid exceptions, always check whether the result is null before accessing specific data.

Decryption

  1. Call cryptoFramework.createCipher with the string parameter 'SM4_128|CBC|PKCS7' to create a Cipher instance with the symmetric key type of SM4_128, 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. Call Cipher.update to pass in the data to be decrypted (ciphertext).

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

If SM4 (CBC mode) decryption fails with error code 17630001, see Failed to Call doFinal During Decryption Using SM4-ECB or SM4-CBC.

Encrypting and Decrypting with an SM4 Symmetric Key (GCM Mode)

Encryption

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

    For details about how to generate an SM4 symmetric key, refer to the following example and also see Symmetric Key Generation and Conversion Specifications: SM4 and Randomly Generating 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 the string parameter 'SM4_128|GCM' to create a Cipher instance for encryption. The key type is SM4_128, and the block cipher mode is GCM.

  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 GcmParamsSpec corresponding to the GCM mode.

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

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

    • If a small amount of data is to be encrypted, you can use Cipher.doFinal immediately after Cipher.init.
    • When the amount of data is large, you can call update multiple times, that is, segmented encryption and decryption.
  5. Call Cipher.doFinal to obtain the encrypted data.

    • If data has been passed in by Cipher.update, pass in null in the data parameter of Cipher.doFinal.
    • The output of Cipher.doFinal may be null. To avoid exceptions, always check whether the result is null before accessing specific data.
  6. Obtain GcmParamsSpec.authTag as the authentication information for decryption.

    In GCM mode, take the last 16 bytes from the encrypted data as the authentication information used for initialization during decryption. In the example, authTag is exactly 16 bytes.

Decryption

  1. Call cryptoFramework.createCipher with the string parameter 'SM4_128|GCM' to create a Cipher instance for decryption. The key type is SM4_128, and the block cipher mode is GCM.

  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 GcmParamsSpec corresponding to the GCM mode.

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

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

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

Encrypting and Decrypting in Segments with an SM4 Symmetric Key (GCM Mode)

Encryption

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

    For details about how to generate an SM4 symmetric key, refer to the following example and also see Symmetric Key Generation and Conversion Specifications: SM4 and Randomly Generating 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 the string parameter 'SM4_128|GCM' to create a Cipher instance for encryption. The key type is SM4_128, and the block mode is GCM.

  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 GcmParamsSpec corresponding to the GCM mode.

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

    • Currently, there is no limit on the length of a single update. You can decide how to call update based on the amount of data.

    • It is recommended that you 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 in units of blocks, and the encrypted block result produced by this update is output. That is, when this 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 to fill a block before output. At the final doFinal, the unencrypted data is padded according to the specified padding mode, and then the remaining encryption result is output. The same applies to update during decryption.

      2. For stream cipher modes (such as CTR and OFB), the ciphertext length is usually equal to the plaintext length.

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

    • If data has been passed in by Cipher.update, pass in null in the data parameter of Cipher.doFinal.
    • The output of Cipher.doFinal may be null. To avoid exceptions, always check whether the result is null before accessing specific data.
  6. Obtain GcmParamsSpec.authTag as the authentication information for decryption.

    In GCM mode, you need to extract the last 16 bytes from the encrypted data as the authentication information for initialization during decryption. In the example, authTag is exactly 16 bytes.

Decryption

  1. Call cryptoFramework.createCipher with the string parameter 'SM4_128|GCM' to create a Cipher instance for decryption. The key type is SM4_128, and the block mode is GCM.

  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 GcmParamsSpec corresponding to the GCM mode.

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

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

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