目录

Encryption and Decryption with ECIES (ArkTS)

The ECIES algorithm is supported since API version 26.0.0. It is an encryption algorithm based on elliptic curve cryptography.

Constraints

Development Procedure

[h2]Encryption

  1. Call cryptoFramework.createAsyKeyGenerator and AsyKeyGenerator.generateKeyPair to generate a key pair (KeyPair) with the ECC algorithm.

  2. Call cryptoFramework.createKeyAgreement and KeyAgreement.generateSecret to perform key agreement based on the local private key (KeyPair.priKey) and peer public key (KeyPair.pubKey) and return the shared key.

  3. Call cryptoFramework.createKdf and Kdf.generateSecret to derive a key based on the shared key (Secret) using the X963KDF algorithm and return the derived key.

  4. Call cryptoFramework.createCipher with the string parameter 'AES128|GCM' to create a Cipher instance for encryption. The key type is AES-128, and the block cipher mode is GCM.

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

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

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

  8. Obtain GcmParamsSpec.authTag as the authentication information for decryption.

[h2]Decryption

  1. Call cryptoFramework.createAsyKeyGenerator and AsyKeyGenerator.generateKeyPair to generate a key pair (KeyPair) with the ECC algorithm.

  2. Call cryptoFramework.createKeyAgreement and KeyAgreement.generateSecret to perform key agreement based on the local private key (KeyPair.priKey) and peer public key (KeyPair.pubKey) and return the shared key.

  3. Call cryptoFramework.createKdf and Kdf.generateSecret to derive a key based on the shared key (Secret) using the X963KDF algorithm and return the derived key.

  4. Call cryptoFramework.createCipher with the string parameter 'AES128|GCM' to create a Cipher instance for decryption. The key type is AES-128, and the block cipher mode is GCM.

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

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

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

[h2]Example Code