• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Encryption and Decryption with a 3DES Symmetric Key (ECB Mode)
2
3
4For details about the algorithm specifications, see [3DES](crypto-sym-encrypt-decrypt-spec.md#3des).
5
6
7**Encryption**
8
9
101. Use [cryptoFramework.createSymKeyGenerator](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatesymkeygenerator) and [SymKeyGenerator.convertKey](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkey-1) to generate a 192-bit 3DES symmetric key (**SymKey**).
11
12   In addition to the example in this topic, [3DES](crypto-sym-key-generation-conversion-spec.md#3des) and [Converting Binary Data into a Symmetric Key](crypto-convert-binary-data-to-sym-key.md) may help you better understand how to generate a 3DES symmetric key pair. Note that the input parameters in the reference documents may be different from those in the example below.
13
142. Use [cryptoFramework.createCipher](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatecipher) with the string parameter **'3DES192|ECB|PKCS7'** to create a **Cipher** instance. The key type is **3DES192**, block cipher mode is **ECB**, and the padding mode is **PKCS7**.
15
163. Use [Cipher.init](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#init-1) to initialize the **Cipher** instance. In **Cipher.init**, set **opMode** to **CryptoMode.ENCRYPT_MODE** (encryption) and **key** to **SymKey** (the key used for encryption).
17
18   When ECB mode is used, pass in **null** in **params**.
19
204. Use [Cipher.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-1) to pass in the data to be encrypted (plaintext).
21
22   - If the data to be encrypted is short, you can use **doFinal** immediately after **init**.
23   - If the data to be encrypted is considerably long, you can call **update()** multiple times to [pass in the data by segment](crypto-aes-sym-encrypt-decrypt-gcm-by-segment.md).
24
255. Use [Cipher.doFinal](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#dofinal-1) to obtain the encrypted data.
26
27   - If data has been passed in by **update()**, pass in **null** in the **data** parameter of **Cipher.doFinal**.
28   - The output of **doFinal** may be **null**. To avoid exceptions, always check whether the result is **null** before accessing specific data.
29
30
31**Decryption**
32
33
341. Use [Cipher.init](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#init-1) to initialize the **Cipher** instance. In **Cipher.init**, set **opMode** to **CryptoMode.DECRYPT_MODE** (decryption) and **key** to **SymKey** (the key used for decryption). When ECB mode is used, pass in **null** in **params**.
35
362. Use [Cipher.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-1) to pass in the data to be decrypted (ciphertext).
37
383. Use [Cipher.doFinal](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#dofinal-1) to obtain the decrypted data.
39
40- Example (using asynchronous APIs):
41
42  ```ts
43  import cryptoFramework from '@ohos.security.cryptoFramework';
44  import buffer from '@ohos.buffer';
45
46  // Encrypt the message.
47  async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) {
48    let cipher = cryptoFramework.createCipher('3DES192|ECB|PKCS7');
49    await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, null);
50    let encryptData = await cipher.doFinal(plainText);
51    return encryptData;
52  }
53  // Decrypt the message.
54  async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) {
55    let decoder = cryptoFramework.createCipher('3DES192|ECB|PKCS7');
56    await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, null);
57    let decryptData = await decoder.doFinal(cipherText);
58    return decryptData;
59  }
60  async function genSymKeyByData(symKeyData: Uint8Array) {
61    let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
62    let symGenerator = cryptoFramework.createSymKeyGenerator('3DES192');
63    let symKey = await symGenerator.convertKey(symKeyBlob);
64    console.info('convertKey success');
65    return symKey;
66  }
67  async function main() {
68    let keyData = new Uint8Array([238, 249, 61, 55, 128, 220, 183, 224, 139, 253, 248, 239, 239, 41, 71, 25, 235, 206, 230, 162, 249, 27, 234, 114]);
69    let symKey = await genSymKeyByData(keyData);
70    let message = "This is a test";
71    let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) };
72    let encryptText = await encryptMessagePromise(symKey, plainText);
73    let decryptText = await decryptMessagePromise(symKey, encryptText);
74    if (plainText.data.toString() === decryptText.data.toString()) {
75      console.info('decrypt ok');
76      console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8'));
77    } else {
78      console.error('decrypt failed');
79    }
80  }
81  ```
82