• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Generating an Asymmetric Key Pair Based on Key Parameters (ArkTS)
2
3<!--Kit: Crypto Architecture Kit-->
4<!--Subsystem: Security-->
5<!--Owner: @zxz--3-->
6<!--Designer: @lanming-->
7<!--Tester: @PAFT-->
8<!--Adviser: @zengyawen-->
9
10This topic walks you through on how to generate an RSA, an ECC, and an SM2 asymmetric key pair (**KeyPair**) based on the specified key parameters and obtain the key parameter properties.
11
12The **KeyPair** object created can be used for subsequent operations, such as encryption and decryption. The obtained key parameter properties can be used for key storage and transfer.
13
14## Generating an RSA Public Key Based on Key Parameters
15
16For details about the algorithm specifications, see [RSA](crypto-asym-key-generation-conversion-spec.md#rsa).
17
181. Create an [RSACommonParamsSpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#rsacommonparamsspec10) object to specify the common parameter (**n**) of both the public and private keys of the RSA algorithm.
19
20   **RSACommonParamsSpec** is a child class of **AsyKeySpec**. Specify the RSA algorithm in the **algName** parameter, and set the key parameter type to **AsyKeySpecType.COMMON_PARAMS_SPEC**, which indicates the common parameter for both the public and private keys.
21
22   When key parameters are specified for generating a key, the bigint value must be a positive number in big-endian format.
23
242. Create an [RSAPubKeySpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#rsapubkeyspec10) object to specify the parameters (**n**, **pk**) contained in the public key of the RSA algorithm.
25
26   **RSAPubKeySpec** is a child class of **AsyKeySpec**. Specify the RSA algorithm in the **algName** parameter, and set the key parameter type to **AsyKeySpecType.PUBLIC_KEY_SPEC**, which indicates the parameters of the public key.
27
283. Call [cryptoFramework.createAsyKeyGeneratorBySpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreateasykeygeneratorbyspec10) with the **RSAPubKeySpec** object to create an asymmetric key generator (**AsyKeyGeneratorBySpec**) object.
29
304. Call [AsyKeyGeneratorBySpec.generatePubKey](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatepubkey10) to generate the public key (**PubKey**).
31
325. Call [PubKey.getAsyKeySpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getasykeyspec10) to obtain the modulus **n** and the public key exponent **pk** (expressed as e in the formula).
33
34- Example: Generate an RSA public key based on key parameters (using callback-based APIs).
35  ```ts
36  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
37  // Generate an RSA public key parameter (RsaPubKeySpec).
38  function genRsaPubKeySpec(nIn: bigint, eIn: bigint): cryptoFramework.RSAPubKeySpec {
39    let rsaCommSpec: cryptoFramework.RSACommonParamsSpec = {
40      n: nIn,
41      algName: 'RSA',
42      specType: cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC
43    };
44    let rsaPubKeySpec: cryptoFramework.RSAPubKeySpec = {
45      params: rsaCommSpec,
46      pk: eIn,
47      algName: 'RSA',
48      specType: cryptoFramework.AsyKeySpecType.PUBLIC_KEY_SPEC
49    };
50    return rsaPubKeySpec;
51  }
52  // Construct an RSA public key specification object based on the key parameter.
53  function genRsa2048PubKeySpec() {
54    let nIn = BigInt('0x9260d0750ae117eee55c3f3deaba74917521a262ee76007cdf8a56755ad73a1598a1408410a01434c3f5bc54a88b57fa19fc4328daea0750a4c44e88cff3b2382621b80f670464433e4336e6d003e8cd65bff211da144b88291c2259a00a72b711c116ef7686e8fee34e4d933c868187bdc26f7be071493c86f7a5941c3510806ad67b0f94d88f5cf5c02a092821d8626e8932b65c5bd8c92049c210932b7afa7ac59c0e886ae5c1edb00d8ce2c57633db26bd6639bff73cee82be9275c402b4cf2a4388da8cf8c64eefe1c5a0f5ab8057c39fa5c0589c3e253f0960332300f94bea44877b588e1edbde97cf2360727a09b775262d7ee552b3319b9266f05a25');
55    let eIn = BigInt('0x010001');
56    return genRsaPubKeySpec(nIn, eIn);
57  }
58  // Compare the RSA public key specifications with the expected value.
59  function compareRsaPubKeyBySpec(rsaKeySpec: cryptoFramework.RSAPubKeySpec, n: bigint | string | number, e: bigint | string | number) {
60    if (typeof n === 'string' || typeof e === 'string') {
61      console.error('type is string');
62      return false;
63    }
64    if (typeof n === 'number' || typeof e === 'number') {
65      console.error('type is number');
66      return false;
67    }
68    if (rsaKeySpec.params.n !== n) {
69      return false;
70    }
71    if (rsaKeySpec.pk !== e) {
72      return false;
73    }
74    return true;
75  }
76  // Generate an RSA public key based on the RSA public key specifications, obtain the key specifications, and compare it with the expected value.
77  function rsaUsePubKeySpecGetCallback() {
78    let rsaPubKeySpec = genRsa2048PubKeySpec();
79    let rsaGeneratorSpec = cryptoFramework.createAsyKeyGeneratorBySpec(rsaPubKeySpec);
80    rsaGeneratorSpec.generatePubKey((error, key) => {
81      if (error) {
82        console.error('generate pubKey error' + 'error code: ' + error.code + 'error message' + error.message);
83      }
84      let pubKey = key;
85      let nBN = pubKey.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.RSA_N_BN);
86      let eBN = pubKey.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.RSA_PK_BN);
87      if (compareRsaPubKeyBySpec(rsaPubKeySpec, nBN, eBN) !== true) {
88        console.error('error pub key big number');
89      } else {
90        console.info('n, e in the pubKey are same as the spec.');
91      }
92    });
93  }
94  ```
95
96- Synchronously return the result ([generatePubKeySync](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatepubkeysync12)):
97  ```ts
98  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
99  // Generate an RSA public key parameter (RsaPubKeySpec).
100  function genRsaPubKeySpec(nIn: bigint, eIn: bigint): cryptoFramework.RSAPubKeySpec {
101    let rsaCommSpec: cryptoFramework.RSACommonParamsSpec = {
102      n: nIn,
103      algName: 'RSA',
104      specType: cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC
105    };
106    let rsaPubKeySpec: cryptoFramework.RSAPubKeySpec = {
107      params: rsaCommSpec,
108      pk: eIn,
109      algName: 'RSA',
110      specType: cryptoFramework.AsyKeySpecType.PUBLIC_KEY_SPEC
111    };
112    return rsaPubKeySpec;
113  }
114  // Construct an RSA public key specification object based on the key parameter.
115  function genRsa2048PubKeySpec() {
116    let nIn = BigInt('0x9260d0750ae117eee55c3f3deaba74917521a262ee76007cdf8a56755ad73a1598a1408410a01434c3f5bc54a88b57fa19fc4328daea0750a4c44e88cff3b2382621b80f670464433e4336e6d003e8cd65bff211da144b88291c2259a00a72b711c116ef7686e8fee34e4d933c868187bdc26f7be071493c86f7a5941c3510806ad67b0f94d88f5cf5c02a092821d8626e8932b65c5bd8c92049c210932b7afa7ac59c0e886ae5c1edb00d8ce2c57633db26bd6639bff73cee82be9275c402b4cf2a4388da8cf8c64eefe1c5a0f5ab8057c39fa5c0589c3e253f0960332300f94bea44877b588e1edbde97cf2360727a09b775262d7ee552b3319b9266f05a25');
117    let eIn = BigInt('0x010001');
118    return genRsaPubKeySpec(nIn, eIn);
119  }
120  // Compare the RSA public key specifications with the expected value.
121  function compareRsaPubKeyBySpec(rsaKeySpec: cryptoFramework.RSAPubKeySpec, n: bigint | string | number, e: bigint | string | number) {
122    if (typeof n === 'string' || typeof e === 'string') {
123      console.error('type is string');
124      return false;
125    }
126    if (typeof n === 'number' || typeof e === 'number') {
127      console.error('type is number');
128      return false;
129    }
130    if (rsaKeySpec.params.n !== n) {
131      return false;
132    }
133    if (rsaKeySpec.pk !== e) {
134      return false;
135    }
136    return true;
137  }
138  // Generate an RSA public key based on the RSA public key specifications, obtain the key specifications, and compare it with the expected value.
139  function rsaUsePubKeySpecGetSync() {
140    let rsaPubKeySpec = genRsa2048PubKeySpec();
141    let rsaGeneratorSpec = cryptoFramework.createAsyKeyGeneratorBySpec(rsaPubKeySpec);
142    try {
143      let pubKey = rsaGeneratorSpec.generatePubKeySync();
144      if (pubKey !== null) {
145        let nBN = pubKey.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.RSA_N_BN);
146        let eBN = pubKey.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.RSA_PK_BN);
147        if (compareRsaPubKeyBySpec(rsaPubKeySpec, nBN, eBN) !== true) {
148          console.error('error pub key big number');
149        } else {
150          console.info('n, e in the pubKey are same as the spec.');
151        }
152      } else {
153        console.error('get pub key result fail!');
154      }
155    } catch (e) {
156      console.error(`get pub key result fail, ${e.code}, ${e.message}`);
157    }
158  }
159  ```
160
161## Generating an ECC Key Pair Based on Key Parameters
162
163For details about the algorithm specifications, see [ECC](crypto-asym-key-generation-conversion-spec.md#ecc).
164
1651. Create an [ECCCommonParamsSpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#ecccommonparamsspec10) object to specify the common parameters of both the public and private keys of the ECC algorithm.
166   **ECCCommonParamsSpec** is a child class of **AsyKeySpec**. Specify the ECC algorithm in the **algName** parameter, and set the key parameter type to **AsyKeySpecType.COMMON_PARAMS_SPEC**, which indicates the common parameter for both the public and private keys.
167
168   When key parameters are specified for generating a key, the bigint value must be a positive number in big-endian format.
169
1702. Call [cryptoFramework.createAsyKeyGeneratorBySpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreateasykeygeneratorbyspec10) with the **ECCCommonParamsSpec** object to create an asymmetric key generator (**AsyKeyGeneratorBySpec**) object.
171
1723. Call [AsyKeyGeneratorBySpec.generateKeyPair](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatekeypair10) to generate a key pair (**KeyPair**).
173
1744. Call [PriKey.getAsyKeySpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getasykeyspec10-1) to obtain the private key specifications, and call [PubKey.getAsyKeySpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getasykeyspec10) to obtain the public key specifications of the ECC.
175
176- Example: Generate an ECC key pair based on key parameters (using promise-based APIs).
177  ```ts
178  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
179  import { BusinessError } from '@kit.BasicServicesKit';
180
181  // Print bigint information.
182  function showBigIntInfo(bnName: string, bnValue: bigint | string | number) {
183    if (typeof bnValue === 'string') {
184      console.error('type is string');
185      return;
186    }
187    if (typeof bnValue === 'number') {
188      console.error('type is number');
189      return;
190    }
191    console.info(bnName + ':');
192    console.info('. Decimal: ' + bnValue.toString());
193    console.info('. Hexadecimal: ' + bnValue.toString(16));
194    console.info('. Length (bits): ' + bnValue.toString(2).length);
195  }
196  // Construct the EccCommonSpec struct, which defines the common parameters of the ECC public and private keys.
197  function genEccCommonSpec(): cryptoFramework.ECCCommonParamsSpec {
198    let fieldFp: cryptoFramework.ECFieldFp = {
199      fieldType: 'Fp',
200      p: BigInt('0xffffffffffffffffffffffffffffffff000000000000000000000001')
201    }
202    let G: cryptoFramework.Point = {
203      x: BigInt('0xb70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21'),
204      y: BigInt('0xbd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34')
205    }
206    let eccCommonSpec: cryptoFramework.ECCCommonParamsSpec = {
207      algName: 'ECC',
208      specType: cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC,
209      field: fieldFp,
210      a: BigInt('0xfffffffffffffffffffffffffffffffefffffffffffffffffffffffe'),
211      b: BigInt('0xb4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4'),
212      g: G,
213      n: BigInt('0xffffffffffffffffffffffffffff16a2e0b8f03e13dd29455c5c2a3d'),
214      h: 1
215    }
216    return eccCommonSpec;
217  }
218  // Print the ECC key specifications.
219  function showEccSpecDetailInfo(key: cryptoFramework.PubKey | cryptoFramework.PriKey, keyType: string) {
220    console.info('show detail of ' + keyType + ':');
221    try {
222      let p = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_FP_P_BN);
223      showBigIntInfo('--- p', p); // length is 224, hex : ffffffffffffffffffffffffffffffff000000000000000000000001
224      let a = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_A_BN);
225      showBigIntInfo('--- a', a); // length is 224, hex : fffffffffffffffffffffffffffffffefffffffffffffffffffffffe
226      let b = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_B_BN);
227      showBigIntInfo('--- b', b); // length is 224, hex : b4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4
228      let gX = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_G_X_BN);
229      showBigIntInfo('--- gX', gX); // length is 224, hex : b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21
230      let gY = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_G_Y_BN);
231      showBigIntInfo('--- gY', gY); // length is 224, hex : bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34
232      let n = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_N_BN);
233      showBigIntInfo('--- n', n); // length is 224, hex : ffffffffffffffffffffffffffff16a2e0b8f03e13dd29455c5c2a3d
234      let h = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_H_NUM);
235      console.warn('--- h: ' + h); // key h: 1
236      let fieldType = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_FIELD_TYPE_STR);
237      console.warn('--- field type: ' + fieldType); // key field type: Fp
238      let fieldSize = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_FIELD_SIZE_NUM);
239      console.warn('--- field size: ' + fieldSize); // key field size: 224
240      let curveName = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_CURVE_NAME_STR);
241      console.warn('--- curve name: ' + curveName); // key curve name: NID_secp224r1
242      if (keyType === 'priKey') {
243        let sk = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_SK_BN);
244        showBigIntInfo('--- sk', sk);
245      } else if (keyType === 'pubKey') {
246        let pkX = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_PK_X_BN);
247        showBigIntInfo('--- pkX', pkX);
248        let pkY = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_PK_Y_BN);
249        showBigIntInfo('--- pkY', pkY);
250      }
251    } catch (error) {
252      console.error('getAsyKeySpec error');
253      let e: BusinessError = error as BusinessError;
254      console.error(`getAsyKeySpec failed, ${e.code}, ${e.message}`);
255    }
256  }
257  // Generate an ECC key pair based on the EccCommonSpec instance and obtain the key specifications.
258  function testEccUseCommKeySpecGet() {
259    try {
260      let commKeySpec = genEccCommonSpec(); // Construct the EccCommonSpec object.
261      let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(commKeySpec); // Create an AsyKeyGenerator instance based on the EccCommonSpec object.
262      let keyPairPromise = generatorBySpec.generateKeyPair(); // Generates an ECC key pair.
263      keyPairPromise.then(keyPair => {// Use AsyKeyGenerator to create an ECC key pair.
264        showEccSpecDetailInfo(keyPair.priKey, "priKey"); // Obtain the ECC private key specifications.
265        showEccSpecDetailInfo(keyPair.pubKey, "pubKey"); // Obtain the ECC public key specifications.
266      }).catch((error: BusinessError) => {
267        // Capture exceptions such as logic errors asynchronously.
268        console.error('generateComm error');
269        console.error('error code: ' + error.code + ', message is: ' + error.message);
270      })
271    } catch (error) {
272      // Capture parameter errors synchronously.
273      console.error('testEccUseCommSpec error');
274      let e: BusinessError = error as BusinessError;
275      console.error(`ecc comm spec failed, ${e.code}, ${e.message}`);
276    }
277  }
278  ```
279
280- Synchronously return the result ([generateKeyPairSync](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatekeypairsync12)):
281  ```ts
282  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
283
284  function showBigIntInfo(bnName: string, bnValue: bigint | string | number) {
285    if (typeof bnValue === 'string') {
286      console.error('type is string');
287      return;
288    }
289    if (typeof bnValue === 'number') {
290      console.error('type is number');
291      return;
292    }
293    console.info(bnName + ':');
294    console.info('. Decimal: ' + bnValue.toString());
295    console.info('. Hexadecimal: ' + bnValue.toString(16));
296    console.info('. Length (bits): ' + bnValue.toString(2).length);
297  }
298  // Construct the EccCommonSpec struct, which defines the common parameters of the ECC public and private keys.
299  function genEccCommonSpec(): cryptoFramework.ECCCommonParamsSpec {
300    let fieldFp: cryptoFramework.ECFieldFp = {
301      fieldType: 'Fp',
302      p: BigInt('0xffffffffffffffffffffffffffffffff000000000000000000000001')
303    }
304    let G: cryptoFramework.Point = {
305      x: BigInt('0xb70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21'),
306      y: BigInt('0xbd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34')
307    }
308    let eccCommonSpec: cryptoFramework.ECCCommonParamsSpec = {
309      algName: 'ECC',
310      specType: cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC,
311      field: fieldFp,
312      a: BigInt('0xfffffffffffffffffffffffffffffffefffffffffffffffffffffffe'),
313      b: BigInt('0xb4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4'),
314      g: G,
315      n: BigInt('0xffffffffffffffffffffffffffff16a2e0b8f03e13dd29455c5c2a3d'),
316      h: 1
317    }
318    return eccCommonSpec;
319  }
320  // Print the ECC key specifications.
321  function showEccSpecDetailInfo(key: cryptoFramework.PubKey | cryptoFramework.PriKey, keyType: string) {
322    console.info('show detail of ' + keyType + ':');
323    try {
324      let p = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_FP_P_BN);
325      showBigIntInfo('--- p', p); // length is 224, hex : ffffffffffffffffffffffffffffffff000000000000000000000001
326      let a = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_A_BN);
327      showBigIntInfo('--- a', a); // length is 224, hex : fffffffffffffffffffffffffffffffefffffffffffffffffffffffe
328      let b = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_B_BN);
329      showBigIntInfo('--- b', b); // length is 224, hex : b4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4
330      let gX = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_G_X_BN);
331      showBigIntInfo('--- gX', gX); // length is 224, hex : b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21
332      let gY = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_G_Y_BN);
333      showBigIntInfo('--- gY', gY); // length is 224, hex : bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34
334      let n = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_N_BN);
335      showBigIntInfo('--- n', n); // length is 224, hex : ffffffffffffffffffffffffffff16a2e0b8f03e13dd29455c5c2a3d
336      let h = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_H_NUM);
337      console.warn('--- h: ' + h); // key h: 1
338      let fieldType = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_FIELD_TYPE_STR);
339      console.warn('--- field type: ' + fieldType); // key field type: Fp
340      let fieldSize = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_FIELD_SIZE_NUM);
341      console.warn('--- field size: ' + fieldSize); // key field size: 224
342      let curveName = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_CURVE_NAME_STR);
343      console.warn('--- curve name: ' + curveName); // key curve name: NID_secp224r1
344      if (keyType === 'priKey') {
345        let sk = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_SK_BN);
346        showBigIntInfo('--- sk', sk);
347      } else if (keyType === 'pubKey') {
348        let pkX = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_PK_X_BN);
349        showBigIntInfo('--- pkX', pkX);
350        let pkY = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_PK_Y_BN);
351        showBigIntInfo('--- pkY', pkY);
352      }
353    } catch (e) {
354      console.error(`getAsyKeySpec failed, ${e.code}, ${e.message}`);
355    }
356  }
357  // Generate an ECC key pair based on the EccCommonSpec instance and obtain the key specifications.
358  function testEccUseCommKeySpecGetSync() {
359    try {
360      let commKeySpec = genEccCommonSpec(); // Construct the EccCommonSpec object.
361      let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(commKeySpec); // Create an AsyKeyGenerator instance based on the EccCommonSpec object.
362      let keyPair = generatorBySpec.generateKeyPairSync(); // Generates an ECC key pair.
363      if (keyPair !== null) {
364        showEccSpecDetailInfo(keyPair.priKey, "priKey"); // Obtain the ECC private key specifications.
365        showEccSpecDetailInfo(keyPair.pubKey, "pubKey"); // Obtain the ECC public key specifications.
366      } else {
367        console.error('get key pair result fail!');
368      }
369    } catch (e) {
370      // Capture exceptions such as logic errors here.
371      console.error(`get key pair result fail, ${e.code}, ${e.message}`);
372    }
373  }
374  ```
375
376
377## Generating an SM2 Key Pair Based on the Elliptic Curve Name
378
379For details about the algorithm specifications, see [SM2](crypto-asym-key-generation-conversion-spec.md#sm2).
380
3811. Create an [ECCCommonParamsSpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#ecccommonparamsspec10) object to specify common parameters of the private and public keys. Call [genECCCommonParamsSpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#genecccommonparamsspec11) with an NID string to generate the common parameters for the SM2 key pair.
382
383    When key parameters are specified for generating a key, the bigint value must be a positive number in big-endian format.
384
3852. Create an [ECCKeyPairSpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#ecckeypairspec10) object with **algName** set to **SM2** to specify the SM2 key pair parameters.
386
3873. Call [cryptoFramework.createAsyKeyGeneratorBySpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreateasykeygeneratorbyspec10) with the **ECCKeyPairSpec** object to create an asymmetric key generator object.
388
3894. Call [AsyKeyGeneratorBySpec.generateKeyPair](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatekeypair10) to generate an SM2 key pair (**KeyPair**).
390
3915. Call [PriKey.getAsyKeySpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getasykeyspec10-1) to obtain elliptic curve parameters of SM2.
392
393- Example: Generate an SM2 key based on the elliptic curve name (using promise-based APIs)
394  ```ts
395  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
396
397  function genSM2KeyPairSpec() {
398    let sm2CommonParamsSpec = cryptoFramework.ECCKeyUtil.genECCCommonParamsSpec('NID_sm2');
399    let sm2KeyPairSpec: cryptoFramework.ECCKeyPairSpec = {
400      algName: "SM2",
401      specType: cryptoFramework.AsyKeySpecType.KEY_PAIR_SPEC,
402      params: sm2CommonParamsSpec,
403      sk: BigInt('0x6330B599ECD23ABDC74B9A5B7B5E00E553005F72743101C5FAB83AEB579B7074'),
404      pk: {
405        x: BigInt('0x67F3B850BDC0BA5D3A29D8A0883C4B17612AB84F87F18E28F77D824A115C02C4'),
406        y: BigInt('0xD48966CE754BBBEDD6501A1385E1B205C186E926ADED44287145E8897D4B2071')
407      },
408    };
409    return sm2KeyPairSpec;
410  }
411
412  async function sm2Test() {
413    let sm2KeyPairSpec = genSM2KeyPairSpec();
414    let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(sm2KeyPairSpec);
415    let keyPair = await generatorBySpec.generateKeyPair();
416    let sm2CurveName = keyPair.priKey.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_CURVE_NAME_STR);
417    console.info('ECC_CURVE_NAME_STR: ' + sm2CurveName); // NID_sm2
418  }
419  ```
420
421- Synchronously return the result ([generateKeyPairSync](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatekeypairsync12)):
422  ```ts
423  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
424
425  function genSM2KeyPairSpec() {
426    let sm2CommonParamsSpec = cryptoFramework.ECCKeyUtil.genECCCommonParamsSpec('NID_sm2');
427    let sm2KeyPairSpec: cryptoFramework.ECCKeyPairSpec = {
428      algName: "SM2",
429      specType: cryptoFramework.AsyKeySpecType.KEY_PAIR_SPEC,
430      params: sm2CommonParamsSpec,
431      sk: BigInt('0x6330B599ECD23ABDC74B9A5B7B5E00E553005F72743101C5FAB83AEB579B7074'),
432      pk: {
433        x: BigInt('0x67F3B850BDC0BA5D3A29D8A0883C4B17612AB84F87F18E28F77D824A115C02C4'),
434        y: BigInt('0xD48966CE754BBBEDD6501A1385E1B205C186E926ADED44287145E8897D4B2071')
435      },
436    };
437    return sm2KeyPairSpec;
438  }
439  function sm2TestSync() {
440    let sm2KeyPairSpec = genSM2KeyPairSpec();
441    let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(sm2KeyPairSpec);
442    try {
443      let keyPair = generatorBySpec.generateKeyPairSync();
444      if (keyPair !== null) {
445        let sm2CurveName = keyPair.priKey.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_CURVE_NAME_STR);
446        console.info('ECC_CURVE_NAME_STR: ' + sm2CurveName); // NID_sm2
447      } else {
448        console.error('get key pair result fail!');
449      }
450    } catch (e) {
451      console.error(`get key pair result fail, ${e.code}, ${e.message}`);
452    }
453  }
454  ```
455