1e41f4b71Sopenharmony_ci# Converting Binary Data into an Asymmetric Key Pair (C/C++) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciThis 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. 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci> **NOTE** 7e41f4b71Sopenharmony_ci> 8e41f4b71Sopenharmony_ci> The asymmetric key conversion must comply with the following requirements: 9e41f4b71Sopenharmony_ci> 10e41f4b71Sopenharmony_ci> - The public key must use the ASN.1 syntax and DER encoding format and comply with X.509 specifications. 11e41f4b71Sopenharmony_ci> 12e41f4b71Sopenharmony_ci> - The private key must use the ASN.1 syntax and DER encoding format and comply with PKCS\#8 specifications. 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci## Adding the Dynamic Library in the CMake Script 15e41f4b71Sopenharmony_ci```txt 16e41f4b71Sopenharmony_ci target_link_libraries(entry PUBLIC libohcrypto.so) 17e41f4b71Sopenharmony_ci``` 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci## Converting Binary Data into an RSA Key Pair 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [RSA](crypto-asym-key-generation-conversion-spec.md#rsa). 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci1. Obtain the binary data of the RSA public key or private key and encapsulates the data into [Crypto_DataBlob](../../reference/apis-crypto-architecture-kit/_crypto_common_api.md#crypto_datablob). 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci Either the public key or private key can be passed in. In this example, the public key is passed in. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci2. Use [OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.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. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci The default number of primes for creating an RSA asymmetric key is **2**. The **PRIMES_2** parameter is omitted in the string parameter here. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci3. Use [OH_CryptoAsymKeyGenerator_Convert](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_convert) to convert the binary data into an asymmetric key pair (**OH_CryptoKeyPair**). 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci- Example: Convert binary data into an RSA key pair. 34e41f4b71Sopenharmony_ci```c++ 35e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_common.h" 36e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_asym_key.h" 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_cistatic OH_Crypto_ErrCode doTestDataCovertAsymKey() 39e41f4b71Sopenharmony_ci{ 40e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator *ctx = nullptr; 41e41f4b71Sopenharmony_ci OH_Crypto_ErrCode ret; 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci ret = OH_CryptoAsymKeyGenerator_Create("RSA1024|PRIMES_2", &ctx); 44e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 45e41f4b71Sopenharmony_ci return ret; 46e41f4b71Sopenharmony_ci } 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci 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, 49e41f4b71Sopenharmony_ci 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, 50e41f4b71Sopenharmony_ci 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, 51e41f4b71Sopenharmony_ci 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, 52e41f4b71Sopenharmony_ci 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, 53e41f4b71Sopenharmony_ci 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, 54e41f4b71Sopenharmony_ci 73,41,70,159,2,3,1,0,1 } 55e41f4b71Sopenharmony_ci Crypto_DataBlob retBlob = { .data = rsaDatablob, .len = sizeof(rsaDatablob) }; 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci OH_CryptoKeyPair *dupKeyPair = nullptr; 58e41f4b71Sopenharmony_ci ret = OH_CryptoAsymKeyGenerator_Convert(ctx, CRYPTO_DER, &retBlob, nullptr, &dupKeyPair); 59e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 60e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(ctx); 61e41f4b71Sopenharmony_ci return ret; 62e41f4b71Sopenharmony_ci } 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(ctx); 66e41f4b71Sopenharmony_ci OH_CryptoKeyPair_Destroy(dupKeyPair); 67e41f4b71Sopenharmony_ci return ret; 68e41f4b71Sopenharmony_ci} 69e41f4b71Sopenharmony_ci``` 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci## Converting Binary Data into an ECC Key Pair 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [ECC](crypto-asym-key-generation-conversion-spec.md#ecc). 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci1. Obtain the binary data of the ECC public key or private key and encapsulates the data into [Crypto_DataBlob](../../reference/apis-crypto-architecture-kit/_crypto_common_api.md#crypto_datablob). 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci 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. 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci2. Use [OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_create) with the string parameter **'ECC256'** to create an asymmetric key generator (**OH_CryptoAsymKeyGenerator**) object for a 256-bit ECC key pair. 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci3. Use [OH_CryptoAsymKeyGenerator_Convert](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_convert) to convert the binary data into an asymmetric key pair (**OH_CryptoKeyPair**). 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci- Example: Convert binary data into an ECC key pair. 84e41f4b71Sopenharmony_ci```c++ 85e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_common.h" 86e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_asym_key.h" 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_cistatic OH_Crypto_ErrCode doAsymEccCovert() 89e41f4b71Sopenharmony_ci{ 90e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator *ctx = nullptr; 91e41f4b71Sopenharmony_ci OH_Crypto_ErrCode ret; 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci ret = OH_CryptoAsymKeyGenerator_Create("ECC256", &ctx); 94e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 95e41f4b71Sopenharmony_ci return ret; 96e41f4b71Sopenharmony_ci } 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci uint8_t ecc224PubKeyBlobData[] = { 99e41f4b71Sopenharmony_ci 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, 100e41f4b71Sopenharmony_ci 205,95,171,229,33,116,44,192,12,115,119,84,156,128,56,180,246,84,43,33,244,224,221,181, 101e41f4b71Sopenharmony_ci 154,155,222,157,124,131,217,214,134,199,155,61,196,203,107,13,227,121,57,199,109,220, 102e41f4b71Sopenharmony_ci 103,55,78,148,185,226,212,162,31,66,201,50,129,1,156 103e41f4b71Sopenharmony_ci }; 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci uint8_t ecc224PriKeyBlobData[] = { 106e41f4b71Sopenharmony_ci 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, 107e41f4b71Sopenharmony_ci 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 108e41f4b71Sopenharmony_ci }; 109e41f4b71Sopenharmony_ci Crypto_DataBlob pubBlob = { .data = ecc224PubKeyBlobData, .len = sizeof(ecc224PubKeyBlobData) }; 110e41f4b71Sopenharmony_ci Crypto_DataBlob priBlob = { .data = ecc224PriKeyBlobData, .len = sizeof(ecc224PriKeyBlobData) }; 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci OH_CryptoKeyPair *dupKeyPair = nullptr; 113e41f4b71Sopenharmony_ci ret = OH_CryptoAsymKeyGenerator_Convert(ctx, CRYPTO_DER, &pubBlob, &priBlob, &dupKeyPair); 114e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 115e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(ctx); 116e41f4b71Sopenharmony_ci return ret; 117e41f4b71Sopenharmony_ci } 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(ctx); 120e41f4b71Sopenharmony_ci OH_CryptoKeyPair_Destroy(dupKeyPair); 121e41f4b71Sopenharmony_ci return ret; 122e41f4b71Sopenharmony_ci} 123e41f4b71Sopenharmony_ci``` 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci## Converting PKCS #8 Binary Data into an ECC Private Key 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [ECC](crypto-asym-key-generation-conversion-spec.md#ecc). 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ciObtain the binary data of the ECC public or private key, encapsulate the data into a [Crypto_DataBlob](../../reference/apis-crypto-architecture-kit/_crypto_common_api.md#crypto_datablob) object, and convert the data into the ECC key format. 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci1. Use [OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_create) with the string parameter **'ECC256'** to create an asymmetric key generator (**OH_CryptoAsymKeyGenerator**) object for a 256-bit ECC key pair. 132e41f4b71Sopenharmony_ci2. Use [OH_CryptoPubKey_Encode](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptopubkey_encode) to obtain the public key data byte stream. 133e41f4b71Sopenharmony_ci3. Use [OH_CryptoAsymKeyGenerator_Convert](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_convert) to convert the binary key data into an asymmetric key object (**OH_CryptoKeyPair**). 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci**Example** 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci```c++ 138e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_common.h" 139e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_asym_key.h" 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_cistatic OH_Crypto_ErrCode doAsymEccCovert() 142e41f4b71Sopenharmony_ci{ 143e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator *ctx = nullptr; 144e41f4b71Sopenharmony_ci OH_CryptoKeyPair *keyPair = nullptr; 145e41f4b71Sopenharmony_ci OH_Crypto_ErrCode ret; 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci ret = OH_CryptoAsymKeyGenerator_Create("ECC256", &ctx); 148e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 149e41f4b71Sopenharmony_ci return ret; 150e41f4b71Sopenharmony_ci } 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci ret = OH_CryptoAsymKeyGenerator_Generate(ctx, &keyPair); 153e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 154e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(ctx); 155e41f4b71Sopenharmony_ci return ret; 156e41f4b71Sopenharmony_ci } 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair); 159e41f4b71Sopenharmony_ci Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 }; 160e41f4b71Sopenharmony_ci ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_DER, nullptr, &retBlob); 161e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 162e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(ctx); 163e41f4b71Sopenharmony_ci OH_CryptoKeyPair_Destroy(keyPair); 164e41f4b71Sopenharmony_ci return ret; 165e41f4b71Sopenharmony_ci } 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci OH_CryptoKeyPair *dupKeyPair = nullptr; 168e41f4b71Sopenharmony_ci ret = OH_CryptoAsymKeyGenerator_Convert(ctx, CRYPTO_DER, &retBlob, nullptr, &dupKeyPair); 169e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 170e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(ctx); 171e41f4b71Sopenharmony_ci OH_CryptoKeyPair_Destroy(keyPair); 172e41f4b71Sopenharmony_ci return ret; 173e41f4b71Sopenharmony_ci } 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci OH_Crypto_FreeDataBlob(&retBlob); 176e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(ctx); 177e41f4b71Sopenharmony_ci OH_CryptoKeyPair_Destroy(keyPair); 178e41f4b71Sopenharmony_ci OH_CryptoKeyPair_Destroy(dupKeyPair); 179e41f4b71Sopenharmony_ci return ret; 180e41f4b71Sopenharmony_ci} 181e41f4b71Sopenharmony_ci``` 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci## Converting Binary Data into an SM2 Key Pair 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [SM2](crypto-asym-key-generation-conversion-spec.md#sm2). 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci1. Obtain the binary data of the SM2 public key or private key and encapsulate the data into [Crypto_DataBlob](../../reference/apis-crypto-architecture-kit/_crypto_common_api.md#crypto_datablob). 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci 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. 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci2. Use [OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.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 pair. 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci3. Use [OH_CryptoAsymKeyGenerator_Convert](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_convert) to convert the binary data into an asymmetric key pair (**OH_CryptoKeyPair**). 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ciExample: Convert binary data into an SM2 key pair. 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci```c++ 198e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_common.h" 199e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_asym_key.h" 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_cistatic OH_Crypto_ErrCode doAsymSm2Covert() 202e41f4b71Sopenharmony_ci{ 203e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator *ctx = nullptr; 204e41f4b71Sopenharmony_ci OH_CryptoKeyPair *dupKeyPair = nullptr; 205e41f4b71Sopenharmony_ci OH_Crypto_ErrCode ret; 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci ret = OH_CryptoAsymKeyGenerator_Create("SM2_256", &ctx); 208e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 209e41f4b71Sopenharmony_ci return ret; 210e41f4b71Sopenharmony_ci } 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ci uint8_t sm2PubKeyBlobData[] = { 48,89,48,19,6,7,42,134,72,206,61,2,1,6,8,42,134, 213e41f4b71Sopenharmony_ci 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, 214e41f4b71Sopenharmony_ci 56,180,246,84,43,33,244,224,221,181,154,155,222,157,124,131,217,214,134,199,155,61,196, 215e41f4b71Sopenharmony_ci 203,107,13,227,121,57,199,109,220,103,55,78,148,185,226,212,162,31,66,201,50,129,1,156 }; 216e41f4b71Sopenharmony_ci 217e41f4b71Sopenharmony_ci uint8_t sm2PriKeyBlobData[] = { 48,49,2,1,1,4,32,255,121,33,196,188,159,112,149,146, 218e41f4b71Sopenharmony_ci 107,243,78,152,214,12,119,87,199,207,57,116,64,150,240,121,22,88,138,196,71,70,222,160, 219e41f4b71Sopenharmony_ci 10,6,8,42,134,72,206,61,3,1,7 }; 220e41f4b71Sopenharmony_ci Crypto_DataBlob pubBlob = { .data = sm2PubKeyBlobData, .len = sizeof(sm2PubKeyBlobData) }; 221e41f4b71Sopenharmony_ci Crypto_DataBlob priBlob = { .data = sm2PriKeyBlobData, .len = sizeof(sm2PriKeyBlobData) }; 222e41f4b71Sopenharmony_ci ret = OH_CryptoAsymKeyGenerator_Convert(ctx, CRYPTO_DER, &pubBlob, &priBlob, &dupKeyPair); 223e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 224e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(ctx); 225e41f4b71Sopenharmony_ci return ret; 226e41f4b71Sopenharmony_ci } 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(ctx); 229e41f4b71Sopenharmony_ci OH_CryptoKeyPair_Destroy(dupKeyPair); 230e41f4b71Sopenharmony_ci return ret; 231e41f4b71Sopenharmony_ci} 232e41f4b71Sopenharmony_ci``` 233