1# Encryption and Decryption with an SM4 Symmetric Key (ECB Mode) (ArkTS) 2 3 4For details about the algorithm specifications, see [SM4](crypto-sym-encrypt-decrypt-spec.md#sm4). 5 6**Encryption** 7 8 91. Use [cryptoFramework.createSymKeyGenerator](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatesymkeygenerator) and [SymKeyGenerator.generateSymKey](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatesymkey-1) to generate a 128-bit SM4 symmetric key (**SymKey**). 10 11 In addition to the example in this topic, [SM4](crypto-sym-key-generation-conversion-spec.md#sm4) and [Randomly Generating a Symmetric Key](crypto-generate-sym-key-randomly.md) may help you better understand how to generate an SM4 symmetric key. Note that the input parameters in the reference documents may be different from those in the example below. 12 132. Use [cryptoFramework.createCipher](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatecipher) with the string parameter **'SM4_128|ECB|PKCS7'** to create a **Cipher** instance. The key type is **SM4_128**, block cipher mode is **ECB**, and the padding mode is **PKCS7**. 14 153. 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). 16 17 When ECB mode is used, pass in **null** in **params**. 18 194. Use [Cipher.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-1) to pass in the data to be encrypted (plaintext). 20 21 - If a small amount of data is to be encrypted, you can use **Cipher.doFinal** immediately after **Cipher.init**. 22 - If a large amount of data is to be encrypted, you can call **Cipher.update** multiple times to pass in the data by segment. 23 245. Use [Cipher.doFinal](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#dofinal-1) to obtain the encrypted data. 25 26 - If data has been passed in by **Cipher.update**, pass in **null** in the **data** parameter of **Cipher.doFinal**. 27 - The output of **Cipher.doFinal** may be **null**. To avoid exceptions, always check whether the result is **null** before accessing specific data. 28 29 30**Decryption** 31 32 331. 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**. 34 352. Use [Cipher.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-1) to pass in the data to be decrypted (ciphertext). 36 373. Use [Cipher.doFinal](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#dofinal-1) to obtain the decrypted data. 38 39 40- Example (using asynchronous APIs): 41 42 ```ts 43 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 44 import { buffer } from '@kit.ArkTS'; 45 46 // Encrypt the message. 47 async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { 48 let cipher = cryptoFramework.createCipher('SM4_128|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('SM4_128|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('SM4_128'); 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([7, 154, 52, 176, 4, 236, 150, 43, 237, 9, 145, 166, 141, 174, 224, 131]); 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 83- Example (using synchronous APIs): 84 85 ```ts 86 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 87 import { buffer } from '@kit.ArkTS'; 88 89 // Encrypt the message. 90 function encryptMessage(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { 91 let cipher = cryptoFramework.createCipher('SM4_128|ECB|PKCS7'); 92 cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, null); 93 let encryptData = cipher.doFinalSync(plainText); 94 return encryptData; 95 } 96 // Decrypt the message. 97 function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { 98 let decoder = cryptoFramework.createCipher('SM4_128|ECB|PKCS7'); 99 decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, null); 100 let decryptData = decoder.doFinalSync(cipherText); 101 return decryptData; 102 } 103 function genSymKeyByData(symKeyData: Uint8Array) { 104 let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; 105 let symGenerator = cryptoFramework.createSymKeyGenerator('SM4_128'); 106 let symKey = symGenerator.convertKeySync(symKeyBlob); 107 console.info('convertKeySync success'); 108 return symKey; 109 } 110 function main() { 111 let keyData = new Uint8Array([7, 154, 52, 176, 4, 236, 150, 43, 237, 9, 145, 166, 141, 174, 224, 131]); 112 let symKey = genSymKeyByData(keyData); 113 let message = "This is a test"; 114 let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; 115 let encryptText = encryptMessage(symKey, plainText); 116 let decryptText = decryptMessage(symKey, encryptText); 117 if (plainText.data.toString() === decryptText.data.toString()) { 118 console.info('decrypt ok'); 119 console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8')); 120 } else { 121 console.error('decrypt failed'); 122 } 123 } 124 ``` 125