目录

Encryption and Decryption with a DES Symmetric Key (ArkTS)

For details about the algorithm specifications, see DES.

Encrypting and Decrypting with DES Symmetric Key (ECB Mode)

Encryption

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

    To generate a DES symmetric key, refer to the following example, together with DES and Converting Binary Data into a Symmetric Key. Note that 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 'DES64|ECB|PKCS7' to create a Cipher instance for encryption. The key type is DES64, 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.
    • You can determine the method to use based on the data size. For example, if the data exceeds 20 bytes, use update.
  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 'DES64|ECB|PKCS7' to create a Cipher instance for decryption. The key type is DES64, 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). If ECB mode is used, 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 DES decryption fails with error code 17630001, see DES/3DES Decryption doFinal Failure.