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