1# Randomly Generating an Asymmetric Key Pair 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 '@ohos.security.cryptoFramework'; 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 AlertDialog.show({ message: 'pk bin data' + pkBlob.data }); 38 AlertDialog.show({ message: 'sk bin data' + skBlob.data }); 39 }); 40 } 41 ``` 42 43## Randomly Generating an SM2 Key Pair 44 45For details about the algorithm specifications, see [SM2](crypto-asym-key-generation-conversion-spec.md#sm2). 46 471. 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. 48 492. Use [AsyKeyGenerator.generateKeyPair](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatekeypair-1) to randomly generate an asymmetric key pair (**KeyPair**). 50 51 The **KeyPair** object includes a public key (**PubKey**) and a private key (**PriKey**). 52 533. 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. 54 55- Example: Randomly generate an SM2 key pair (using promise-based APIs). 56 ```ts 57 import cryptoFramework from '@ohos.security.cryptoFramework'; 58 59 function generateSM2Key() { 60 // Create an AsyKeyGenerator instance. 61 let sm2Generator = cryptoFramework.createAsyKeyGenerator('SM2_256'); 62 // Use AsyKeyGenerator to randomly generate an asymmetric key pair. 63 let keyGenPromise = sm2Generator.generateKeyPair(); 64 keyGenPromise.then(keyPair => { 65 let pubKey = keyPair.pubKey; 66 let priKey = keyPair.priKey; 67 // Obtain the binary data of the asymmetric key pair. 68 let pkBlob = pubKey.getEncoded(); 69 let skBlob = priKey.getEncoded(); 70 AlertDialog.show({ message: 'pk bin data' + pkBlob.data }); 71 AlertDialog.show({ message: 'sk bin data' + skBlob.data }); 72 }); 73 } 74 ``` 75