1# Converting Binary Data into 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, ECC, and SM2 as an example to describe how to convert binary data into an asymmetric key pair (**KeyPair**). That is, convert a piece of external or internal binary data into a **KeyPair** object for subsequent operations, such as encryption and decryption. 11 12> **NOTE** 13> 14> The asymmetric key conversion must comply with the following requirements: 15> 16> - The public key must use the ASN.1 syntax and DER encoding format and comply with X.509 specifications. 17> 18> - The private key must use the ASN.1 syntax and DER encoding format and comply with PKCS\#8 specifications. 19 20## Converting Binary Data into an RSA Key Pair 21 22For details about the algorithm specifications, see [RSA](crypto-asym-key-generation-conversion-spec.md#rsa). 23 241. Obtain the binary data of the RSA public key or private key and encapsulate the data into a **DataBlob** object. 25 26 The public key and private key can be passed separately. In this example, the public key is passed. 27 282. Call [cryptoFramework.createAsyKeyGenerator](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreateasykeygenerator) with the string parameter **'RSA1024'** to create an asymmetric key generator (**AsyKeyGenerator**) object for a 1024-bit RSA key with two primes. 29 30 The default number of primes for creating an RSA asymmetric key is **2**. The **PRIMES_2** parameter is omitted in the string parameter here. 31 323. Call [AsyKeyGenerator.convertKey](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkey-3) to convert the binary data into an asymmetric key pair (**KeyPair**). That is, convert a piece of external or internal binary data into a crypto key object for subsequent operations, such as encryption and decryption. 33 34- Example: Convert binary data into an RSA key pair (using callback-based APIs). 35 ```ts 36 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 37 38 function convertAsyKey() { 39 let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); 40 let pkVal = new Uint8Array([48, 129, 159, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 141, 0, 48, 129, 137, 2, 129, 129, 0, 174, 203, 113, 83, 113, 3, 143, 213, 194, 79, 91, 9, 51, 142, 87, 45, 97, 65, 136, 24, 166, 35, 5, 179, 42, 47, 212, 79, 111, 74, 134, 120, 73, 67, 21, 19, 235, 80, 46, 152, 209, 133, 232, 87, 192, 140, 18, 206, 27, 106, 106, 169, 106, 46, 135, 111, 118, 32, 129, 27, 89, 255, 183, 116, 247, 38, 12, 7, 238, 77, 151, 167, 6, 102, 153, 126, 66, 28, 253, 253, 216, 64, 20, 138, 117, 72, 15, 216, 178, 37, 208, 179, 63, 204, 39, 94, 244, 170, 48, 190, 21, 11, 73, 169, 156, 104, 193, 3, 17, 100, 28, 60, 50, 92, 235, 218, 57, 73, 119, 19, 101, 164, 192, 161, 197, 106, 105, 73, 2, 3, 1, 0, 1]); 41 let pkBlob: cryptoFramework.DataBlob = { data: pkVal }; 42 rsaGenerator.convertKey(pkBlob, null, (err, keyPair) => { 43 if (err) { 44 console.error(`convertKey failed, ${err.code}, ${err.message}`); 45 return; 46 } 47 console.info('convertKey success'); 48 }); 49 } 50 ``` 51 52- Example: Convert binary data into a key pair (using the synchronous API [convertKeySync](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkeysync12)). 53 ```ts 54 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 55 56 function convertAsyKeySync() { 57 let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); 58 let pkVal = new Uint8Array([48, 129, 159, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 141, 0, 48, 129, 137, 2, 129, 129, 0, 174, 203, 113, 83, 113, 3, 143, 213, 194, 79, 91, 9, 51, 142, 87, 45, 97, 65, 136, 24, 166, 35, 5, 179, 42, 47, 212, 79, 111, 74, 134, 120, 73, 67, 21, 19, 235, 80, 46, 152, 209, 133, 232, 87, 192, 140, 18, 206, 27, 106, 106, 169, 106, 46, 135, 111, 118, 32, 129, 27, 89, 255, 183, 116, 247, 38, 12, 7, 238, 77, 151, 167, 6, 102, 153, 126, 66, 28, 253, 253, 216, 64, 20, 138, 117, 72, 15, 216, 178, 37, 208, 179, 63, 204, 39, 94, 244, 170, 48, 190, 21, 11, 73, 169, 156, 104, 193, 3, 17, 100, 28, 60, 50, 92, 235, 218, 57, 73, 119, 19, 101, 164, 192, 161, 197, 106, 105, 73, 2, 3, 1, 0, 1]); 59 let pkBlob: cryptoFramework.DataBlob = { data: pkVal }; 60 try { 61 let keyPair = rsaGenerator.convertKeySync(pkBlob, null); 62 if (keyPair !== null) { 63 console.info('convertKeySync success'); 64 } 65 } catch (e) { 66 console.error(`get key pair failed, ${e.code}, ${e.message}`); 67 } 68 } 69 ``` 70 71## Converting Binary Data into an ECC Key Pair 72 73For details, see the ECC section of [Asymmetric Key Generation and Conversion Specifications](crypto-asym-key-generation-conversion-spec.md). 74 751. Obtain the binary data of the ECC public key or private key and encapsulate the data into a **DataBlob** object. 76 77 Either the public key or private key can be passed in. As shown in the following example, the public key and private key are passed in separately. 78 792. Call [cryptoFramework.createAsyKeyGenerator](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreateasykeygenerator) with the string parameter **'ECC256'** to create an asymmetric key generator (**AsyKeyGenerator**) object for a 256-bit ECC key pair. 80 813. Call [AsyKeyGenerator.convertKey](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkey-3) to convert the binary data into an asymmetric key pair (**KeyPair**). 82 83- Generate an ECC key pair (using callback-based APIs). 84 ```ts 85 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 86 87 function convertEccAsyKey() { 88 let pubKeyArray = new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7, 3, 66, 0, 4, 83, 96, 142, 9, 86, 214, 126, 106, 247, 233, 92, 125, 4, 128, 138, 105, 246, 162, 215, 71, 81, 58, 202, 121, 26, 105, 211, 55, 130, 45, 236, 143, 55, 16, 248, 75, 167, 160, 167, 106, 2, 152, 243, 44, 68, 66, 0, 167, 99, 92, 235, 215, 159, 239, 28, 106, 124, 171, 34, 145, 124, 174, 57, 92]); 89 let priKeyArray = new Uint8Array([48, 49, 2, 1, 1, 4, 32, 115, 56, 137, 35, 207, 0, 60, 191, 90, 61, 136, 105, 210, 16, 27, 4, 171, 57, 10, 61, 123, 40, 189, 28, 34, 207, 236, 22, 45, 223, 10, 189, 160, 10, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7]); 90 let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyArray }; 91 let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyArray }; 92 let generator = cryptoFramework.createAsyKeyGenerator('ECC256'); 93 generator.convertKey(pubKeyBlob, priKeyBlob, (error, data) => { 94 if (error) { 95 console.error(`convertKey failed, ${error.code}, ${error.message}`); 96 return; 97 } 98 console.info('convertKey success'); 99 }); 100 } 101 ``` 102 103- Example: Convert binary data into an ECC key pair (using the synchronous API [convertKeySync](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkeysync12)). 104 ```ts 105 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 106 107 function convertECCAsyKeySync() { 108 let pubKeyArray = new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7, 3, 66, 0, 4, 83, 96, 142, 9, 86, 214, 126, 106, 247, 233, 92, 125, 4, 128, 138, 105, 246, 162, 215, 71, 81, 58, 202, 121, 26, 105, 211, 55, 130, 45, 236, 143, 55, 16, 248, 75, 167, 160, 167, 106, 2, 152, 243, 44, 68, 66, 0, 167, 99, 92, 235, 215, 159, 239, 28, 106, 124, 171, 34, 145, 124, 174, 57, 92]); 109 let priKeyArray = new Uint8Array([48, 49, 2, 1, 1, 4, 32, 115, 56, 137, 35, 207, 0, 60, 191, 90, 61, 136, 105, 210, 16, 27, 4, 171, 57, 10, 61, 123, 40, 189, 28, 34, 207, 236, 22, 45, 223, 10, 189, 160, 10, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7]); 110 let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyArray }; 111 let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyArray }; 112 let generator = cryptoFramework.createAsyKeyGenerator('ECC256'); 113 try { 114 let keyPair = generator.convertKeySync(pubKeyBlob, priKeyBlob); 115 if (keyPair !== null) { 116 console.info('convertKeySync success'); 117 } 118 } catch (e) { 119 console.error(`get key pair failed, ${e.code}, ${e.message}`); 120 } 121 } 122 ``` 123 124## Converting PKCS#8 Binary Data into an ECC Private Key 125 126For details, see the ECC section of [Asymmetric Key Generation and Conversion Specifications](crypto-asym-key-generation-conversion-spec.md). 127 128Obtain the binary data of the ECC public or private key, encapsulate the data into a **DataBlob** object, and convert the data into the ECC key format. The following is an example: 129 1301. Call [cryptoFramework.createAsyKeyGenerator](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreateasykeygenerator) with the string parameter **'ECC256'** to create an asymmetric key generator (**AsyKeyGenerator**) object for a 256-bit ECC key pair. 131 1322. Call [PubKey.getEncoded](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getencoded) to obtain the byte stream of the public key data, and call [PriKey.getEncodeDer](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getencodedder12-1) with **format** set to **'PKCS8'** to obtain the byte stream of the private key data. The binary data of the key object is obtained. 133 1343. Call [AsyKeyGenerator.convertKey](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkey-3) to convert the binary data into an asymmetric key pair. 135 136 ```ts 137 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 138 139 async function main() { 140 // Create an AsyKeyGenerator instance. 141 let eccGenerator = cryptoFramework.createAsyKeyGenerator('ECC256'); 142 // Use AsyKeyGenerator to randomly generate an asymmetric key pair. 143 let keyGenPromise = eccGenerator.generateKeyPair(); 144 keyGenPromise.then(keyPair => { 145 let pubKey = keyPair.pubKey; 146 let priKey = keyPair.priKey; 147 // Obtain the binary data of the ECC asymmetric key pair. 148 let pubBlob = pubKey.getEncoded(); 149 let skBlob = priKey.getEncodedDer('PKCS8'); 150 let generator = cryptoFramework.createAsyKeyGenerator('ECC256'); 151 generator.convertKey(pubBlob, skBlob, (error, data) => { 152 if (error) { 153 console.error(`convertKey failed, ${error.code}, ${error.message}`); 154 return; 155 } 156 console.info('convertKey success'); 157 }); 158 }); 159 } 160 ``` 161 162## Converting Binary Data into an SM2 Key Pair 163 164For details, see the SM2 section of [Asymmetric Key Generation and Conversion Specifications](crypto-asym-key-generation-conversion-spec.md#sm2). 165 1661. Obtain the binary data of the SM2 public key or private key and encapsulate the data into a **DataBlob** object. 167 168 Either the public key or private key can be passed in. In the following example, the public key and private key are passed in separately. 169 1702. 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. 171 1723. Call [AsyKeyGenerator.convertKey](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkey-3) to convert the binary data into an asymmetric key pair (**KeyPair**). 173 174- Example: Convert binary data into an SM2 key pair (using callback-based APIs). 175 ```ts 176 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 177 178 function convertSM2AsyKey() { 179 let pubKeyArray = new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 129, 28, 207, 85, 1, 130, 45, 3, 66, 0, 4, 90, 3, 58, 157, 190, 248, 76, 7, 132, 200, 151, 208, 112, 230, 96, 140, 90, 238, 211, 155, 128, 109, 248, 40, 83, 214, 78, 42, 104, 106, 55, 148, 249, 35, 61, 32, 221, 135, 143, 100, 45, 97, 194, 176, 52, 73, 136, 174, 40, 70, 70, 34, 103, 103, 161, 99, 27, 187, 13, 187, 109, 244, 13, 7]); 180 let priKeyArray = new Uint8Array([48, 49, 2, 1, 1, 4, 32, 54, 41, 239, 240, 63, 188, 134, 113, 31, 102, 149, 203, 245, 89, 15, 15, 47, 202, 170, 60, 38, 154, 28, 169, 189, 100, 251, 76, 112, 223, 156, 159, 160, 10, 6, 8, 42, 129, 28, 207, 85, 1, 130, 45]); 181 let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyArray }; 182 let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyArray }; 183 let generator = cryptoFramework.createAsyKeyGenerator('SM2_256'); 184 generator.convertKey(pubKeyBlob, priKeyBlob, (error, data) => { 185 if (error) { 186 console.error(`convertKey failed, ${error.code}, ${error.message}`); 187 return; 188 } 189 console.info('convertKey success'); 190 }); 191 } 192 ``` 193 194- Example: Convert binary data into a key pair (using the synchronous API [convertKeySync](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkeysync12)). 195 ```ts 196 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 197 198 function convertSM2AsyKeySync() { 199 let pubKeyArray = new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 129, 28, 207, 85, 1, 130, 45, 3, 66, 0, 4, 90, 3, 58, 157, 190, 248, 76, 7, 132, 200, 151, 208, 112, 230, 96, 140, 90, 238, 211, 155, 128, 109, 248, 40, 83, 214, 78, 42, 104, 106, 55, 148, 249, 35, 61, 32, 221, 135, 143, 100, 45, 97, 194, 176, 52, 73, 136, 174, 40, 70, 70, 34, 103, 103, 161, 99, 27, 187, 13, 187, 109, 244, 13, 7]); 200 let priKeyArray = new Uint8Array([48, 49, 2, 1, 1, 4, 32, 54, 41, 239, 240, 63, 188, 134, 113, 31, 102, 149, 203, 245, 89, 15, 15, 47, 202, 170, 60, 38, 154, 28, 169, 189, 100, 251, 76, 112, 223, 156, 159, 160, 10, 6, 8, 42, 129, 28, 207, 85, 1, 130, 45]); 201 let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyArray }; 202 let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyArray }; 203 let generator = cryptoFramework.createAsyKeyGenerator('SM2_256'); 204 try { 205 let keyPair = generator.convertKeySync(pubKeyBlob, priKeyBlob); 206 if (keyPair !== null) { 207 console.info('convertKeySync success'); 208 } 209 } catch (e) { 210 console.error(`get key pair failed, ${e.code}, ${e.message}`); 211 } 212 } 213 ``` 214