1e41f4b71Sopenharmony_ci# Encryption and Decryption with an AES Symmetric Key (GCM Mode) (ArkTS) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [AES](crypto-sym-encrypt-decrypt-spec.md#aes). 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci**Encryption** 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci1. 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 AES symmetric key (**SymKey**). 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci In addition to the example in this topic, [AES](crypto-sym-key-generation-conversion-spec.md#aes) and [Randomly Generating a Symmetric Key](crypto-generate-sym-key-randomly.md) may help you better understand how to generate an AES symmetric key. Note that the input parameters in the reference documents may be different from those in the example below. 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci2. Use [cryptoFramework.createCipher](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatecipher) with the string parameter **'AES128|GCM|PKCS7'** to create a **Cipher** instance. The key type is **AES128**, block cipher mode is **GCM**, and the padding mode is **PKCS7**. 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci3. Use [Cipher.init](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#init-1) to initialize the **Cipher** instance. In the **Cipher.init** API, set **opMode** to **CryptoMode.ENCRYPT_MODE** (encryption), **key** to **SymKey** (the key for encryption), and **params** to **GcmParamsSpec** corresponding to the GCM mode. 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci4. Use [Cipher.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-1) to pass in the data to be encrypted (plaintext). 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci Currently, the amount of data to be passed in by a single **Cipher.update** is not limited. You can determine how to pass in data based on the data volume. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci - If a small amount of data is to be encrypted, you can use **Cipher.doFinal** immediately after **Cipher.init**. 23e41f4b71Sopenharmony_ci - If a large amount of data is to be encrypted, you can call **Cipher.update** multiple times to pass in the data by segment. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci5. Use [Cipher.doFinal](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#dofinal-1) to obtain the encrypted data. 26e41f4b71Sopenharmony_ci - If data has been passed in by **Cipher.update**, pass in **null** in the **data** parameter of **Cipher.doFinal**. 27e41f4b71Sopenharmony_ci - The output of **Cipher.doFinal** may be **null**. To avoid exceptions, always check whether the result is **null** before accessing specific data. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci6. Obtain [GcmParamsSpec.authTag](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#gcmparamsspec) as the authentication information for decryption. 30e41f4b71Sopenharmony_ci In GCM mode, extract the last 16 bytes from the encrypted data as the authentication information for initializing the **Cipher** instance in decryption. In the example, **authTag** is of 16 bytes. 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci**Decryption** 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci1. Use [Cipher.init](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#init-1) to initialize the **Cipher** instance. In the **Cipher.init** API, set **opMode** to **CryptoMode.DECRYPT_MODE** (decryption), **key** to **SymKey** (the key for decryption), and **params** to **GcmParamsSpec** corresponding to the GCM mode. 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci2. Use [Cipher.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-1) to pass in the data to be decrypted (ciphertext). 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci3. Use [Cipher.doFinal](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#dofinal-1) to obtain the decrypted data. 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci- Example (using asynchronous APIs): 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci ```ts 46e41f4b71Sopenharmony_ci import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 47e41f4b71Sopenharmony_ci import { buffer } from '@kit.ArkTS'; 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci function genGcmParamsSpec() { 50e41f4b71Sopenharmony_ci let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 12 bytes 51e41f4b71Sopenharmony_ci let dataIv = new Uint8Array(arr); 52e41f4b71Sopenharmony_ci let ivBlob: cryptoFramework.DataBlob = { data: dataIv }; 53e41f4b71Sopenharmony_ci arr = [0, 0, 0, 0, 0, 0, 0, 0]; // 8 bytes 54e41f4b71Sopenharmony_ci let dataAad = new Uint8Array(arr); 55e41f4b71Sopenharmony_ci let aadBlob: cryptoFramework.DataBlob = { data: dataAad }; 56e41f4b71Sopenharmony_ci arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 16 bytes 57e41f4b71Sopenharmony_ci let dataTag = new Uint8Array(arr); 58e41f4b71Sopenharmony_ci let tagBlob: cryptoFramework.DataBlob = { 59e41f4b71Sopenharmony_ci data: dataTag 60e41f4b71Sopenharmony_ci }; 61e41f4b71Sopenharmony_ci // Obtain the GCM authTag from the Cipher.doFinal result in encryption and fill it in the params parameter of Cipher.init in decryption. 62e41f4b71Sopenharmony_ci let gcmParamsSpec: cryptoFramework.GcmParamsSpec = { 63e41f4b71Sopenharmony_ci iv: ivBlob, 64e41f4b71Sopenharmony_ci aad: aadBlob, 65e41f4b71Sopenharmony_ci authTag: tagBlob, 66e41f4b71Sopenharmony_ci algName: "GcmParamsSpec" 67e41f4b71Sopenharmony_ci }; 68e41f4b71Sopenharmony_ci return gcmParamsSpec; 69e41f4b71Sopenharmony_ci } 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci let gcmParams = genGcmParamsSpec(); 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci // Encrypt the message. 74e41f4b71Sopenharmony_ci async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { 75e41f4b71Sopenharmony_ci let cipher = cryptoFramework.createCipher('AES128|GCM|PKCS7'); 76e41f4b71Sopenharmony_ci await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, gcmParams); 77e41f4b71Sopenharmony_ci let encryptUpdate = await cipher.update(plainText); 78e41f4b71Sopenharmony_ci // In GCM mode, pass in null in Cipher.doFinal in encryption. Obtain the tag data and fill it in the gcmParams object. 79e41f4b71Sopenharmony_ci gcmParams.authTag = await cipher.doFinal(null); 80e41f4b71Sopenharmony_ci return encryptUpdate; 81e41f4b71Sopenharmony_ci } 82e41f4b71Sopenharmony_ci // Decrypt the message. 83e41f4b71Sopenharmony_ci async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { 84e41f4b71Sopenharmony_ci let decoder = cryptoFramework.createCipher('AES128|GCM|PKCS7'); 85e41f4b71Sopenharmony_ci await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, gcmParams); 86e41f4b71Sopenharmony_ci let decryptUpdate = await decoder.update(cipherText); 87e41f4b71Sopenharmony_ci // In GCM mode, pass in null in Cipher.doFinal in decryption. Verify the tag data passed in Cipher.init. If the verification fails, an exception will be thrown. 88e41f4b71Sopenharmony_ci let decryptData = await decoder.doFinal(null); 89e41f4b71Sopenharmony_ci if (decryptData == null) { 90e41f4b71Sopenharmony_ci console.info('GCM decrypt success, decryptData is null'); 91e41f4b71Sopenharmony_ci } 92e41f4b71Sopenharmony_ci return decryptUpdate; 93e41f4b71Sopenharmony_ci } 94e41f4b71Sopenharmony_ci async function genSymKeyByData(symKeyData: Uint8Array) { 95e41f4b71Sopenharmony_ci let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; 96e41f4b71Sopenharmony_ci let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128'); 97e41f4b71Sopenharmony_ci let symKey = await aesGenerator.convertKey(symKeyBlob); 98e41f4b71Sopenharmony_ci console.info('convertKey success'); 99e41f4b71Sopenharmony_ci return symKey; 100e41f4b71Sopenharmony_ci } 101e41f4b71Sopenharmony_ci async function main() { 102e41f4b71Sopenharmony_ci let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); 103e41f4b71Sopenharmony_ci let symKey = await genSymKeyByData(keyData); 104e41f4b71Sopenharmony_ci let message = "This is a test"; 105e41f4b71Sopenharmony_ci let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; 106e41f4b71Sopenharmony_ci let encryptText = await encryptMessagePromise(symKey, plainText); 107e41f4b71Sopenharmony_ci let decryptText = await decryptMessagePromise(symKey, encryptText); 108e41f4b71Sopenharmony_ci if (plainText.data.toString() === decryptText.data.toString()) { 109e41f4b71Sopenharmony_ci console.info('decrypt ok'); 110e41f4b71Sopenharmony_ci console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8')); 111e41f4b71Sopenharmony_ci } else { 112e41f4b71Sopenharmony_ci console.error('decrypt failed'); 113e41f4b71Sopenharmony_ci } 114e41f4b71Sopenharmony_ci } 115e41f4b71Sopenharmony_ci ``` 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci- Example (using synchronous APIs): 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci ```ts 120e41f4b71Sopenharmony_ci import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 121e41f4b71Sopenharmony_ci import { buffer } from '@kit.ArkTS'; 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci function genGcmParamsSpec() { 125e41f4b71Sopenharmony_ci let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 12 bytes 126e41f4b71Sopenharmony_ci let dataIv = new Uint8Array(arr); 127e41f4b71Sopenharmony_ci let ivBlob: cryptoFramework.DataBlob = { data: dataIv }; 128e41f4b71Sopenharmony_ci arr = [0, 0, 0, 0, 0, 0, 0, 0]; // 8 bytes 129e41f4b71Sopenharmony_ci let dataAad = new Uint8Array(arr); 130e41f4b71Sopenharmony_ci let aadBlob: cryptoFramework.DataBlob = { data: dataAad }; 131e41f4b71Sopenharmony_ci arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 16 bytes 132e41f4b71Sopenharmony_ci let dataTag = new Uint8Array(arr); 133e41f4b71Sopenharmony_ci let tagBlob: cryptoFramework.DataBlob = { 134e41f4b71Sopenharmony_ci data: dataTag 135e41f4b71Sopenharmony_ci }; 136e41f4b71Sopenharmony_ci // Obtain the GCM authTag from the Cipher.doFinal result in encryption and fill it in the params parameter of Cipher.init in decryption. 137e41f4b71Sopenharmony_ci let gcmParamsSpec: cryptoFramework.GcmParamsSpec = { 138e41f4b71Sopenharmony_ci iv: ivBlob, 139e41f4b71Sopenharmony_ci aad: aadBlob, 140e41f4b71Sopenharmony_ci authTag: tagBlob, 141e41f4b71Sopenharmony_ci algName: "GcmParamsSpec" 142e41f4b71Sopenharmony_ci }; 143e41f4b71Sopenharmony_ci return gcmParamsSpec; 144e41f4b71Sopenharmony_ci } 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci let gcmParams = genGcmParamsSpec(); 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci // Encrypt the message. 149e41f4b71Sopenharmony_ci function encryptMessage(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { 150e41f4b71Sopenharmony_ci let cipher = cryptoFramework.createCipher('AES128|GCM|PKCS7'); 151e41f4b71Sopenharmony_ci cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, gcmParams); 152e41f4b71Sopenharmony_ci let encryptUpdate = cipher.updateSync(plainText); 153e41f4b71Sopenharmony_ci // In GCM mode, pass in null in Cipher.doFinal in encryption. Obtain the tag data and fill it in the gcmParams object. 154e41f4b71Sopenharmony_ci gcmParams.authTag = cipher.doFinalSync(null); 155e41f4b71Sopenharmony_ci return encryptUpdate; 156e41f4b71Sopenharmony_ci } 157e41f4b71Sopenharmony_ci // Decrypt the message. 158e41f4b71Sopenharmony_ci function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { 159e41f4b71Sopenharmony_ci let decoder = cryptoFramework.createCipher('AES128|GCM|PKCS7'); 160e41f4b71Sopenharmony_ci decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, gcmParams); 161e41f4b71Sopenharmony_ci let decryptUpdate = decoder.updateSync(cipherText); 162e41f4b71Sopenharmony_ci // In GCM mode, pass in null in Cipher.doFinal in decryption. Verify the tag data passed in Cipher.init. If the verification fails, an exception will be thrown. 163e41f4b71Sopenharmony_ci let decryptData = decoder.doFinalSync(null); 164e41f4b71Sopenharmony_ci if (decryptData == null) { 165e41f4b71Sopenharmony_ci console.info('GCM decrypt success, decryptData is null'); 166e41f4b71Sopenharmony_ci } 167e41f4b71Sopenharmony_ci return decryptUpdate; 168e41f4b71Sopenharmony_ci } 169e41f4b71Sopenharmony_ci async function genSymKeyByData(symKeyData: Uint8Array) { 170e41f4b71Sopenharmony_ci let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; 171e41f4b71Sopenharmony_ci let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128'); 172e41f4b71Sopenharmony_ci let symKey = await aesGenerator.convertKey(symKeyBlob); 173e41f4b71Sopenharmony_ci console.info('convertKey success'); 174e41f4b71Sopenharmony_ci return symKey; 175e41f4b71Sopenharmony_ci } 176e41f4b71Sopenharmony_ci async function main() { 177e41f4b71Sopenharmony_ci let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); 178e41f4b71Sopenharmony_ci let symKey = await genSymKeyByData(keyData); 179e41f4b71Sopenharmony_ci let message = "This is a test"; 180e41f4b71Sopenharmony_ci let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; 181e41f4b71Sopenharmony_ci let encryptText = encryptMessage(symKey, plainText); 182e41f4b71Sopenharmony_ci let decryptText = decryptMessage(symKey, encryptText); 183e41f4b71Sopenharmony_ci if (plainText.data.toString() === decryptText.data.toString()) { 184e41f4b71Sopenharmony_ci console.info('decrypt ok'); 185e41f4b71Sopenharmony_ci console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8')); 186e41f4b71Sopenharmony_ci } else { 187e41f4b71Sopenharmony_ci console.error('decrypt failed'); 188e41f4b71Sopenharmony_ci } 189e41f4b71Sopenharmony_ci } 190e41f4b71Sopenharmony_ci ``` 191