1e41f4b71Sopenharmony_ci# Importing a Key in Plaintext (ArkTS)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciThis topic walks you through on how to import an AES 256-bit key and an RSA 2048-bit key. For details about the scenarios and supported algorithm specifications, see [Supported Algorithms](huks-key-import-overview.md#supported-algorithms).
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## How to Develop
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci1. Set the alias **keyAlias** of the key to import.
10e41f4b71Sopenharmony_ci   The key alias cannot exceed 128 bytes.
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci2. Encapsulate the key property set and key material.
13e41f4b71Sopenharmony_ci   - 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).
14e41f4b71Sopenharmony_ci   - 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.
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci3. Use [huks.importKeyItem](../../reference/apis-universal-keystore-kit/js-apis-huks.md#huksimportkeyitem9) to import the key.
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci```ts
19e41f4b71Sopenharmony_ci/* Import an AES 256-bit key in plaintext. This example uses callback-based APIs. */
20e41f4b71Sopenharmony_ciimport { huks } from '@kit.UniversalKeystoreKit'
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci/* Key material */
23e41f4b71Sopenharmony_cilet plainTextSize32 = new Uint8Array([
24e41f4b71Sopenharmony_ci  0xfb, 0x8b, 0x9f, 0x12, 0xa0, 0x83, 0x19, 0xbe, 0x6a, 0x6f, 0x63, 0x2a, 0x7c, 0x86, 0xba, 0xca,
25e41f4b71Sopenharmony_ci  0x64, 0x0b, 0x88, 0x96, 0xe2, 0xfa, 0x77, 0xbc, 0x71, 0xe3, 0x0f, 0x0f, 0x9e, 0x3c, 0xe5, 0xf9
26e41f4b71Sopenharmony_ci]);
27e41f4b71Sopenharmony_ci/* 1. Set the key alias. */
28e41f4b71Sopenharmony_cilet keyAlias = 'AES256Alias_sample';
29e41f4b71Sopenharmony_ci/* 2. Encapsulate the key property set and key material. */
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_cilet properties: Array<huks.HuksParam> = [
32e41f4b71Sopenharmony_ci  {
33e41f4b71Sopenharmony_ci    tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
34e41f4b71Sopenharmony_ci    value: huks.HuksKeyAlg.HUKS_ALG_AES
35e41f4b71Sopenharmony_ci  },
36e41f4b71Sopenharmony_ci  {
37e41f4b71Sopenharmony_ci    tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
38e41f4b71Sopenharmony_ci    value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_256
39e41f4b71Sopenharmony_ci  },
40e41f4b71Sopenharmony_ci  {
41e41f4b71Sopenharmony_ci    tag: huks.HuksTag.HUKS_TAG_PURPOSE,
42e41f4b71Sopenharmony_ci    value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT
43e41f4b71Sopenharmony_ci  },
44e41f4b71Sopenharmony_ci]
45e41f4b71Sopenharmony_cilet options: huks.HuksOptions = {
46e41f4b71Sopenharmony_ci  properties: properties,
47e41f4b71Sopenharmony_ci  inData: plainTextSize32
48e41f4b71Sopenharmony_ci};
49e41f4b71Sopenharmony_ci/* 3. Import the key in plaintext. */
50e41f4b71Sopenharmony_citry {
51e41f4b71Sopenharmony_ci  huks.importKeyItem(keyAlias, options, (error, data) => {
52e41f4b71Sopenharmony_ci    if (error) {
53e41f4b71Sopenharmony_ci      console.error(`callback: importKeyItem failed` + JSON.stringify(error));
54e41f4b71Sopenharmony_ci    } else {
55e41f4b71Sopenharmony_ci      console.info(`callback: importKeyItem success`);
56e41f4b71Sopenharmony_ci    }
57e41f4b71Sopenharmony_ci  });
58e41f4b71Sopenharmony_ci} catch (error) {
59e41f4b71Sopenharmony_ci  console.error(`callback: importKeyItem input arg invalid` + JSON.stringify(error));
60e41f4b71Sopenharmony_ci}
61e41f4b71Sopenharmony_ci```
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci```ts
64e41f4b71Sopenharmony_ci/* Import an RSA 2048-bit key in plaintext. This example uses callback-based APIs. */
65e41f4b71Sopenharmony_ciimport { huks } from '@kit.UniversalKeystoreKit'
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_cilet rsa2048KeyPairMaterial = new Uint8Array([
68e41f4b71Sopenharmony_ci  0x01, 0x00, 0x00, 0x00, // Key algorithm (expressed in little-endian) huks.HuksKeyAlg.HUKS_ALG_RSA = 1
69e41f4b71Sopenharmony_ci  0x00, 0x08, 0x00, 0x00, // Key size (bit): 2048
70e41f4b71Sopenharmony_ci  0x00, 0x01, 0x00, 0x00, // Length of modulus n (byte): 256
71e41f4b71Sopenharmony_ci  0x03, 0x00, 0x00, 0x00, // Length of the public key exponent e (byte): 3
72e41f4b71Sopenharmony_ci  0x00, 0x01, 0x00, 0x00, // Length of the private key exponent d (byte): 256
73e41f4b71Sopenharmony_ci  // Modulus n
74e41f4b71Sopenharmony_ci  0xc5, 0x35, 0x62, 0x48, 0xc4, 0x92, 0x87, 0x73, 0x0d, 0x42, 0x96, 0xfc, 0x7b, 0x11, 0x05, 0x06,
75e41f4b71Sopenharmony_ci  0x0f, 0x8d, 0x66, 0xc1, 0x0e, 0xad, 0x37, 0x44, 0x92, 0x95, 0x2f, 0x6a, 0x55, 0xba, 0xec, 0x1d,
76e41f4b71Sopenharmony_ci  0x54, 0x62, 0x0a, 0x4b, 0xd3, 0xc7, 0x05, 0xe4, 0x07, 0x40, 0xd9, 0xb7, 0xc2, 0x12, 0xcb, 0x9a,
77e41f4b71Sopenharmony_ci  0x90, 0xad, 0xe3, 0x24, 0xe8, 0x5e, 0xa6, 0xf8, 0xd0, 0x6e, 0xbc, 0xd1, 0x69, 0x7f, 0x6b, 0xe4,
78e41f4b71Sopenharmony_ci  0x2b, 0x4e, 0x1a, 0x65, 0xbb, 0x73, 0x88, 0x6b, 0x7c, 0xaf, 0x7e, 0xd0, 0x47, 0x26, 0xeb, 0xa5,
79e41f4b71Sopenharmony_ci  0xbe, 0xd6, 0xe8, 0xee, 0x9c, 0xa5, 0x66, 0xa5, 0xc9, 0xd3, 0x25, 0x13, 0xc4, 0x0e, 0x6c, 0xab,
80e41f4b71Sopenharmony_ci  0x50, 0xb6, 0x50, 0xc9, 0xce, 0x8f, 0x0a, 0x0b, 0xc6, 0x28, 0x69, 0xe9, 0x83, 0x69, 0xde, 0x42,
81e41f4b71Sopenharmony_ci  0x56, 0x79, 0x7f, 0xde, 0x86, 0x24, 0xca, 0xfc, 0xaa, 0xc0, 0xf3, 0xf3, 0x7f, 0x92, 0x8e, 0x8a,
82e41f4b71Sopenharmony_ci  0x12, 0x52, 0xfe, 0x50, 0xb1, 0x5e, 0x8c, 0x01, 0xce, 0xfc, 0x7e, 0xf2, 0x4f, 0x5f, 0x03, 0xfe,
83e41f4b71Sopenharmony_ci  0xa7, 0xcd, 0xa1, 0xfc, 0x94, 0x52, 0x00, 0x8b, 0x9b, 0x7f, 0x09, 0xab, 0xa8, 0xa4, 0xf5, 0xb4,
84e41f4b71Sopenharmony_ci  0xa5, 0xaa, 0xfc, 0x72, 0xeb, 0x17, 0x40, 0xa9, 0xee, 0xbe, 0x8f, 0xc2, 0xd1, 0x80, 0xc2, 0x0d,
85e41f4b71Sopenharmony_ci  0x44, 0xa9, 0x59, 0x44, 0x59, 0x81, 0x3b, 0x5d, 0x4a, 0xde, 0xfb, 0xae, 0x24, 0xfc, 0xa3, 0xd9,
86e41f4b71Sopenharmony_ci  0xbc, 0x57, 0x55, 0xc2, 0x26, 0xbc, 0x19, 0xa7, 0x9a, 0xc5, 0x59, 0xa3, 0xee, 0x5a, 0xef, 0x41,
87e41f4b71Sopenharmony_ci  0x80, 0x7d, 0xf8, 0x5e, 0xc1, 0x1d, 0x32, 0x38, 0x41, 0x5b, 0xb6, 0x92, 0xb8, 0xb7, 0x03, 0x0d,
88e41f4b71Sopenharmony_ci  0x3e, 0x59, 0x0f, 0x1c, 0xb3, 0xe1, 0x2a, 0x95, 0x1a, 0x3b, 0x50, 0x4f, 0xc4, 0x1d, 0xcf, 0x73,
89e41f4b71Sopenharmony_ci  0x7c, 0x14, 0xca, 0xe3, 0x0b, 0xa7, 0xc7, 0x1a, 0x41, 0x4a, 0xee, 0xbe, 0x1f, 0x43, 0xdd, 0xf9,
90e41f4b71Sopenharmony_ci  // Public key exponent e
91e41f4b71Sopenharmony_ci  0x01, 0x00, 0x01,
92e41f4b71Sopenharmony_ci  // Private key exponent d
93e41f4b71Sopenharmony_ci  0x88, 0x4b, 0x82, 0xe7, 0xe3, 0xe3, 0x99, 0x75, 0x6c, 0x9e, 0xaf, 0x17, 0x44, 0x3e, 0xd9, 0x07,
94e41f4b71Sopenharmony_ci  0xfd, 0x4b, 0xae, 0xce, 0x92, 0xc4, 0x28, 0x44, 0x5e, 0x42, 0x79, 0x08, 0xb6, 0xc3, 0x7f, 0x58,
95e41f4b71Sopenharmony_ci  0x2d, 0xef, 0xac, 0x4a, 0x07, 0xcd, 0xaf, 0x46, 0x8f, 0xb4, 0xc4, 0x43, 0xf9, 0xff, 0x5f, 0x74,
96e41f4b71Sopenharmony_ci  0x2d, 0xb5, 0xe0, 0x1c, 0xab, 0xf4, 0x6e, 0xd5, 0xdb, 0xc8, 0x0c, 0xfb, 0x76, 0x3c, 0x38, 0x66,
97e41f4b71Sopenharmony_ci  0xf3, 0x7f, 0x01, 0x43, 0x7a, 0x30, 0x39, 0x02, 0x80, 0xa4, 0x11, 0xb3, 0x04, 0xd9, 0xe3, 0x57,
98e41f4b71Sopenharmony_ci  0x23, 0xf4, 0x07, 0xfc, 0x91, 0x8a, 0xc6, 0xcc, 0xa2, 0x16, 0x29, 0xb3, 0xe5, 0x76, 0x4a, 0xa8,
99e41f4b71Sopenharmony_ci  0x84, 0x19, 0xdc, 0xef, 0xfc, 0xb0, 0x63, 0x33, 0x0b, 0xfa, 0xf6, 0x68, 0x0b, 0x08, 0xea, 0x31,
100e41f4b71Sopenharmony_ci  0x52, 0xee, 0x99, 0xef, 0x43, 0x2a, 0xbe, 0x97, 0xad, 0xb3, 0xb9, 0x66, 0x7a, 0xae, 0xe1, 0x8f,
101e41f4b71Sopenharmony_ci  0x57, 0x86, 0xe5, 0xfe, 0x14, 0x3c, 0x81, 0xd0, 0x64, 0xf8, 0x86, 0x1a, 0x0b, 0x40, 0x58, 0xc9,
102e41f4b71Sopenharmony_ci  0x33, 0x49, 0xb8, 0x99, 0xc6, 0x2e, 0x94, 0x70, 0xee, 0x09, 0x88, 0xe1, 0x5c, 0x4e, 0x6c, 0x22,
103e41f4b71Sopenharmony_ci  0x72, 0xa7, 0x2a, 0x21, 0xdd, 0xd7, 0x1d, 0xfc, 0x63, 0x15, 0x0b, 0xde, 0x06, 0x9c, 0xf3, 0x28,
104e41f4b71Sopenharmony_ci  0xf3, 0xac, 0x4a, 0xa8, 0xb5, 0x50, 0xca, 0x9b, 0xcc, 0x0a, 0x04, 0xfe, 0x3f, 0x98, 0x68, 0x81,
105e41f4b71Sopenharmony_ci  0xac, 0x24, 0x53, 0xea, 0x1f, 0x1c, 0x6e, 0x5e, 0xca, 0xe8, 0x31, 0x0d, 0x08, 0x12, 0xf3, 0x26,
106e41f4b71Sopenharmony_ci  0xf8, 0x5e, 0xeb, 0x10, 0x27, 0xae, 0xaa, 0xc3, 0xad, 0x6c, 0xc1, 0x89, 0xdb, 0x7d, 0x5a, 0x12,
107e41f4b71Sopenharmony_ci  0x55, 0xad, 0x11, 0x19, 0xa1, 0xa9, 0x8f, 0x0b, 0x6d, 0x78, 0x8d, 0x1c, 0xdf, 0xe5, 0x63, 0x82,
108e41f4b71Sopenharmony_ci  0x0b, 0x7d, 0x23, 0x04, 0xb4, 0x75, 0x8c, 0xed, 0x77, 0xfc, 0x1a, 0x85, 0x29, 0x11, 0xe0, 0x61,
109e41f4b71Sopenharmony_ci]);
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ci/* 1. Set the key alias. */
112e41f4b71Sopenharmony_cilet keyAlias = 'RSA_sample';
113e41f4b71Sopenharmony_ci/* 2. Encapsulate the key property set and key material. */
114e41f4b71Sopenharmony_cilet properties: Array<huks.HuksParam> = [
115e41f4b71Sopenharmony_ci  {
116e41f4b71Sopenharmony_ci    tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
117e41f4b71Sopenharmony_ci    value: huks.HuksKeyAlg.HUKS_ALG_RSA
118e41f4b71Sopenharmony_ci  },
119e41f4b71Sopenharmony_ci  {
120e41f4b71Sopenharmony_ci    tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
121e41f4b71Sopenharmony_ci    value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_2048
122e41f4b71Sopenharmony_ci  },
123e41f4b71Sopenharmony_ci  {
124e41f4b71Sopenharmony_ci    // This tag indicates the usage of the imported key, which cannot be changed after the import.
125e41f4b71Sopenharmony_ci    tag: huks.HuksTag.HUKS_TAG_PURPOSE,
126e41f4b71Sopenharmony_ci    value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT
127e41f4b71Sopenharmony_ci  },
128e41f4b71Sopenharmony_ci  {
129e41f4b71Sopenharmony_ci    // This tag indicates the type of the key to be imported.
130e41f4b71Sopenharmony_ci    tag: huks.HuksTag.HUKS_TAG_IMPORT_KEY_TYPE,
131e41f4b71Sopenharmony_ci    // The value here means to import a key parit. To import a public key, set value to HUKS_KEY_TYPE_PUBLIC_KEY.
132e41f4b71Sopenharmony_ci    value: huks.HuksImportKeyType.HUKS_KEY_TYPE_KEY_PAIR
133e41f4b71Sopenharmony_ci  },
134e41f4b71Sopenharmony_ci]
135e41f4b71Sopenharmony_cilet options: huks.HuksOptions = {
136e41f4b71Sopenharmony_ci  properties: properties,
137e41f4b71Sopenharmony_ci  inData: rsa2048KeyPairMaterial
138e41f4b71Sopenharmony_ci};
139e41f4b71Sopenharmony_ci/* 3. Import the key in plaintext. */
140e41f4b71Sopenharmony_citry {
141e41f4b71Sopenharmony_ci  huks.importKeyItem(keyAlias, options, (error, data) => {
142e41f4b71Sopenharmony_ci    if (error) {
143e41f4b71Sopenharmony_ci      console.error(`callback: importKeyItem failed` + error);
144e41f4b71Sopenharmony_ci    } else {
145e41f4b71Sopenharmony_ci      console.info(`callback: importKeyItem success`);
146e41f4b71Sopenharmony_ci    }
147e41f4b71Sopenharmony_ci  });
148e41f4b71Sopenharmony_ci} catch (error) {
149e41f4b71Sopenharmony_ci  console.error(`callback: importKeyItem input arg invalid` + error);
150e41f4b71Sopenharmony_ci}
151e41f4b71Sopenharmony_ci```
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ci## Verification
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ciUse [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.
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci```ts
159e41f4b71Sopenharmony_ciimport { huks } from '@kit.UniversalKeystoreKit';
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_cilet keyAlias = 'AES256Alias_sample';
162e41f4b71Sopenharmony_cilet isKeyExist = false;
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_cilet keyProperties: Array<huks.HuksParam> = [
165e41f4b71Sopenharmony_ci  {
166e41f4b71Sopenharmony_ci    tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
167e41f4b71Sopenharmony_ci    value: huks.HuksKeyAlg.HUKS_ALG_AES
168e41f4b71Sopenharmony_ci  }
169e41f4b71Sopenharmony_ci]
170e41f4b71Sopenharmony_cilet huksOptions: huks.HuksOptions = {
171e41f4b71Sopenharmony_ci  properties: keyProperties, // It cannot be empty.
172e41f4b71Sopenharmony_ci  inData: new Uint8Array(new Array()) // It cannot be empty.
173e41f4b71Sopenharmony_ci}
174e41f4b71Sopenharmony_citry {
175e41f4b71Sopenharmony_ci  huks.isKeyItemExist(keyAlias, huksOptions, (error, data) => {
176e41f4b71Sopenharmony_ci    if (error) {
177e41f4b71Sopenharmony_ci      console.error(`callback: isKeyItemExist failed, ` + JSON.stringify(error));
178e41f4b71Sopenharmony_ci    } else {
179e41f4b71Sopenharmony_ci      if (data !== null && data.valueOf() !== null) {
180e41f4b71Sopenharmony_ci        isKeyExist = data.valueOf();
181e41f4b71Sopenharmony_ci        console.info(`callback: isKeyItemExist success, isKeyExist = ${isKeyExist}`);
182e41f4b71Sopenharmony_ci      }
183e41f4b71Sopenharmony_ci    }
184e41f4b71Sopenharmony_ci  });
185e41f4b71Sopenharmony_ci} catch (error) {
186e41f4b71Sopenharmony_ci  console.error(`callback: isKeyItemExist input arg invalid, ` + JSON.stringify(error));
187e41f4b71Sopenharmony_ci}
188e41f4b71Sopenharmony_ci```
189