1e41f4b71Sopenharmony_ci# Encryption and Decryption with an AES Symmetric Key (CCM 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|CCM'** to create a **Cipher** instance. The key type is AES128, and the block cipher mode is CCM. 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 **CcmParamsSpec** corresponding to the CCM 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 The CCM mode does not support segment-based encryption and decryption. 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci5. Use [Cipher.doFinal](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#dofinal-1) to obtain the encrypted data. 25e41f4b71Sopenharmony_ci - If data has been passed in by **Cipher.update**, pass in **null** in the **data** parameter of **Cipher.doFinal**. 26e41f4b71Sopenharmony_ci - The output of **Cipher.doFinal** may be **null**. To avoid exceptions, always check whether the result is **null** before accessing specific data. 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci6. Obtain [CcmParamsSpec.authTag](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#ccmparamsspec) as the authentication information for decryption. 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci In CCM mode, extract the last 12 bytes from the encrypted data as the authentication information for initializing the **Cipher** instance in decryption. In the example, **authTag** is of 12 bytes. 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci**Decryption** 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_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 **CcmParamsSpec** corresponding to the CCM mode. 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci2. Use [Cipher.doFinal](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#dofinal-1) to obtain the decrypted data. 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci- Example (using asynchronous APIs): 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci ```ts 43e41f4b71Sopenharmony_ci import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 44e41f4b71Sopenharmony_ci import { buffer } from '@kit.ArkTS'; 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci function genCcmParamsSpec() { 47e41f4b71Sopenharmony_ci let rand: cryptoFramework.Random = cryptoFramework.createRandom(); 48e41f4b71Sopenharmony_ci let ivBlob: cryptoFramework.DataBlob = rand.generateRandomSync(7); 49e41f4b71Sopenharmony_ci let aadBlob: cryptoFramework.DataBlob = rand.generateRandomSync(8); 50e41f4b71Sopenharmony_ci let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 12 bytes 51e41f4b71Sopenharmony_ci let dataTag = new Uint8Array(arr); 52e41f4b71Sopenharmony_ci let tagBlob: cryptoFramework.DataBlob = { 53e41f4b71Sopenharmony_ci data: dataTag 54e41f4b71Sopenharmony_ci }; 55e41f4b71Sopenharmony_ci // Obtain the CCM authTag from the Cipher.doFinal result in encryption and fill it in the params parameter of Cipher.init in decryption. 56e41f4b71Sopenharmony_ci let ccmParamsSpec: cryptoFramework.CcmParamsSpec = { 57e41f4b71Sopenharmony_ci iv: ivBlob, 58e41f4b71Sopenharmony_ci aad: aadBlob, 59e41f4b71Sopenharmony_ci authTag: tagBlob, 60e41f4b71Sopenharmony_ci algName: "CcmParamsSpec" 61e41f4b71Sopenharmony_ci }; 62e41f4b71Sopenharmony_ci return ccmParamsSpec; 63e41f4b71Sopenharmony_ci } 64e41f4b71Sopenharmony_ci let ccmParams = genCcmParamsSpec(); 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci // Encrypt the message. 67e41f4b71Sopenharmony_ci async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { 68e41f4b71Sopenharmony_ci let cipher = cryptoFramework.createCipher('AES128|CCM'); 69e41f4b71Sopenharmony_ci await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, ccmParams); 70e41f4b71Sopenharmony_ci let encryptUpdate = await cipher.update(plainText); 71e41f4b71Sopenharmony_ci // In CCM mode, pass in null in Cipher.doFinal in encryption. Obtain the tag data and fill it in the ccmParams object. 72e41f4b71Sopenharmony_ci ccmParams.authTag = await cipher.doFinal(null); 73e41f4b71Sopenharmony_ci return encryptUpdate; 74e41f4b71Sopenharmony_ci } 75e41f4b71Sopenharmony_ci // Decrypt the message. 76e41f4b71Sopenharmony_ci async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { 77e41f4b71Sopenharmony_ci let decoder = cryptoFramework.createCipher('AES128|CCM'); 78e41f4b71Sopenharmony_ci await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, ccmParams); 79e41f4b71Sopenharmony_ci let decryptUpdate = await decoder.doFinal(cipherText); 80e41f4b71Sopenharmony_ci return decryptUpdate; 81e41f4b71Sopenharmony_ci } 82e41f4b71Sopenharmony_ci async function genSymKeyByData(symKeyData: Uint8Array) { 83e41f4b71Sopenharmony_ci let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; 84e41f4b71Sopenharmony_ci let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128'); 85e41f4b71Sopenharmony_ci let symKey = await aesGenerator.convertKey(symKeyBlob); 86e41f4b71Sopenharmony_ci console.info('convertKey success'); 87e41f4b71Sopenharmony_ci return symKey; 88e41f4b71Sopenharmony_ci } 89e41f4b71Sopenharmony_ci async function main() { 90e41f4b71Sopenharmony_ci let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); 91e41f4b71Sopenharmony_ci let symKey = await genSymKeyByData(keyData); 92e41f4b71Sopenharmony_ci let message = "This is a test"; 93e41f4b71Sopenharmony_ci let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; 94e41f4b71Sopenharmony_ci let encryptText = await encryptMessagePromise(symKey, plainText); 95e41f4b71Sopenharmony_ci let decryptText = await decryptMessagePromise(symKey, encryptText); 96e41f4b71Sopenharmony_ci if (plainText.data.toString() === decryptText.data.toString()) { 97e41f4b71Sopenharmony_ci console.info('decrypt ok'); 98e41f4b71Sopenharmony_ci console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8')); 99e41f4b71Sopenharmony_ci } else { 100e41f4b71Sopenharmony_ci console.error('decrypt failed'); 101e41f4b71Sopenharmony_ci } 102e41f4b71Sopenharmony_ci } 103e41f4b71Sopenharmony_ci ``` 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci- Example (using synchronous APIs): 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci ```ts 108e41f4b71Sopenharmony_ci import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 109e41f4b71Sopenharmony_ci import { buffer } from '@kit.ArkTS'; 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci function genCcmParamsSpec() { 113e41f4b71Sopenharmony_ci let rand: cryptoFramework.Random = cryptoFramework.createRandom(); 114e41f4b71Sopenharmony_ci let ivBlob: cryptoFramework.DataBlob = rand.generateRandomSync(7); 115e41f4b71Sopenharmony_ci let aadBlob: cryptoFramework.DataBlob = rand.generateRandomSync(8); 116e41f4b71Sopenharmony_ci let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 12 bytes 117e41f4b71Sopenharmony_ci let dataTag = new Uint8Array(arr); 118e41f4b71Sopenharmony_ci let tagBlob: cryptoFramework.DataBlob = { 119e41f4b71Sopenharmony_ci data: dataTag 120e41f4b71Sopenharmony_ci }; 121e41f4b71Sopenharmony_ci // Obtain the CCM authTag from the Cipher.doFinal result in encryption and fill it in the params parameter of Cipher.init in decryption. 122e41f4b71Sopenharmony_ci let ccmParamsSpec: cryptoFramework.CcmParamsSpec = { 123e41f4b71Sopenharmony_ci iv: ivBlob, 124e41f4b71Sopenharmony_ci aad: aadBlob, 125e41f4b71Sopenharmony_ci authTag: tagBlob, 126e41f4b71Sopenharmony_ci algName: "CcmParamsSpec" 127e41f4b71Sopenharmony_ci }; 128e41f4b71Sopenharmony_ci return ccmParamsSpec; 129e41f4b71Sopenharmony_ci } 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci let ccmParams = genCcmParamsSpec(); 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci // Encrypt the message. 134e41f4b71Sopenharmony_ci function encryptMessage(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { 135e41f4b71Sopenharmony_ci let cipher = cryptoFramework.createCipher('AES128|CCM'); 136e41f4b71Sopenharmony_ci cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, ccmParams); 137e41f4b71Sopenharmony_ci let encryptUpdate = cipher.updateSync(plainText); 138e41f4b71Sopenharmony_ci // In CCM mode, pass in null in Cipher.doFinal in encryption. Obtain the tag data and fill it in the ccmParams object. 139e41f4b71Sopenharmony_ci ccmParams.authTag = cipher.doFinalSync(null); 140e41f4b71Sopenharmony_ci return encryptUpdate; 141e41f4b71Sopenharmony_ci } 142e41f4b71Sopenharmony_ci // Decrypt the message. 143e41f4b71Sopenharmony_ci function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { 144e41f4b71Sopenharmony_ci let decoder = cryptoFramework.createCipher('AES128|CCM'); 145e41f4b71Sopenharmony_ci decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, ccmParams); 146e41f4b71Sopenharmony_ci let decryptUpdate = decoder.doFinalSync(cipherText); 147e41f4b71Sopenharmony_ci return decryptUpdate; 148e41f4b71Sopenharmony_ci } 149e41f4b71Sopenharmony_ci async function genSymKeyByData(symKeyData: Uint8Array) { 150e41f4b71Sopenharmony_ci let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; 151e41f4b71Sopenharmony_ci let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128'); 152e41f4b71Sopenharmony_ci let symKey = await aesGenerator.convertKey(symKeyBlob); 153e41f4b71Sopenharmony_ci console.info('convertKey success'); 154e41f4b71Sopenharmony_ci return symKey; 155e41f4b71Sopenharmony_ci } 156e41f4b71Sopenharmony_ci async function main() { 157e41f4b71Sopenharmony_ci let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); 158e41f4b71Sopenharmony_ci let symKey = await genSymKeyByData(keyData); 159e41f4b71Sopenharmony_ci let message = "This is a test"; 160e41f4b71Sopenharmony_ci let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; 161e41f4b71Sopenharmony_ci let encryptText = encryptMessage(symKey, plainText); 162e41f4b71Sopenharmony_ci let decryptText = decryptMessage(symKey, encryptText); 163e41f4b71Sopenharmony_ci if (plainText.data.toString() === decryptText.data.toString()) { 164e41f4b71Sopenharmony_ci console.info('decrypt ok'); 165e41f4b71Sopenharmony_ci console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8')); 166e41f4b71Sopenharmony_ci } else { 167e41f4b71Sopenharmony_ci console.error('decrypt failed'); 168e41f4b71Sopenharmony_ci } 169e41f4b71Sopenharmony_ci } 170e41f4b71Sopenharmony_ci ``` 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci 175