使用SM4对称密钥（GCM模式）分段加解密(ArkTS)
============================

对应的算法规格请查看[对称密钥加解密算法规格：SM4](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/crypto-sym-encrypt-decrypt-spec#sm4)。

**加密**

1.  调用[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)，生成密钥算法为SM4、密钥长度为128位的对称密钥（SymKey）。
    
    如何生成SM4对称密钥，开发者可参考下文示例，并结合[对称密钥生成和转换规格：SM4](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/crypto-sym-key-generation-conversion-spec#sm4)和[随机生成对称密钥](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/crypto-generate-sym-key-randomly)理解，参考文档与当前示例可能存在入参差异，请在阅读时注意区分。
    
2.  调用[cryptoFramework.createCipher](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework#cryptoframeworkcreatecipher)，指定字符串参数'SM4\_128|GCM'，创建对称密钥类型为SM4\_128、分组模式为GCM的Cipher实例，用于完成加密操作。
    
3.  调用[Cipher.init](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework#init-1)，设置模式为加密（cryptoFramework.CryptoMode.ENCRYPT\_MODE），指定加密密钥（SymKey）和GCM模式对应的加密参数（GcmParamsSpec），初始化加密Cipher实例。
    
4.  将一次传入数据量设置为20字节，多次调用[Cipher.update](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework#update-1)，更新数据（明文）。
    
    *   当前单次update长度没有限制，开发者可以根据数据量判断如何调用update。
        
    *   建议开发者对每次update的结果都判断是否为null，并在结果不为null时取出其中的数据进行拼接，形成完整的密文。因为在不同的模式下，update的结果可能会受到不同影响。
        
        1）比如ECB和CBC模式，始终以分组作为基本单位来加密，并输出本次update产生的加密分组结果。即当本次update操作凑满一个分组就输出密文，没有凑满则此次update输出null，将未加密的数据与下次输入的数据拼接凑分组再输出。等到最后doFinal的时候，将未加密的数据，根据指定的填充模式进行填充，再输出剩余加密结果。解密过程中的update同理。
        
        2）对于流加密模式（比如CTR和OFB模式），通常密文长度和明文长度相等。
        
5.  调用[Cipher.doFinal](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework#dofinal-1)，获取加密后的数据。
    
    *   由于已使用update传入数据，此处data传入null。
    *   doFinal输出结果可能为null，在访问具体数据前，需要先判断结果是否为null，避免产生异常。
6.  读取[GcmParamsSpec](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework#gcmparamsspec).authTag作为解密的认证信息。
    
    在GCM模式下，需要从加密后的数据中取出末尾16字节，作为解密时初始化的认证信息。示例中authTag恰好为16字节。
    

**解密**

1.  调用[cryptoFramework.createCipher](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework#cryptoframeworkcreatecipher)，指定字符串参数'SM4\_128|GCM'，创建对称密钥类型为SM4\_128、分组模式为GCM的Cipher实例，用于完成解密操作。
    
2.  调用[Cipher.init](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework#init-1)，设置模式为解密（cryptoFramework.CryptoMode.DECRYPT\_MODE），指定解密密钥（SymKey）和GCM模式对应的解密参数（GcmParamsSpec），初始化解密Cipher实例。
    
3.  将一次传入数据量设置为20字节，多次调用[Cipher.update](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework#update-1)，更新数据（密文）。
    
4.  调用[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 generateRandom(len: number) {
      let rand = cryptoFramework.createRandom();
      let generateRandSync = rand.generateRandomSync(len);
      return generateRandSync;
    }
    
    function genGcmParamsSpec() {
      let ivBlob = generateRandom(12);
      let arr = \[1, 2, 3, 4, 5, 6, 7, 8\]; // 8 bytes
      let dataAad = new Uint8Array(arr);
      let aadBlob: cryptoFramework.DataBlob = { data: dataAad };
      arr = \[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0\]; // 16 bytes
      let dataTag = new Uint8Array(arr);
      let tagBlob: cryptoFramework.DataBlob = {
        data: dataTag
      };
      let gcmParamsSpec: cryptoFramework.GcmParamsSpec = {
        iv: ivBlob,
        aad: aadBlob,
        authTag: tagBlob,
        algName: 'GcmParamsSpec'
      };
      return gcmParamsSpec;
    }
    
    let gcmParams = genGcmParamsSpec();
    
    // 分段加密消息
    async function encryptMessageUpdateBySegment(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) {
      let cipher = cryptoFramework.createCipher('SM4\_128|GCM');
      await cipher.init(cryptoFramework.CryptoMode.ENCRYPT\_MODE, symKey, gcmParams);
      let updateLength = 20; // 假设以20字节为单位进行分段update，实际并无要求
      let cipherText = new Uint8Array();
      for (let i = 0; i < plainText.data.length; i += updateLength) {
        let updateMessage = plainText.data.subarray(i, i + updateLength);
        let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
        // 分段update
        let updateOutput = await cipher.update(updateMessageBlob);
        // 把update的结果拼接起来，得到密文（有些情况下还需拼接doFinal的结果，这取决于分组模式
        // 和填充模式，本例中GCM模式的doFinal结果只包含authTag而不含密文，所以不需要拼接）
        let mergeText = new Uint8Array(cipherText.length + updateOutput.data.length);
        mergeText.set(cipherText);
        mergeText.set(updateOutput.data, cipherText.length);
        cipherText = mergeText;
      }
      gcmParams.authTag = await cipher.doFinal(null);
      let cipherBlob: cryptoFramework.DataBlob = { data: cipherText };
      return cipherBlob;
    }
    
    // 分段解密消息
    async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) {
      let decoder = cryptoFramework.createCipher('SM4\_128|GCM');
      await decoder.init(cryptoFramework.CryptoMode.DECRYPT\_MODE, symKey, gcmParams);
      let updateLength = 20; // 假设以20字节为单位进行分段update，实际并无要求
      let decryptText = new Uint8Array();
      for (let i = 0; i < cipherText.data.length; i += updateLength) {
        let updateMessage = cipherText.data.subarray(i, i + updateLength);
        let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
        // 分段update
        let updateOutput = await decoder.update(updateMessageBlob);
        // 把update的结果拼接起来，得到明文
        let mergeText = new Uint8Array(decryptText.length + updateOutput.data.length);
        mergeText.set(decryptText);
        mergeText.set(updateOutput.data, decryptText.length);
        decryptText = mergeText;
      }
      let decryptData = await decoder.doFinal(null);
      if (decryptData == null) {
        console.info('GCM decrypt result: success, decryptData is null.');
      }
      let decryptBlob: cryptoFramework.DataBlob = { data: decryptText };
      return decryptBlob;
    }
    
    async function genSymKeyByData(symKeyData: Uint8Array) {
      let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
      let sm4Generator = cryptoFramework.createSymKeyGenerator('SM4\_128');
      let symKey = await sm4Generator.convertKey(symKeyBlob);
      console.info('convertKey result: success.');
      return symKey;
    }
    
    async function sm4() {
      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 = 'aaaaa.....bbbbb.....ccccc.....ddddd.....eee'; // 假设信息总共43字节，根据utf-8解码后，也是43字节
      let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) };
      let encryptText = await encryptMessageUpdateBySegment(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.');
      }
    }
    
*   同步方法示例：
    
    import { cryptoFramework } from '@kit.CryptoArchitectureKit';
    import { buffer } from '@kit.ArkTS';
    
    function generateRandom(len: number) {
      let rand = cryptoFramework.createRandom();
      let generateRandSync = rand.generateRandomSync(len);
      return generateRandSync;
    }
    
    function genGcmParamsSpec() {
      let ivBlob = generateRandom(12); // 12 bytes
      let arr = \[1, 2, 3, 4, 5, 6, 7, 8\]; // 8 bytes
      let dataAad = new Uint8Array(arr);
      let aadBlob: cryptoFramework.DataBlob = { data: dataAad };
      arr = \[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0\]; // 16 bytes
      let dataTag = new Uint8Array(arr);
      let tagBlob: cryptoFramework.DataBlob = {
        data: dataTag
      };
      let gcmParamsSpec: cryptoFramework.GcmParamsSpec = {
        iv: ivBlob,
        aad: aadBlob,
        authTag: tagBlob,
        algName: 'GcmParamsSpec'
      };
      return gcmParamsSpec;
    }
    
    let gcmParams = genGcmParamsSpec();
    
    // 分段加密消息
    function encryptMessageUpdateBySegment(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) {
      let cipher = cryptoFramework.createCipher('SM4\_128|GCM');
      cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT\_MODE, symKey, gcmParams);
      let updateLength = 20; // 假设以20字节为单位进行分段update，实际并无要求
      let cipherText = new Uint8Array();
      for (let i = 0; i < plainText.data.length; i += updateLength) {
        let updateMessage = plainText.data.subarray(i, i + updateLength);
        let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
        // 分段update
        let updateOutput = cipher.updateSync(updateMessageBlob);
        // 把update的结果拼接起来，得到密文（有些情况下还需拼接doFinal的结果，这取决于分组模式
        // 和填充模式，本例中GCM模式的doFinal结果只包含authTag而不含密文，所以不需要拼接）
        let mergeText = new Uint8Array(cipherText.length + updateOutput.data.length);
        mergeText.set(cipherText);
        mergeText.set(updateOutput.data, cipherText.length);
        cipherText = mergeText;
      }
      gcmParams.authTag = cipher.doFinalSync(null);
      let cipherBlob: cryptoFramework.DataBlob = { data: cipherText };
      return cipherBlob;
    }
    
    // 分段解密消息
    function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) {
      let decoder = cryptoFramework.createCipher('SM4\_128|GCM');
      decoder.initSync(cryptoFramework.CryptoMode.DECRYPT\_MODE, symKey, gcmParams);
      let updateLength = 20; // 假设以20字节为单位进行分段update，实际并无要求
      let decryptText = new Uint8Array();
      for (let i = 0; i < cipherText.data.length; i += updateLength) {
        let updateMessage = cipherText.data.subarray(i, i + updateLength);
        let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
        // 分段update
        let updateOutput = decoder.updateSync(updateMessageBlob);
        // 把update的结果拼接起来，得到明文
        let mergeText = new Uint8Array(decryptText.length + updateOutput.data.length);
        mergeText.set(decryptText);
        mergeText.set(updateOutput.data, decryptText.length);
        decryptText = mergeText;
      }
      let decryptData = decoder.doFinalSync(null);
      if (decryptData == null) {
        console.info('GCM decrypt result: success, decryptData is null.');
      }
      let decryptBlob: cryptoFramework.DataBlob = { data: decryptText };
      return decryptBlob;
    }
    
    function genSymKeyByData(symKeyData: Uint8Array) {
      let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
      let sm4Generator = cryptoFramework.createSymKeyGenerator('SM4\_128');
      let symKey = sm4Generator.convertKeySync(symKeyBlob);
      console.info('convertKeySync result: success.');
      return symKey;
    }
    
    function main() {
      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 = 'aaaaa.....bbbbb.....ccccc.....ddddd.....eee'; // 假设信息总共43字节，根据utf-8解码后，也是43字节
      let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) };
      let encryptText = encryptMessageUpdateBySegment(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.');
      }
    }