• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Randomly Generating an Asymmetric Key Pair (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 uses RSA and SM2 as an example to describe how to generate an asymmetric key pair (**KeyPair**) and obtain the binary data.
11
12The **KeyPair** object created can be used for subsequent encryption and decryption operations, and the binary data can be used for key storage and transfer.
13
14## Randomly Generating an RSA Key Pair
15
16For details about the algorithm specifications, see [RSA](crypto-asym-key-generation-conversion-spec.md#rsa).
17
181. Call [cryptoFramework.createAsyKeyGenerator](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreateasykeygenerator) with the string parameter **'RSA1024|PRIMES_2'** to create an asymmetric key generator (**AsyKeyGenerator**) object for a 1024-bit RSA key with two primes.
19
202. Call [AsyKeyGenerator.generateKeyPair](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatekeypair-1) to randomly generate an asymmetric key pair (**KeyPair**).
21
22   The **KeyPair** object includes a public key (**PubKey**) and a private key (**PriKey**).
23
243. Call [PubKey.getEncoded](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getencoded) to obtain the binary data of the public key, and call [PriKey.getEncoded](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getencoded) to obtain the binary data of the private key.
25
26- Example: Randomly generate an RSA key pair (using promise-based APIs).
27  ```ts
28  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
29
30  function generateAsyKey() {
31    // Create an AsyKeyGenerator instance.
32    let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024|PRIMES_2');
33    // Use AsyKeyGenerator to randomly generate an asymmetric key pair.
34    let keyGenPromise = rsaGenerator.generateKeyPair();
35    keyGenPromise.then(keyPair => {
36      let pubKey = keyPair.pubKey;
37      let priKey = keyPair.priKey;
38      // Obtain the binary data of the asymmetric key pair.
39      let pkBlob = pubKey.getEncoded();
40      let skBlob = priKey.getEncoded();
41      console.info('pk bin data' + pkBlob.data);
42      console.info('sk bin data' + skBlob.data);
43    });
44  }
45  ```
46
47- Example: Randomly generate an RSA key pair (using the synchronous API [generateKeyPairSync](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatekeypairsync12)).
48  ```ts
49  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
50
51  function generateAsyKeySync() {
52    // Create an AsyKeyGenerator instance.
53    let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024|PRIMES_2');
54    // Use AsyKeyGenerator to randomly generate an asymmetric key pair.
55    try {
56      let keyPair = rsaGenerator.generateKeyPairSync();
57      if (keyPair !== null) {
58        let pubKey = keyPair.pubKey;
59        let priKey = keyPair.priKey;
60        // Obtain the binary data of the asymmetric key pair.
61        let pkBlob = pubKey.getEncoded();
62        let skBlob = priKey.getEncoded();
63        console.info('pk bin data' + pkBlob.data);
64        console.info('sk bin data' + skBlob.data);
65      } else {
66        console.error("[Sync]: get key pair result fail!");
67      }
68    } catch (e) {
69      console.error(`get key pair failed, ${e.code}, ${e.message}`);
70    }
71  }
72  ```
73
74## Randomly Generating an SM2 Key Pair
75
76For details about the algorithm specifications, see [SM2](crypto-asym-key-generation-conversion-spec.md#sm2).
77
781. Call [cryptoFramework.createAsyKeyGenerator](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreateasykeygenerator) with the string parameter **'SM2_256'** to create an asymmetric key generator (**AsyKeyGenerator**) object for a 256-bit SM2 key pair.
79
802. Call [AsyKeyGenerator.generateKeyPair](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatekeypair-1) to randomly generate an asymmetric key pair (**KeyPair**).
81
82   The **KeyPair** object includes a public key (**PubKey**) and a private key (**PriKey**).
83
843. Call [PubKey.getEncoded](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getencoded) to obtain the binary data of the public key, and call [PriKey.getEncoded](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getencoded) to obtain the binary data of the private key.
85
86- Example: Randomly generate an SM2 key pair (using promise-based APIs).
87  ```ts
88  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
89
90  function generateSM2Key() {
91    // Create an AsyKeyGenerator instance.
92    let sm2Generator = cryptoFramework.createAsyKeyGenerator('SM2_256');
93    // Use AsyKeyGenerator to randomly generate an asymmetric key pair.
94    let keyGenPromise = sm2Generator.generateKeyPair();
95    keyGenPromise.then(keyPair => {
96      let pubKey = keyPair.pubKey;
97      let priKey = keyPair.priKey;
98      // Obtain the binary data of the asymmetric key pair.
99      let pkBlob = pubKey.getEncoded();
100      let skBlob = priKey.getEncoded();
101      console.info('pk bin data' + pkBlob.data);
102      console.info('sk bin data' + skBlob.data);
103    });
104  }
105  ```
106
107- Example: Randomly generate an RSA key pair (using the synchronous API [generateKeyPairSync](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatekeypairsync12)).
108  ```ts
109  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
110
111  function generateSM2KeySync() {
112    // Create an AsyKeyGenerator instance.
113    let rsaGenerator = cryptoFramework.createAsyKeyGenerator('SM2_256');
114    // Use AsyKeyGenerator to randomly generate an asymmetric key pair.
115    try {
116      let keyPair = rsaGenerator.generateKeyPairSync();
117      if (keyPair !== null) {
118        let pubKey = keyPair.pubKey;
119        let priKey = keyPair.priKey;
120        // Obtain the binary data of the asymmetric key pair.
121        let pkBlob = pubKey.getEncoded();
122        let skBlob = priKey.getEncoded();
123        console.info('pk bin data' + pkBlob.data);
124        console.info('sk bin data' + skBlob.data);
125      } else {
126        console.error("[Sync]: get key pair result fail!");
127      }
128    } catch (e) {
129      console.error(`get key pair failed, ${e.code}, ${e.message}`);
130    }
131  }
132  ```
133