加解密(ArkTS)
==========

以AES128、RSA2048、SM2和DES64为例，完成加解密。具体的场景介绍及支持的算法规格，请参考[加解密支持的算法](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-encryption-decryption-overview#支持的算法)。

#### 开发步骤

**生成密钥**

1.  指定密钥别名，密钥别名命名规范参考[密钥生成介绍及算法规格](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-key-generation-overview)。
    
2.  初始化密钥属性集。
    
3.  调用[generateKeyItem](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-huks#huksgeneratekeyitem9)生成密钥，具体请参考[密钥生成](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-key-generation-overview)。
    

除此之外，开发者也可以参考[密钥导入](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-key-import-overview)，导入已有的密钥。

**加密**

1.  获取密钥别名。
    
2.  获取待加密的数据。
    
3.  使用[HuksParam](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-huks#huksparam)设置加密算法参数配置。
    
    文档中提供多个示例，当使用不同算法时，请注意配置对应参数。
    
    *   使用AES算法加密，选取的分组模式为CBC、填充模式为PKCS7时，参数IV必选，请见开发案例：[AES/CBC/PKCS7](#aescbcpkcs7)。
    *   使用AES算法加密，选取的分组模式为GCM时，参数NONCE可选，AAD可选，请见开发案例：[AES/GCM/NoPadding](#aesgcmnopadding)。
    *   使用AES算法加密，选取的分组模式为CCM时，参数NONCE可选，AAD可选，请见开发案例：[AES/CCM/NoPadding](#aesccmnopadding)。
    *   使用RSA算法加密，需要选择相对应的分组模式、填充模式以及摘要算法DIGEST，请见开发案例：[RSA/ECB/PKCS1\_V1\_5](#rsaecbpkcs1_v1_5)和[RSA/ECB/OAEP/SHA256](#rsaecboaepsha256)。
    *   使用SM2算法加密，摘要算法DIGEST需要指定为SM3，请见开发案例：[SM2](#sm2)。
    
    详细规格请参考[加密/解密介绍及算法规格](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-encryption-decryption-overview)。
    
4.  调用[initSession](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-huks#huksinitsession9)初始化密钥会话，并获取会话的句柄handle。
    
5.  调用[finishSession](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-huks#huksfinishsession9)结束密钥会话，获取加密后的密文。
    

**解密**

1.  获取密钥别名。
    
2.  获取待解密的密文。
    
3.  使用[HuksParam](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-huks#huksparam)设置解密算法参数配置。
    
    文档中提供多个示例，当使用不同算法时，请注意配置对应参数。
    
    *   使用AES算法解密，用例中选取的分组模式为GCM时，必须要填参数NONCE和参数AEAD，AAD可选，请见开发案例：[AES/GCM/NoPadding](#aesgcmnopadding)。
    *   其余示例参数与加密要求一致。
    
    详细规格请参考[加密/解密介绍及算法规格](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-encryption-decryption-overview)。
    
4.  调用[initSession](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-huks#huksinitsession9)初始化密钥会话，并获取会话的句柄handle。
    
5.  调用[finishSession](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-huks#huksfinishsession9)结束密钥会话，获取解密后的数据。
    

**删除密钥**

当密钥废弃不用时，需要调用[deleteKeyItem](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-huks#huksdeletekeyitem9)删除密钥，具体请参考[密钥删除](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-delete-key-arkts)。

#### 开发案例

#### \[h2\]AES/CBC/PKCS7

/\*
 \* 以下以AES/CBC/PKCS7的Promise操作使用为例
 \*/
import { huks } from '@kit.UniversalKeystoreKit';
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';

let aesKeyAlias = 'test\_aesKeyAlias';
let handle: number;
let plainText = '123456';
let IV = cryptoFramework.createRandom().generateRandomSync(12).data;
let cipherData: Uint8Array;

function stringToUint8Array(str: string) {
  let arr: number\[\] = \[\];
  for (let i = 0, j = str.length; i < j; ++i) {
    arr.push(str.charCodeAt(i));
  }
  return new Uint8Array(arr);
}

function uint8ArrayToString(fileData: Uint8Array) {
  let dataString = '';
  for (let i = 0; i < fileData.length; i++) {
    dataString += String.fromCharCode(fileData\[i\]);
  }
  return dataString;
}

function getAesGenerateProperties() {
  let properties: huks.HuksParam\[\] = \[{
    tag: huks.HuksTag.HUKS\_TAG\_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS\_ALG\_AES
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_KEY\_SIZE,
    value: huks.HuksKeySize.HUKS\_AES\_KEY\_SIZE\_128
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_ENCRYPT |
    huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_DECRYPT
  }\];
  return properties;
}

function getAesEncryptProperties() {
  let properties: huks.HuksParam\[\] = \[{
    tag: huks.HuksTag.HUKS\_TAG\_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS\_ALG\_AES
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_KEY\_SIZE,
    value: huks.HuksKeySize.HUKS\_AES\_KEY\_SIZE\_128
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_ENCRYPT
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PADDING,
    value: huks.HuksKeyPadding.HUKS\_PADDING\_PKCS7
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_BLOCK\_MODE,
    value: huks.HuksCipherMode.HUKS\_MODE\_CBC
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_IV,
    value: IV
  }\];
  return properties;
}

function getAesDecryptProperties() {
  let properties: huks.HuksParam\[\] = \[{
    tag: huks.HuksTag.HUKS\_TAG\_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS\_ALG\_AES
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_KEY\_SIZE,
    value: huks.HuksKeySize.HUKS\_AES\_KEY\_SIZE\_128
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_DECRYPT
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PADDING,
    value: huks.HuksKeyPadding.HUKS\_PADDING\_PKCS7
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_BLOCK\_MODE,
    value: huks.HuksCipherMode.HUKS\_MODE\_CBC
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_IV,
    value: IV
  }\];
  return properties;
}

async function generateAesKey() {
  /\*
   \* 模拟生成密钥场景
   \*/
  /\*
   \* 1. 获取生成密钥算法参数配置
   \*/
  let genProperties = getAesGenerateProperties();
  let options: huks.HuksOptions = {
    properties: genProperties
  }
  /\*
   \* 2. 调用generateKeyItem
   \*/
  await huks.generateKeyItem(aesKeyAlias, options)
    .then(() => {
      console.info(\`promise: generate AES Key success\`);
    }).catch((error: BusinessError) => {
      console.error(\`promise: generate AES Key failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function encryptData() {
  /\*
   \* 模拟加密场景
   \*/
  /\*
   \* 1. 获取加密算法参数配置
   \*/
  let encryptProperties = getAesEncryptProperties();
  let options: huks.HuksOptions = {
    properties: encryptProperties,
    inData: stringToUint8Array(plainText)
  }
  /\*
   \* 2. 调用initSession获取handle
   \*/
  await huks.initSession(aesKeyAlias, options)
    .then((data) => {
      handle = data.handle;
    }).catch((error: BusinessError) => {
      console.error(\`promise: init EncryptData failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
  /\*
   \* 3. 调用finishSession获取加密后的密文
   \*/
  await huks.finishSession(handle, options)
    .then((data) => {
      console.info(\`promise: encrypt data success, data is \` + uint8ArrayToString(data.outData as Uint8Array));
      cipherData = data.outData as Uint8Array;
    }).catch((error: BusinessError) => {
      console.error(\`promise: encrypt data failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function decryptData() {
  /\*
   \* 模拟解密场景
   \* 1. 获取密钥别名
   \*/
  /\*
   \* 1. 获取解密算法参数配置
   \*/
  let decryptOptions = getAesDecryptProperties()
  let options: huks.HuksOptions = {
    properties: decryptOptions,
    inData: cipherData
  }
  /\*
   \* 2. 调用initSession获取handle
   \*/
  await huks.initSession(aesKeyAlias, options)
    .then((data) => {
      handle = data.handle;
    }).catch((error: BusinessError) => {
      console.error(\`promise: init DecryptData failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
  /\*
   \* 3. 调用finishSession获取解密后的数据
   \*/
  await huks.finishSession(handle, options)
    .then((data) => {
      console.info(\`promise: decrypt data success, data is \` + uint8ArrayToString(data.outData as Uint8Array));
    }).catch((error: BusinessError) => {
      console.error(\`promise: decrypt data failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function deleteKey() {
  /\*
   \* 模拟删除密钥场景
   \*/
  let emptyOptions: huks.HuksOptions = {
    properties: \[\]
  }
  /\*
   \* 1. 调用deleteKeyItem删除密钥
   \*/
  await huks.deleteKeyItem(aesKeyAlias, emptyOptions)
    .then(() => {
      console.info(\`promise: delete data success\`);
    }).catch((error: BusinessError) => {
      console.error(\`promise: delete data failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

#### \[h2\]AES/GCM/NoPadding

/\*
 \* 以下以AES/GCM/NoPadding的Promise操作使用为例
 \*/
import { huks } from '@kit.UniversalKeystoreKit';
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';

let aesKeyAlias = 'test\_aesKeyAlias';
let handle: number;
let plainText = '123456';
let cipherData: Uint8Array;
let AAD = '1234567890123456';
let NONCE = cryptoFramework.createRandom().generateRandomSync(12).data;

function stringToUint8Array(str: string) {
  let arr: number\[\] = \[\];
  for (let i = 0, j = str.length; i < j; ++i) {
    arr.push(str.charCodeAt(i));
  }
  return new Uint8Array(arr);
}

function uint8ArrayToString(fileData: Uint8Array) {
  let dataString = '';
  for (let i = 0; i < fileData.length; i++) {
    dataString += String.fromCharCode(fileData\[i\]);
  }
  return dataString;
}

function getAesGenerateProperties() {
  let properties: huks.HuksParam\[\] = \[{
    tag: huks.HuksTag.HUKS\_TAG\_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS\_ALG\_AES
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_KEY\_SIZE,
    value: huks.HuksKeySize.HUKS\_AES\_KEY\_SIZE\_128
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_ENCRYPT |
    huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_DECRYPT
  }\];
  return properties;
}

function getAesGcmEncryptProperties() {
  let properties: huks.HuksParam\[\] = \[{
    tag: huks.HuksTag.HUKS\_TAG\_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS\_ALG\_AES
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_KEY\_SIZE,
    value: huks.HuksKeySize.HUKS\_AES\_KEY\_SIZE\_128
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_ENCRYPT
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PADDING,
    value: huks.HuksKeyPadding.HUKS\_PADDING\_NONE
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_BLOCK\_MODE,
    value: huks.HuksCipherMode.HUKS\_MODE\_GCM
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_NONCE,
    value: NONCE
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_ASSOCIATED\_DATA,
    value: stringToUint8Array(AAD)
  }\];
  return properties;
}

function getAesGcmDecryptProperties(cipherData: Uint8Array) {
  let properties: huks.HuksParam\[\] = \[{
    tag: huks.HuksTag.HUKS\_TAG\_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS\_ALG\_AES
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_KEY\_SIZE,
    value: huks.HuksKeySize.HUKS\_AES\_KEY\_SIZE\_128
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_DECRYPT
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PADDING,
    value: huks.HuksKeyPadding.HUKS\_PADDING\_NONE
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_BLOCK\_MODE,
    value: huks.HuksCipherMode.HUKS\_MODE\_GCM
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_NONCE,
    value: NONCE
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_ASSOCIATED\_DATA,
    value: stringToUint8Array(AAD)
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_AE\_TAG,
    value: cipherData.slice(cipherData.length - 16)
  }\];
  return properties;
}

async function generateAesKey() {
  /\*
   \* 模拟生成密钥场景
   \*/
  /\*
   \* 1. 获取生成密钥算法参数配置
   \*/
  let genProperties = getAesGenerateProperties();
  let options: huks.HuksOptions = {
    properties: genProperties
  }
  /\*
   \* 2. 调用generateKeyItem
   \*/
  await huks.generateKeyItem(aesKeyAlias, options)
    .then(() => {
      console.info(\`promise: generate AES Key success\`);
    }).catch((error: BusinessError) => {
      console.error(\`promise: generate AES Key failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function encryptData() {
  /\*
   \* 模拟加密场景
   \*/
  /\*
   \* 1. 获取加密算法参数配置
   \*/
  let encryptProperties = getAesGcmEncryptProperties();
  let options: huks.HuksOptions = {
    properties: encryptProperties,
    inData: stringToUint8Array(plainText)
  }
  /\*
   \* 2. 调用initSession获取handle
   \*/
  await huks.initSession(aesKeyAlias, options)
    .then((data) => {
      handle = data.handle;
    }).catch((error: BusinessError) => {
      console.error(\`promise: init EncryptDataGcm failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
  /\*
   \* 3. 调用finishSession获取加密后的密文
   \*/
  await huks.finishSession(handle, options)
    .then((data) => {
      console.info(\`promise: encrypt data success, data is \` + uint8ArrayToString(data.outData as Uint8Array));
      cipherData = data.outData as Uint8Array;
    }).catch((error: BusinessError) => {
      console.error(\`promise: encrypt data failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function decryptData() {
  /\*
   \* 模拟解密场景
   \*/
  /\*
   \* 1. 获取解密算法参数配置
   \*/
  let decryptOptions = getAesGcmDecryptProperties(cipherData)
  let options: huks.HuksOptions = {
    properties: decryptOptions,
    inData: cipherData.slice(0, cipherData.length - 16)
  }
  /\*
   \* 2. 调用initSession获取handle
   \*/
  await huks.initSession(aesKeyAlias, options)
    .then((data) => {
      handle = data.handle;
    }).catch((error: BusinessError) => {
      console.error(\`promise: init DecryptDataGcm failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
  /\*
   \* 3. 调用finishSession获取解密后的数据
   \*/
  await huks.finishSession(handle, options)
    .then((data) => {
      console.info(\`promise: decrypt data success, data is \` + uint8ArrayToString(data.outData as Uint8Array));
    }).catch((error: BusinessError) => {
      console.error(\`promise: decrypt data failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function deleteKey() {
  /\*
   \* 模拟删除密钥场景
   \*/
  let emptyOptions: huks.HuksOptions = {
    properties: \[\]
  }
  /\*
   \* 1. 调用deleteKeyItem删除密钥
   \*/
  await huks.deleteKeyItem(aesKeyAlias, emptyOptions)
    .then(() => {
      console.info(\`promise: delete data success\`);
    }).catch((error: BusinessError) => {
      console.error(\`promise: delete data failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

#### \[h2\]AES/CCM/NoPadding

/\*
 \* 以下以AES/CCM/NoPadding的Promise操作使用为例
 \*/
import { huks } from '@kit.UniversalKeystoreKit';
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from "@kit.BasicServicesKit";

let aesKeyAlias = 'test\_aesCcmKeyAlias';
let handle: number;
let plainText = '123456';
let cipherData: Uint8Array;
let AAD = '1234567890123456';
let NONCE = cryptoFramework.createRandom().generateRandomSync(12).data;
let aeadTagLen = 14;

function StringToUint8Array(str: string) {
  let arr: number\[\] = new Array();
  for (let i = 0, j = str.length; i < j; ++i) {
    arr.push(str.charCodeAt(i));
  }
  return new Uint8Array(arr);
}

function Uint8ArrayToString(fileData: Uint8Array) {
  let dataString = '';
  for (let i = 0; i < fileData.length; i++) {
    dataString += String.fromCharCode(fileData\[i\]);
  }
  return dataString;
}

function GetAesGenerateProperties() {
  let properties: Array<huks.HuksParam> = \[{
    tag: huks.HuksTag.HUKS\_TAG\_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS\_ALG\_AES
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_KEY\_SIZE,
    value: huks.HuksKeySize.HUKS\_AES\_KEY\_SIZE\_128
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_ENCRYPT |
    huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_DECRYPT
  }\];
  return properties;
}

function GetAesCcmEncryptProperties() {
  let properties: Array<huks.HuksParam> = \[{
    tag: huks.HuksTag.HUKS\_TAG\_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS\_ALG\_AES
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_KEY\_SIZE,
    value: huks.HuksKeySize.HUKS\_AES\_KEY\_SIZE\_128
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_ENCRYPT
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PADDING,
    value: huks.HuksKeyPadding.HUKS\_PADDING\_NONE
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_BLOCK\_MODE,
    value: huks.HuksCipherMode.HUKS\_MODE\_CCM
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_NONCE,
    value: NONCE
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_ASSOCIATED\_DATA,
    value: StringToUint8Array(AAD)
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_AE\_TAG\_LEN,
    value: aeadTagLen
  }\];
  return properties;
}

function GetAesCcmDecryptProperties(cipherData: Uint8Array) {
  let properties: Array<huks.HuksParam> = \[{
    tag: huks.HuksTag.HUKS\_TAG\_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS\_ALG\_AES
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_KEY\_SIZE,
    value: huks.HuksKeySize.HUKS\_AES\_KEY\_SIZE\_128
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_DECRYPT
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PADDING,
    value: huks.HuksKeyPadding.HUKS\_PADDING\_NONE
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_BLOCK\_MODE,
    value: huks.HuksCipherMode.HUKS\_MODE\_CCM
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_NONCE,
    value: NONCE
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_ASSOCIATED\_DATA,
    value: StringToUint8Array(AAD)
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_AE\_TAG,
    value: cipherData.slice(cipherData.length - aeadTagLen)
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_AE\_TAG\_LEN,
    value: aeadTagLen
  }\];
  return properties;
}

async function GenerateAesKey() {
  /\*
   \* 模拟生成密钥场景
   \* 1. 确定密钥别名
   \*/
  /\*
   \* 2. 获取生成密钥算法参数配置
   \*/
  let genProperties = GetAesGenerateProperties();
  let options: huks.HuksOptions = {
    properties: genProperties
  }
  /\*
   \* 3. 调用generateKeyItem
   \*/
  await huks.generateKeyItem(aesKeyAlias, options)
    .then(() => {
      console.info(\`promise: generate AES Key success\`);
    }).catch((error: BusinessError) => {
      console.error(\`promise: generate AES Key failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function EncryptData() {
  /\*
   \* 模拟加密场景
   \* 1. 获取密钥别名
   \*/
  /\*
   \* 2. 获取待加密的数据
   \*/
  /\*
   \* 3. 获取加密算法参数配置
   \*/
  let encryptProperties = GetAesCcmEncryptProperties();
  let options: huks.HuksOptions = {
    properties: encryptProperties,
    inData: StringToUint8Array(plainText)
  }
  /\*
   \* 4. 调用initSession获取handle
   \*/
  await huks.initSession(aesKeyAlias, options)
    .then((data) => {
      handle = data.handle;
    }).catch((error: BusinessError) => {
      console.error(\`promise: init EncryptDataCcm failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
  /\*
   \* 5. 调用finishSession获取加密后的密文
   \*/
  await huks.finishSession(handle, options)
    .then((data) => {
      console.info(\`promise: encrypt data success, data is \` + Uint8ArrayToString(data.outData as Uint8Array));
      cipherData = data.outData as Uint8Array;
    }).catch((error: BusinessError) => {
      console.error(\`promise: encrypt data failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function DecryptData() {
  /\*
   \* 模拟解密场景
   \* 1. 获取密钥别名
   \*/
  /\*
   \* 2. 获取待解密的密文
   \*/
  /\*
   \* 3. 获取解密算法参数配置
   \*/
  let decryptOptions = GetAesCcmDecryptProperties(cipherData)
  let options: huks.HuksOptions = {
    properties: decryptOptions,
    inData: cipherData.slice(0, cipherData.length - aeadTagLen)
  }
  /\*
   \* 4. 调用initSession获取handle
   \*/
  await huks.initSession(aesKeyAlias, options)
    .then((data) => {
      handle = data.handle;
    }).catch((error: BusinessError) => {
      console.error(\`promise: init DecryptDataCcm failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
  /\*
   \* 5. 调用finishSession获取解密后的数据
   \*/
  await huks.finishSession(handle, options)
    .then((data) => {
      console.info(\`promise: decrypt data success, data is \` + Uint8ArrayToString(data.outData as Uint8Array));
    }).catch((error: BusinessError) => {
      console.error(\`promise: decrypt data failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function DeleteKey() {
  /\*
   \* 模拟删除密钥场景
   \* 1. 获取密钥别名
   \*/
  let emptyOptions: huks.HuksOptions = {
    properties: \[\]
  }
  /\*
   \* 2. 调用deleteKeyItem删除密钥
   \*/
  await huks.deleteKeyItem(aesKeyAlias, emptyOptions)
    .then(() => {
      console.info(\`promise: delete data success\`);
    }).catch((error: BusinessError) => {
      console.error(\`promise: delete data failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function TestEncryptDecrypt() {
  await GenerateAesKey();
  await EncryptData();
  await DecryptData();
  await DeleteKey();
}

#### \[h2\]RSA/ECB/PKCS1\_V1\_5

/\*
 \* 以下以RSA/ECB/PKCS1\_V1\_5模式的Promise操作使用为例
 \*/
import { huks } from '@kit.UniversalKeystoreKit';
import { BusinessError } from '@kit.BasicServicesKit';

let rsaKeyAlias = 'test\_rsaKeyAlias';
let handle: number;
let plainText = '123456';
let cipherData: Uint8Array;

function stringToUint8Array(str: string) {
  let arr: number\[\] = \[\];
  for (let i = 0, j = str.length; i < j; ++i) {
    arr.push(str.charCodeAt(i));
  }
  return new Uint8Array(arr);
}

function uint8ArrayToString(fileData: Uint8Array) {
  let dataString = '';
  for (let i = 0; i < fileData.length; i++) {
    dataString += String.fromCharCode(fileData\[i\]);
  }
  return dataString;
}

function getRsaGenerateProperties() {
  let properties: huks.HuksParam\[\] = \[{
    tag: huks.HuksTag.HUKS\_TAG\_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS\_ALG\_RSA
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_KEY\_SIZE,
    value: huks.HuksKeySize.HUKS\_RSA\_KEY\_SIZE\_2048
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_ENCRYPT |
    huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_DECRYPT
  }\];
  return properties;
}

function getRsaEncryptProperties() {
  let properties: huks.HuksParam\[\] = \[{
    tag: huks.HuksTag.HUKS\_TAG\_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS\_ALG\_RSA
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_KEY\_SIZE,
    value: huks.HuksKeySize.HUKS\_RSA\_KEY\_SIZE\_2048
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_ENCRYPT
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PADDING,
    value: huks.HuksKeyPadding.HUKS\_PADDING\_PKCS1\_V1\_5
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_BLOCK\_MODE,
    value: huks.HuksCipherMode.HUKS\_MODE\_ECB
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_DIGEST,
    value: huks.HuksKeyDigest.HUKS\_DIGEST\_NONE
  }\];
  return properties;
}

function getRsaDecryptProperties() {
  let properties: huks.HuksParam\[\] = \[{
    tag: huks.HuksTag.HUKS\_TAG\_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS\_ALG\_RSA
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_KEY\_SIZE,
    value: huks.HuksKeySize.HUKS\_RSA\_KEY\_SIZE\_2048
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_DECRYPT
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PADDING,
    value: huks.HuksKeyPadding.HUKS\_PADDING\_PKCS1\_V1\_5
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_BLOCK\_MODE,
    value: huks.HuksCipherMode.HUKS\_MODE\_ECB
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_DIGEST,
    value: huks.HuksKeyDigest.HUKS\_DIGEST\_NONE
  }\];
  return properties;
}

async function generateRsaKey() {
  /\*
   \* 模拟生成密钥场景
   \*/
  /\*
   \* 1. 获取生成密钥算法参数配置
   \*/
  let genProperties = getRsaGenerateProperties();
  let options: huks.HuksOptions = {
    properties: genProperties
  }
  /\*
   \* 2. 调用generateKeyItem
   \*/
  await huks.generateKeyItem(rsaKeyAlias, options)
    .then(() => {
      console.info(\`promise: generate RSA Key success\`);
    }).catch((error: BusinessError) => {
      console.error(\`promise: generate RSA Key failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function encryptData() {
  /\*
   \* 模拟加密场景
   \*/
  /\*
   \* 1. 获取加密算法参数配置
   \*/
  let encryptProperties = getRsaEncryptProperties();
  let options: huks.HuksOptions = {
    properties: encryptProperties,
    inData: stringToUint8Array(plainText)
  }
  /\*
   \* 2. 调用initSession获取handle
   \*/
  await huks.initSession(rsaKeyAlias, options)
    .then((data) => {
      handle = data.handle;
    }).catch((error: BusinessError) => {
      console.error(\`promise: init EncryptDataRsa failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
  /\*
   \* 3. 调用finishSession获取加密后的密文
   \*/
  await huks.finishSession(handle, options)
    .then((data) => {
      console.info(\`promise: encrypt data success, data is \` + uint8ArrayToString(data.outData as Uint8Array));
      cipherData = data.outData as Uint8Array;
    }).catch((error: BusinessError) => {
      console.error(\`promise: encrypt data failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function decryptData() {
  /\*
   \* 模拟解密场景
   \*/
  /\*
   \* 1. 获取解密算法参数配置
   \*/
  let decryptOptions = getRsaDecryptProperties()
  let options: huks.HuksOptions = {
    properties: decryptOptions,
    inData: cipherData
  }
  /\*
   \* 2. 调用initSession获取handle
   \*/
  await huks.initSession(rsaKeyAlias, options)
    .then((data) => {
      handle = data.handle;
    }).catch((error: BusinessError) => {
      console.error(\`promise: init DecryptDataRsa failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
  /\*
   \* 3. 调用finishSession获取解密后的数据
   \*/
  await huks.finishSession(handle, options)
    .then((data) => {
      console.info(\`promise: decrypt data success, data is \` + uint8ArrayToString(data.outData as Uint8Array));
    }).catch((error: BusinessError) => {
      console.error(\`promise: decrypt data failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function deleteKey() {
  /\*
   \* 模拟删除密钥场景
   \*/
  let emptyOptions: huks.HuksOptions = {
    properties: \[\]
  }
  /\*
   \* 1. 调用deleteKeyItem删除密钥
   \*/
  await huks.deleteKeyItem(rsaKeyAlias, emptyOptions)
    .then(() => {
      console.info(\`promise: delete data success\`);
    }).catch((error: BusinessError) => {
      console.error(\`promise: delete data failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

#### \[h2\]RSA/ECB/OAEP/SHA256

/\*
 \* 以下以RSA/ECB/OAEP/SHA256模式的Promise操作使用为例
 \*/
import { huks } from '@kit.UniversalKeystoreKit';
import { BusinessError } from '@kit.BasicServicesKit';

let rsaKeyAlias = 'test\_rsaKeyAlias';
let handle: number;
let plainText = '123456';
let cipherData: Uint8Array;

function stringToUint8Array(str: string) {
  let arr: number\[\] = \[\];
  for (let i = 0, j = str.length; i < j; ++i) {
    arr.push(str.charCodeAt(i));
  }
  return new Uint8Array(arr);
}

function uint8ArrayToString(fileData: Uint8Array) {
  let dataString = '';
  for (let i = 0; i < fileData.length; i++) {
    dataString += String.fromCharCode(fileData\[i\]);
  }
  return dataString;
}

function getRsaGenerateProperties() {
  let properties: huks.HuksParam\[\] = \[{
    tag: huks.HuksTag.HUKS\_TAG\_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS\_ALG\_RSA
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_KEY\_SIZE,
    value: huks.HuksKeySize.HUKS\_RSA\_KEY\_SIZE\_2048
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_ENCRYPT |
    huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_DECRYPT
  }\];
  return properties;
}

function getRsaEncryptProperties() {
  let properties: huks.HuksParam\[\] = \[{
    tag: huks.HuksTag.HUKS\_TAG\_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS\_ALG\_RSA
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_KEY\_SIZE,
    value: huks.HuksKeySize.HUKS\_RSA\_KEY\_SIZE\_2048
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_ENCRYPT
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PADDING,
    value: huks.HuksKeyPadding.HUKS\_PADDING\_OAEP
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_BLOCK\_MODE,
    value: huks.HuksCipherMode.HUKS\_MODE\_ECB
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_DIGEST,
    value: huks.HuksKeyDigest.HUKS\_DIGEST\_SHA256
  }\];
  return properties;
}

function getRsaDecryptProperties() {
  let properties: huks.HuksParam\[\] = \[{
    tag: huks.HuksTag.HUKS\_TAG\_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS\_ALG\_RSA
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_KEY\_SIZE,
    value: huks.HuksKeySize.HUKS\_RSA\_KEY\_SIZE\_2048
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_DECRYPT
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PADDING,
    value: huks.HuksKeyPadding.HUKS\_PADDING\_OAEP
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_BLOCK\_MODE,
    value: huks.HuksCipherMode.HUKS\_MODE\_ECB
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_DIGEST,
    value: huks.HuksKeyDigest.HUKS\_DIGEST\_SHA256
  }\];
  return properties;
}

async function generateRsaKey() {
  /\*
   \* 模拟生成密钥场景
   \*/
  /\*
   \* 1. 获取生成密钥算法参数配置
   \*/
  let genProperties = getRsaGenerateProperties();
  let options: huks.HuksOptions = {
    properties: genProperties
  }
  /\*
   \* 2. 调用generateKeyItem
   \*/
  await huks.generateKeyItem(rsaKeyAlias, options)
    .then(() => {
      console.info(\`promise: generate RSA Key success\`);
    }).catch((error: BusinessError) => {
      console.error(\`promise: generate RSA Key failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function encryptData() {
  /\*
   \* 模拟加密场景
   \*/
  /\*
   \* 1. 获取加密算法参数配置
   \*/
  let encryptProperties = getRsaEncryptProperties();
  let options: huks.HuksOptions = {
    properties: encryptProperties,
    inData: stringToUint8Array(plainText)
  }
  /\*
   \* 2. 调用initSession获取handle
   \*/
  await huks.initSession(rsaKeyAlias, options)
    .then((data) => {
      handle = data.handle;
    }).catch((error: BusinessError) => {
      console.error(\`promise: init EncryptDataRsa failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
  /\*
   \* 3. 调用finishSession获取加密后的密文
   \*/
  await huks.finishSession(handle, options)
    .then((data) => {
      console.info(\`promise: encrypt data success, data is \` + uint8ArrayToString(data.outData as Uint8Array));
      cipherData = data.outData as Uint8Array;
    }).catch((error: BusinessError) => {
      console.error(\`promise: encrypt data failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function decryptData() {
  /\*
   \* 模拟解密场景
   \*/
  /\*
   \* 1. 获取解密算法参数配置
   \*/
  let decryptOptions = getRsaDecryptProperties()
  let options: huks.HuksOptions = {
    properties: decryptOptions,
    inData: cipherData
  }
  /\*
   \* 2. 调用initSession获取handle
   \*/
  await huks.initSession(rsaKeyAlias, options)
    .then((data) => {
      handle = data.handle;
    }).catch((error: BusinessError) => {
      console.error(\`promise: init DecryptDataRsa failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
  /\*
   \* 3. 调用finishSession获取解密后的数据
   \*/
  await huks.finishSession(handle, options)
    .then((data) => {
      console.info(\`promise: decrypt data success, data is \` + uint8ArrayToString(data.outData as Uint8Array));
    }).catch((error: BusinessError) => {
      console.error(\`promise: decrypt data failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function deleteKey() {
  /\*
   \* 模拟删除密钥场景
   \*/
  let emptyOptions: huks.HuksOptions = {
    properties: \[\]
  }
  /\*
   \* 1. 调用deleteKeyItem删除密钥
   \*/
  await huks.deleteKeyItem(rsaKeyAlias, emptyOptions)
    .then((data) => {
      console.info(\`promise: delete data success\`);
    }).catch((error: BusinessError) => {
      console.error(\`promise: delete data failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

#### \[h2\]SM2

/\*
 \* 以下以SM2模式的Promise操作使用为例
 \*/
import { huks } from '@kit.UniversalKeystoreKit';
import { BusinessError } from '@kit.BasicServicesKit';

let sm2KeyAlias = 'test\_sm2KeyAlias';
let handle: number;
let plainText = '123456';
let cipherData: Uint8Array;

function stringToUint8Array(str: string) {
  let arr: number\[\] = \[\];
  for (let i = 0, j = str.length; i < j; ++i) {
    arr.push(str.charCodeAt(i));
  }
  return new Uint8Array(arr);
}

function uint8ArrayToString(fileData: Uint8Array) {
  let dataString = '';
  for (let i = 0; i < fileData.length; i++) {
    dataString += String.fromCharCode(fileData\[i\]);
  }
  return dataString;
}

function getSm2GenerateProperties() {
  let properties: huks.HuksParam\[\] = \[{
    tag: huks.HuksTag.HUKS\_TAG\_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS\_ALG\_SM2
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_KEY\_SIZE,
    value: huks.HuksKeySize.HUKS\_SM2\_KEY\_SIZE\_256
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_ENCRYPT |
    huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_DECRYPT
  }\];
  return properties;
}

function getSm2EncryptProperties() {
  let properties: huks.HuksParam\[\] = \[{
    tag: huks.HuksTag.HUKS\_TAG\_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS\_ALG\_SM2
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_KEY\_SIZE,
    value: huks.HuksKeySize.HUKS\_SM2\_KEY\_SIZE\_256
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_ENCRYPT
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_DIGEST,
    value: huks.HuksKeyDigest.HUKS\_DIGEST\_SM3
  }\];
  return properties;
}

function getSm2DecryptProperties() {
  let properties: huks.HuksParam\[\] = \[{
    tag: huks.HuksTag.HUKS\_TAG\_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS\_ALG\_SM2
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_KEY\_SIZE,
    value: huks.HuksKeySize.HUKS\_SM2\_KEY\_SIZE\_256
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS\_KEY\_PURPOSE\_DECRYPT
  }, {
    tag: huks.HuksTag.HUKS\_TAG\_DIGEST,
    value: huks.HuksKeyDigest.HUKS\_DIGEST\_SM3
  }\];
  return properties;
}

async function generateSm2Key() {
  /\*
   \* 模拟生成密钥场景
   \*/
  /\*
   \* 1. 获取生成密钥算法参数配置
   \*/
  let genProperties = getSm2GenerateProperties();
  let options: huks.HuksOptions = {
    properties: genProperties
  }
  /\*
   \* 2. 调用generateKeyItem
   \*/
  await huks.generateKeyItem(sm2KeyAlias, options)
    .then(() => {
      console.info(\`promise: generate SM2 Key success\`);
    }).catch((error: BusinessError) => {
      console.error(\`promise: generate SM2 Key failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function encryptDataSm2() {
  /\*
   \* 模拟加密场景
   \*/
  /\*
   \* 1. 获取加密算法参数配置
   \*/
  let encryptProperties = getSm2EncryptProperties();
  let options: huks.HuksOptions = {
    properties: encryptProperties,
    inData: stringToUint8Array(plainText)
  }
  /\*
   \* 2. 调用initSession获取handle
   \*/
  await huks.initSession(sm2KeyAlias, options)
    .then((data) => {
      handle = data.handle;
    }).catch((error: BusinessError) => {
      console.error(\`promise: init EncryptDataSm2 failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
  /\*
   \* 3. 调用finishSession获取加密后的密文
   \*/
  await huks.finishSession(handle, options)
    .then((data) => {
      console.info(\`promise: encrypt data success, data is \` + uint8ArrayToString(data.outData as Uint8Array));
      cipherData = data.outData as Uint8Array;
    }).catch((error: BusinessError) => {
      console.error(\`promise: encrypt data failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function decryptDataSm2() {
  /\*
   \* 模拟解密场景
   \*/
  /\*
   \* 1. 获取解密算法参数配置
   \*/
  let decryptOptions = getSm2DecryptProperties()
  let options: huks.HuksOptions = {
    properties: decryptOptions,
    inData: cipherData
  }
  /\*
   \* 2. 调用initSession获取handle
   \*/
  await huks.initSession(sm2KeyAlias, options)
    .then((data) => {
      handle = data.handle;
    }).catch((error: BusinessError) => {
      console.error(\`promise: init DecryptDataSm2 failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
  /\*
   \* 3. 调用finishSession获取解密后的数据
   \*/
  await huks.finishSession(handle, options)
    .then((data) => {
      console.info(\`promise: decrypt data success, data is \` + uint8ArrayToString(data.outData as Uint8Array));
    }).catch((error: BusinessError) => {
      console.error(\`promise: decrypt data failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}

async function deleteKey() {
  /\*
   \* 模拟删除密钥场景
   \*/
  let emptyOptions: huks.HuksOptions = {
    properties: \[\]
  }
  /\*
   \* 1. 调用deleteKeyItem删除密钥
   \*/
  await huks.deleteKeyItem(sm2KeyAlias, emptyOptions)
    .then(() => {
      console.info(\`promise: delete data success\`);
    }).catch((error: BusinessError) => {
      console.error(\`promise: delete data failed, errCode : ${error.code}, errMsg : ${error.message}\`);
    })
}