1# Converting Binary Data into an Asymmetric Key Pair (ArkTS) 2 3 4This 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. 5 6 7> **NOTE** 8> 9> The asymmetric key conversion must comply with the following requirements: 10> 11> - The public key must use the ASN.1 syntax and DER encoding format and comply with X.509 specifications. 12> 13> - The private key must use the ASN.1 syntax and DER encoding format and comply with PKCS\#8 specifications. 14 15 16## Converting Binary Data into an RSA Key Pair 17 18For details about the algorithm specifications, see [RSA](crypto-asym-key-generation-conversion-spec.md#rsa). 19 201. Obtain the binary data of the RSA public key or private key and encapsulate the data into a **DataBlob** object. 21 22 Either the public key or private key can be passed in. In this example, the public key is passed in. 23 242. 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. 25 26 The default number of primes for creating an RSA asymmetric key is **2**. The **PRIMES_2** parameter is omitted in the string parameter here. 27 283. 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**). 29 30- Example: Convert binary data into an RSA key pair (using callback-based APIs). 31 ```ts 32 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 33 34 function convertAsyKey() { 35 let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); 36 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]); 37 let pkBlob: cryptoFramework.DataBlob = { data: pkVal }; 38 rsaGenerator.convertKey(pkBlob, null, (err, keyPair) => { 39 if (err) { 40 console.error(`convertKey failed, ${err.code}, ${err.message}`); 41 return; 42 } 43 console.info('convertKey success'); 44 }); 45 } 46 ``` 47 48- Example: Convert binary data into an RSA key pair (using the synchronous API [convertKeySync](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkeysync12)). 49 ```ts 50 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 51 52 function convertAsyKeySync() { 53 let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); 54 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]); 55 let pkBlob: cryptoFramework.DataBlob = { data: pkVal }; 56 try { 57 let keyPair = rsaGenerator.convertKeySync(pkBlob, null); 58 if (keyPair !== null) { 59 console.info('convertKeySync success'); 60 } 61 } catch (e) { 62 console.error(`get key pair failed, ${e.code}, ${e.message}`); 63 } 64 } 65 ``` 66 67 68## Converting Binary Data into an ECC Key Pair 69 70For details about the algorithm specifications, see [ECC](crypto-asym-key-generation-conversion-spec.md#ecc). 71 721. Obtain the binary data of the ECC public key or private key and encapsulate the data into a **DataBlob** object. 73 74 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. 75 762. 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. 77 783. 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**). 79 80- Example: Convert binary data into an ECC key pair (using callback-based APIs). 81 ```ts 82 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 83 84 function convertEccAsyKey() { 85 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]); 86 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]); 87 let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyArray }; 88 let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyArray }; 89 let generator = cryptoFramework.createAsyKeyGenerator('ECC256'); 90 generator.convertKey(pubKeyBlob, priKeyBlob, (error, data) => { 91 if (error) { 92 console.error(`convertKey failed, ${error.code}, ${error.message}`); 93 return; 94 } 95 console.info('convertKey success'); 96 }); 97 } 98 ``` 99 100- Example: Convert binary data into an ECC key pair (using the synchronous API [convertKeySync](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkeysync12)). 101 ```ts 102 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 103 104 function convertECCAsyKeySync() { 105 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]); 106 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]); 107 let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyArray }; 108 let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyArray }; 109 let generator = cryptoFramework.createAsyKeyGenerator('ECC256'); 110 try { 111 let keyPair = generator.convertKeySync(pubKeyBlob, priKeyBlob); 112 if (keyPair !== null) { 113 console.info('convertKeySync success'); 114 } 115 } catch (e) { 116 console.error(`get key pair failed, ${e.code}, ${e.message}`); 117 } 118 } 119 ``` 120 121## Converting PKCS #8 Binary Data into an ECC Private Key 122 123For details about the algorithm specifications, see [ECC](crypto-asym-key-generation-conversion-spec.md#ecc). 124 125Obtain 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: 126 1271. 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. 128 1292. 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. 130 1313. Call [AsyKeyGenerator.convertKey](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkey-3) to convert the binary data into an asymmetric key pair. 132 133 ```ts 134 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 135 136 async function main() { 137 // Create an AsyKeyGenerator instance. 138 let eccGenerator = cryptoFramework.createAsyKeyGenerator('ECC256'); 139 // Use AsyKeyGenerator to randomly generate an asymmetric key pair. 140 let keyGenPromise = eccGenerator.generateKeyPair(); 141 keyGenPromise.then(keyPair => { 142 let pubKey = keyPair.pubKey; 143 let priKey = keyPair.priKey; 144 // Obtain the binary data of the ECC asymmetric key pair. 145 let pubBlob = pubKey.getEncoded(); 146 let skBlob = priKey.getEncodedDer('PKCS8'); 147 let generator = cryptoFramework.createAsyKeyGenerator('ECC256'); 148 generator.convertKey(pubBlob, skBlob, (error, data) => { 149 if (error) { 150 console.error(`convertKey failed, ${error.code}, ${error.message}`); 151 return; 152 } 153 console.info('convertKey success'); 154 }); 155 }); 156 } 157 ``` 158 159## Converting Binary Data into an SM2 Key Pair 160 161For details about the algorithm specifications, see [SM2](crypto-asym-key-generation-conversion-spec.md#sm2). 162 1631. Obtain the binary data of the SM2 public key or private key and encapsulate the data into a **DataBlob** object. 164 165 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. 166 1672. 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. 168 1693. 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**). 170 171- Example: Convert binary data into an SM2 key pair (using callback-based APIs). 172 ```ts 173 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 174 175 function convertSM2AsyKey() { 176 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]); 177 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]); 178 let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyArray }; 179 let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyArray }; 180 let generator = cryptoFramework.createAsyKeyGenerator('SM2_256'); 181 generator.convertKey(pubKeyBlob, priKeyBlob, (error, data) => { 182 if (error) { 183 console.error(`convertKey failed, ${error.code}, ${error.message}`); 184 return; 185 } 186 console.info('convertKey success'); 187 }); 188 } 189 ``` 190 191- Example: Convert binary data into an SM2 key pair (using the synchronous API [convertKeySync](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkeysync12)). 192 ```ts 193 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 194 195 function convertSM2AsyKeySync() { 196 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]); 197 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]); 198 let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyArray }; 199 let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyArray }; 200 let generator = cryptoFramework.createAsyKeyGenerator('SM2_256'); 201 try { 202 let keyPair = generator.convertKeySync(pubKeyBlob, priKeyBlob); 203 if (keyPair !== null) { 204 console.info('convertKeySync success'); 205 } 206 } catch (e) { 207 console.error(`get key pair failed, ${e.code}, ${e.message}`); 208 } 209 } 210 ``` 211