1# Importing a Key in Plaintext (ArkTS) 2 3 4This topic walks you through on how to import an AES 256-bit key. For details about the scenarios and supported algorithm specifications, see [Supported Algorithms](huks-key-import-overview.md#supported-algorithms). 5 6 7## How to Develop 8 91. Set the alias **keyAlias** of the key to import. 10 The key alias cannot exceed 64 bytes. 11 122. Encapsulate the key property set and key material. 13 - The key property set must contain [HuksKeyAlg](../../reference/apis-universal-keystore-kit/js-apis-huks.md#hukskeyalg), [HuksKeySize](../../reference/apis-universal-keystore-kit/js-apis-huks.md#hukskeysize), and [HuksKeyPurpose](../../reference/apis-universal-keystore-kit/js-apis-huks.md#hukskeypurpose). 14 - The key material must comply with the [HUKS key material format](huks-concepts.md#key-material-format) and is used to fill the **inData** field of [HuksOptions](../../reference/apis-universal-keystore-kit/js-apis-huks.md#huksoptions) in Uint8Array format. 15 163. Use [huks.importKeyItem](../../reference/apis-universal-keystore-kit/js-apis-huks.md#huksimportkeyitem9) to import the key. 17 18```ts 19/* Import an AES 256-bit key in plaintext. This example uses callback-based APIs. */ 20import huks from '@ohos.security.huks' 21/* Key material */ 22let plainTextSize32 = new Uint8Array([ 23 0xfb, 0x8b, 0x9f, 0x12, 0xa0, 0x83, 0x19, 0xbe, 0x6a, 0x6f, 0x63, 0x2a, 0x7c, 0x86, 0xba, 0xca, 24 0x64, 0x0b, 0x88, 0x96, 0xe2, 0xfa, 0x77, 0xbc, 0x71, 0xe3, 0x0f, 0x0f, 0x9e, 0x3c, 0xe5, 0xf9 25]); 26/* 1. Set the key alias. */ 27let keyAlias = 'AES256Alias_sample'; 28/* 2. Encapsulate the key property set and key material. */ 29class propertyType { 30 tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM; 31 value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose = huks.HuksKeyAlg.HUKS_ALG_RSA; 32} 33let properties: propertyType[] = [ 34 { 35 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 36 value:huks.HuksKeyAlg.HUKS_ALG_AES 37 }, 38 { 39 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 40 value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_256 41 }, 42 { 43 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 44 value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT 45 }, 46] 47let options: huks.HuksOptions = { 48 properties: properties, 49 inData: plainTextSize32 50}; 51/* 3. Import the key in plaintext. */ 52try { 53 huks.importKeyItem(keyAlias, options, (error, data) => { 54 if (error) { 55 console.error(`callback: importKeyItem failed` + error); 56 } else { 57 console.info(`callback: importKeyItem success`); 58 } 59 }); 60} catch (error) { 61 console.error(`callback: importKeyItem input arg invalid` + error); 62} 63``` 64 65 66## Verification 67 68Use [huks.isKeyItemExist](../../reference/apis-universal-keystore-kit/js-apis-huks.md#huksiskeyitemexist9) to check whether the key exists. If the key exists, the key is successfully imported. 69 70```ts 71import huks from '@ohos.security.huks'; 72let keyAlias = 'AES256Alias_sample'; 73let isKeyExist = false; 74class keyPropertyType { 75 tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM; 76 value: huks.HuksKeyAlg = huks.HuksKeyAlg.HUKS_ALG_RSA; 77} 78let keyProperties: keyPropertyType[] = [ 79 { 80 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 81 value: huks.HuksKeyAlg.HUKS_ALG_AES 82 }, 83] 84let huksOptions: huks.HuksOptions = { 85 properties: keyProperties, // It cannot be empty. 86 inData: new Uint8Array(new Array()) // It cannot be empty. 87} 88try { 89 huks.isKeyItemExist(keyAlias, huksOptions, (error, data) => { 90 if (error) { 91 console.error(`callback: isKeyItemExist failed` + error); 92 } else { 93 if (data !== null && data.valueOf() !== null) { 94 isKeyExist = data.valueOf(); 95 console.info(`callback: isKeyItemExist success, isKeyExist = ${isKeyExist}`); 96 } 97 } 98 }); 99} catch (error) { 100 console.error(`callback: isKeyItemExist input arg invalid` + error); 101} 102``` 103