使用AES对称密钥（CCM模式）参数为AeadParamsSpec加解密(ArkTS)
===========================================

从API版本26.0.0开始，[AES](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/crypto-sym-encrypt-decrypt-spec#aes)对称密钥（CCM模式）的加解密支持使用AeadParamsSpec参数。

当用户不希望显式保存authTag或用户需要指定authTag的长度时，使用AeadParamsSpec参数。

**密钥生成**

调用[cryptoFramework.createSymKeyGenerator](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework#cryptoframeworkcreatesymkeygenerator)、[SymKeyGenerator.generateSymKey](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework#generatesymkey-1)，生成密钥算法为AES、密钥长度为128位的对称密钥（SymKey）。

如何生成AES对称密钥，开发者可参考下文示例，并结合对称密钥生成和转换规格：[AES](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/crypto-sym-key-generation-conversion-spec#aes)和[随机生成对称密钥](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/crypto-generate-sym-key-randomly)进行理解，参考文档与当前示例可能存在入参差异，请注意区分。

**加密**

1.  调用[cryptoFramework.createCipher](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework#cryptoframeworkcreatecipher)，指定字符串参数'AES128|CCM'，创建对称密钥为AES128、分组模式为CCM的Cipher实例，用于执行加密操作。
    
2.  调用[Cipher.init](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework#init-1)，设置模式为加密（cryptoFramework.CryptoMode.ENCRYPT\_MODE），指定密钥（SymKey）和CCM模式对应的加密参数（AeadParamsSpec），初始化加密Cipher实例。
    
3.  调用[Cipher.doFinal](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework#dofinal-1)获取加密后的数据。
    
    ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/ab/v3/0CCDrgHGRN6wk5HXgchgvg/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260709T132402Z&HW-CC-Expire=86400&HW-CC-Sign=427B81070699B8024F4857896239F3ECBCCA52BE57B97E1DACC87979D255F062)
    
    当前使用AeadParamsSpec参数，CCM模式下update与doFinal只能调用其中一个进行加密。且每个方法只能调用一次。
    
    使用AeadParamsSpec初始化Cipher实例，身份认证标签（authTag）自动拼接在密文后，不需要单独保存。
    
    在使用AeadParamsSpec结构的CCM模式下，tagLen支持4、6、8、10、12、14和16，如果不传默认为12，单位均为字节。
    

**解密**

1.  调用[cryptoFramework.createCipher](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework#cryptoframeworkcreatecipher)，指定字符串参数'AES128|CCM'，创建对称密钥为AES128、分组模式为CCM的Cipher实例，用于完成解密操作。
    
2.  调用[Cipher.init](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework#init-1)，设置模式为解密（cryptoFramework.CryptoMode.DECRYPT\_MODE），指定密钥（SymKey）和CCM模式对应的解密参数（AeadParamsSpec），初始化解密Cipher实例。
    
3.  调用[Cipher.doFinal](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework#dofinal-1)，获取解密后的数据。
    

#### 示例

*   异步方法示例：
    
    import { cryptoFramework } from '@kit.CryptoArchitectureKit';
    import { buffer } from '@kit.ArkTS';
    
    function genCcmAeadParamsSpec() {
      let nonce = \[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12\]; // 12 bytes
      let nonceValue = new Uint8Array(nonce);
      let aad = \[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12\]; // 12 bytes
      let aadValue = new Uint8Array(aad);
      let aeadParamsSpec: cryptoFramework.AeadParamsSpec = {
          nonce: nonceValue,
          authenticatedData: aadValue,
          algName: 'AeadParamsSpec'
      };
      return aeadParamsSpec;
    }
    
      let aeadParams = genCcmAeadParamsSpec();
    
    // 加密消息
    async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) {
      let cipher = cryptoFramework.createCipher('AES128|CCM');
      await cipher.init(cryptoFramework.CryptoMode.ENCRYPT\_MODE, symKey, aeadParams);
      let encryptUpdate = await cipher.doFinal(plainText);
      return encryptUpdate;
    }
    
    // 解密消息
    async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) {
      let decoder = cryptoFramework.createCipher('AES128|CCM');
      await decoder.init(cryptoFramework.CryptoMode.DECRYPT\_MODE, symKey, aeadParams);
      let decryptUpdate = await decoder.doFinal(cipherText);
      return decryptUpdate;
    }
    
    async function genSymKeyByData(symKeyData: Uint8Array) {
      let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
      let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128');
      let symKey = await aesGenerator.convertKey(symKeyBlob);
      console.info('convertKey result: success.');
      return symKey;
    }
    
    export async function main() : Promise<string> {
      try {
        let keyData = new Uint8Array(\[83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159\]);
        let symKey = await genSymKeyByData(keyData);
        let message = 'This is a test';
        let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) };
        let encryptText = await encryptMessagePromise(symKey, plainText);
        let decryptText = await decryptMessagePromise(symKey, encryptText);
        if (plainText.data.toString() === decryptText.data.toString()) {
          console.info('decrypt ok.');
          console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8'));
        } else {
          console.error('decrypt failed.');
        }
        return 'success';
      } catch (error) {
        console.error('decrypt failed. error: ' + error);
        return 'fail';
      }
    }
    
*   同步方法示例：
    
    import { cryptoFramework } from '@kit.CryptoArchitectureKit';
    import { buffer } from '@kit.ArkTS';
    
    function genCcmAeadParamsSpec() {
      let nonce = \[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12\]; // 12 bytes
      let nonceValue = new Uint8Array(nonce);
      let aad = \[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12\]; // 12 bytes
      let aadValue = new Uint8Array(aad);
      let aeadParamsSpec: cryptoFramework.AeadParamsSpec = {
          nonce: nonceValue,
          authenticatedData: aadValue,
          algName: 'AeadParamsSpec'
      };
      return aeadParamsSpec;
    }
    
      let aeadParams = genCcmAeadParamsSpec();
    
    // 加密消息
    function encryptMessage(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) {
      let cipher = cryptoFramework.createCipher('AES128|CCM');
      cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT\_MODE, symKey, aeadParams);
      let encryptUpdate = cipher.doFinalSync(plainText);
      return encryptUpdate;
    }
    
    // 解密消息
    function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) {
      let decoder = cryptoFramework.createCipher('AES128|CCM');
      decoder.initSync(cryptoFramework.CryptoMode.DECRYPT\_MODE, symKey, aeadParams);
      let decryptUpdate = decoder.doFinalSync(cipherText);
      return decryptUpdate;
    }
    
    function genSymKeyByData(symKeyData: Uint8Array) {
      let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
      let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128');
      let symKey = aesGenerator.convertKeySync(symKeyBlob);
      console.info('convertKeySync result: success.');
      return symKey;
    }
    
    export function main() : string {
      try {
        let keyData = new Uint8Array(\[83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159\]);
        let symKey = genSymKeyByData(keyData);
        let message = 'This is a test';
        let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) };
        let encryptText = encryptMessage(symKey, plainText);
        let decryptText = decryptMessage(symKey, encryptText);
        if (plainText.data.toString() === decryptText.data.toString()) {
          console.info('decrypt ok.');
          console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8'));
        } else {
          console.error('decrypt failed.');
        }
        return 'success';
      } catch (error) {
        console.error('decrypt failed. error: ' + error);
        return 'fail';
      }
    }