1# Converting Binary Data into an Asymmetric Key Pair (C/C++) 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 (**OH_CryptoKeyPair**). 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## Adding the Dynamic Library in the CMake Script 21```txt 22target_link_libraries(entry PUBLIC libohcrypto.so) 23``` 24 25## Converting Binary Data into an RSA Key Pair 26 27For details about the algorithm specifications, see [RSA](crypto-asym-key-generation-conversion-spec.md#rsa). 28 291. Obtain the binary data of the RSA public key or private key and encapsulate the data into a [Crypto_DataBlob](../../reference/apis-crypto-architecture-kit/capi-cryptocommonapi-crypto-datablob.md) object. 30 31 The public key and private key can be passed separately. In this example, the public key is passed. 32 332. Call [OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-key-h.md#oh_cryptoasymkeygenerator_create) with the string parameter **'RSA1024'** to create an asymmetric key generator (**OH_CryptoAsymKeyGenerator**) object for a 1024-bit RSA key with two primes. 34 35 The default number of primes for creating an RSA asymmetric key is **2**. The **PRIMES_2** parameter is omitted in the string parameter here. 36 373. Call [OH_CryptoAsymKeyGenerator_Convert](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-key-h.md#oh_cryptoasymkeygenerator_convert) to convert the binary data into an asymmetric key pair (**OH_CryptoKeyPair**). 38 39- Example: Convert binary data into an RSA key pair. 40```c++ 41#include "CryptoArchitectureKit/crypto_common.h" 42#include "CryptoArchitectureKit/crypto_asym_key.h" 43 44static OH_Crypto_ErrCode doTestDataCovertAsymKey() 45{ 46 OH_CryptoAsymKeyGenerator *ctx = nullptr; 47 OH_Crypto_ErrCode ret; 48 49 ret = OH_CryptoAsymKeyGenerator_Create("RSA1024|PRIMES_2", &ctx); 50 if (ret != CRYPTO_SUCCESS) { 51 return ret; 52 } 53 54 uint8_t rsaDatablob[] = { 48,129,159,48,13,6,9,42,134,72,134,247,13,1,1,1,5,0,3,129,141,0, 55 48,129,137,2,129,129,0,235,184,151,247,130,216,140,187,64,124,219,137,140,184,53,137,216,105, 56 156,141,137,165,30,80,232,55,96,46,23,237,197,123,121,27,240,190,14,111,237,172,67,42,47,164, 57 226,248,211,157,213,194,131,109,181,41,173,217,127,252,121,126,26,130,55,4,134,104,73,5,132, 58 91,214,146,232,64,99,87,33,222,155,159,9,59,212,144,46,183,83,89,220,189,148,13,176,5,139,156, 59 230,143,16,152,79,36,8,112,40,174,35,83,82,57,137,87,123,215,99,199,66,131,150,31,143,56,252,2, 60 73,41,70,159,2,3,1,0,1 }; 61 Crypto_DataBlob retBlob = { .data = rsaDatablob, .len = sizeof(rsaDatablob) }; 62 63 OH_CryptoKeyPair *dupKeyPair = nullptr; 64 ret = OH_CryptoAsymKeyGenerator_Convert(ctx, CRYPTO_DER, &retBlob, nullptr, &dupKeyPair); 65 if (ret != CRYPTO_SUCCESS) { 66 OH_CryptoAsymKeyGenerator_Destroy(ctx); 67 return ret; 68 } 69 70 71 OH_CryptoAsymKeyGenerator_Destroy(ctx); 72 OH_CryptoKeyPair_Destroy(dupKeyPair); 73 return ret; 74} 75``` 76 77## Converting Binary Data into an ECC Key Pair 78 79For details about the algorithm specifications, see [ECC](crypto-asym-key-generation-conversion-spec.md#ecc). 80 811. Obtain the binary data of the ECC public key or private key and encapsulate the data into a [Crypto_DataBlob](../../reference/apis-crypto-architecture-kit/capi-cryptocommonapi-crypto-datablob.md) object. 82 83 The public key and private key can be passed separately. In this example, the public key and private key are passed. 84 852. Call [OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-key-h.md#oh_cryptoasymkeygenerator_create) with the string parameter **'ECC256'** to create an asymmetric key generator (**OH_CryptoAsymKeyGenerator**) object for a 256-bit ECC key. 86 873. Call [OH_CryptoAsymKeyGenerator_Convert](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-key-h.md#oh_cryptoasymkeygenerator_convert) to convert the binary data into an asymmetric key pair (**OH_CryptoKeyPair**). 88 89- Example: Convert binary data into an ECC key pair. 90```c++ 91#include "CryptoArchitectureKit/crypto_common.h" 92#include "CryptoArchitectureKit/crypto_asym_key.h" 93 94static OH_Crypto_ErrCode doAsymEccCovert() 95{ 96 OH_CryptoAsymKeyGenerator *ctx = nullptr; 97 OH_Crypto_ErrCode ret; 98 99 ret = OH_CryptoAsymKeyGenerator_Create("ECC256", &ctx); 100 if (ret != CRYPTO_SUCCESS) { 101 return ret; 102 } 103 104 uint8_t ecc224PubKeyBlobData[] = { 105 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,157,58,248, 106 205,95,171,229,33,116,44,192,12,115,119,84,156,128,56,180,246,84,43,33,244,224,221,181, 107 154,155,222,157,124,131,217,214,134,199,155,61,196,203,107,13,227,121,57,199,109,220, 108 103,55,78,148,185,226,212,162,31,66,201,50,129,1,156 109 }; 110 111 uint8_t ecc224PriKeyBlobData[] = { 112 48,49,2,1,1,4,32,255,121,33,196,188,159,112,149,146,107,243,78,152,214,12,119,87,199, 113 207,57,116,64,150,240,121,22,88,138,196,71,70,222,160,10,6,8,42,134,72,206,61,3,1,7 114 }; 115 Crypto_DataBlob pubBlob = { .data = ecc224PubKeyBlobData, .len = sizeof(ecc224PubKeyBlobData) }; 116 Crypto_DataBlob priBlob = { .data = ecc224PriKeyBlobData, .len = sizeof(ecc224PriKeyBlobData) }; 117 118 OH_CryptoKeyPair *dupKeyPair = nullptr; 119 ret = OH_CryptoAsymKeyGenerator_Convert(ctx, CRYPTO_DER, &pubBlob, &priBlob, &dupKeyPair); 120 if (ret != CRYPTO_SUCCESS) { 121 OH_CryptoAsymKeyGenerator_Destroy(ctx); 122 return ret; 123 } 124 125 OH_CryptoAsymKeyGenerator_Destroy(ctx); 126 OH_CryptoKeyPair_Destroy(dupKeyPair); 127 return ret; 128} 129``` 130 131## Converting Binary Data into an SM2 Key Pair 132 133For details about the algorithm specifications, see [SM2](crypto-asym-key-generation-conversion-spec.md#sm2). 134 1351. Obtain the binary data of the SM2 public key or private key and encapsulate the data into a [Crypto_DataBlob](../../reference/apis-crypto-architecture-kit/capi-cryptocommonapi-crypto-datablob.md) object. 136 137 The public key and private key can be passed separately. In this example, the public key and private key are passed. 138 1392. Call [OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-key-h.md#oh_cryptoasymkeygenerator_create) with the string parameter **'SM2_256'** to create an asymmetric key generator (**OH_CryptoAsymKeyGenerator**) object for a 256-bit SM2 key. 140 1413. Call [OH_CryptoAsymKeyGenerator_Convert](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-key-h.md#oh_cryptoasymkeygenerator_convert) to convert the binary data into an asymmetric key pair (**OH_CryptoKeyPair**). 142 143- The following uses the generation of an SM2 key pair as an example: 144```c++ 145#include "CryptoArchitectureKit/crypto_common.h" 146#include "CryptoArchitectureKit/crypto_asym_key.h" 147 148static OH_Crypto_ErrCode doAsymSm2Covert() 149{ 150 OH_CryptoAsymKeyGenerator *ctx = nullptr; 151 OH_CryptoKeyPair *dupKeyPair = nullptr; 152 OH_Crypto_ErrCode ret; 153 154 ret = OH_CryptoAsymKeyGenerator_Create("SM2_256", &ctx); 155 if (ret != CRYPTO_SUCCESS) { 156 return ret; 157 } 158 159 uint8_t sm2PubKeyBlobData[] = { 48,89,48,19,6,7,42,134,72,206,61,2,1,6,8,42,134, 160 72,206,61,3,1,7,3,66,0,4,157,58,248,205,95,171,229,33,116,44,192,12,115,119,84,156,128, 161 56,180,246,84,43,33,244,224,221,181,154,155,222,157,124,131,217,214,134,199,155,61,196, 162 203,107,13,227,121,57,199,109,220,103,55,78,148,185,226,212,162,31,66,201,50,129,1,156 }; 163 164 uint8_t sm2PriKeyBlobData[] = { 48,49,2,1,1,4,32,255,121,33,196,188,159,112,149,146, 165 107,243,78,152,214,12,119,87,199,207,57,116,64,150,240,121,22,88,138,196,71,70,222,160, 166 10,6,8,42,134,72,206,61,3,1,7 }; 167 Crypto_DataBlob pubBlob = { .data = sm2PubKeyBlobData, .len = sizeof(sm2PubKeyBlobData) }; 168 Crypto_DataBlob priBlob = { .data = sm2PriKeyBlobData, .len = sizeof(sm2PriKeyBlobData) }; 169 ret = OH_CryptoAsymKeyGenerator_Convert(ctx, CRYPTO_DER, &pubBlob, &priBlob, &dupKeyPair); 170 if (ret != CRYPTO_SUCCESS) { 171 OH_CryptoAsymKeyGenerator_Destroy(ctx); 172 return ret; 173 } 174 175 OH_CryptoAsymKeyGenerator_Destroy(ctx); 176 OH_CryptoKeyPair_Destroy(dupKeyPair); 177 return ret; 178} 179``` 180