1e41f4b71Sopenharmony_ci# Converting a PEM String into an Asymmetric Key Pair (C/C++) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciThis topic walks you through on how to convert a string in PEM format into an RSA asymmetric key pair (**OH_CryptoKeyPair**). 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci> **NOTE** 7e41f4b71Sopenharmony_ci> 8e41f4b71Sopenharmony_ci> The **convertPemKey** operation must comply with the following requirements: 9e41f4b71Sopenharmony_ci> 10e41f4b71Sopenharmony_ci> - The public key must comply with X.509 specifications, PKCS\#1 specifications, and PEM encoding format. 11e41f4b71Sopenharmony_ci> 12e41f4b71Sopenharmony_ci> - The private key must comply with the PKCS\#8 or PKCS\#1 specifications and the PEM encoding format. 13e41f4b71Sopenharmony_ci> 14e41f4b71Sopenharmony_ci> - Currently, only RSA asymmetric keys can be converted. 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## Adding the Dynamic Library in the CMake Script 17e41f4b71Sopenharmony_ci```txt 18e41f4b71Sopenharmony_ci target_link_libraries(entry PUBLIC libohcrypto.so) 19e41f4b71Sopenharmony_ci``` 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci## Converting a String in PEM Format into an RSA Key Pair 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [RSA](crypto-asym-key-generation-conversion-spec.md#rsa). 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci1. 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. 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_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. 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci2. 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**). 31e41f4b71Sopenharmony_ci3. Use [OH_CryptoPubKey_Encode](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptopubkey_encode) to convert the public key in the asymmetric key object into PKCS #1 or X.509 format and convert the private key into PKCS #1 or PKCS #8 format. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ciExample: Convert binary data into an RSA key pair. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci```c++ 36e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_common.h" 37e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_asym_key.h" 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_cistatic OH_Crypto_ErrCode doTestPemDataCovertAsymKey() 40e41f4b71Sopenharmony_ci{ 41e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator *ctx = nullptr; 42e41f4b71Sopenharmony_ci OH_Crypto_ErrCode ret; 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci ret = OH_CryptoAsymKeyGenerator_Create("RSA1024", &ctx); 45e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 46e41f4b71Sopenharmony_ci return ret; 47e41f4b71Sopenharmony_ci } 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci uint8_t sm2PubKeyBlobData[] = { 48,129,159,48,13,6,9,42,134,72,134,247,13,1,1,1,5,0,3,129, 50e41f4b71Sopenharmony_ci 141,0,48,129,137,2,129,129,0,235,184,151,247,130,216,140,187,64,124,219,137,140,184,53, 51e41f4b71Sopenharmony_ci 137,216,105,156,141,137,165,30,80,232,55,96,46,23,237,197,123,121,27,240,190,14,111,237, 52e41f4b71Sopenharmony_ci 172,67,42,47,164,226,248,211,157,213,194,131,109,181,41,173,217,127,252,121,126,26,130, 53e41f4b71Sopenharmony_ci 55,4,134,104,73,5,132,91,214,146,232,64,99,87,33,222,155,159,9,59,212,144,46,183,83,89, 54e41f4b71Sopenharmony_ci 220,189,148,13,176,5,139,156,230,143,16,152,79,36,8,112,40,174,35,83,82,57,137,87,123, 55e41f4b71Sopenharmony_ci 215,99,199,66,131,150,31,143,56,252,2,73,41,70,159,2,3,1,0,1 }; 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci OH_CryptoKeyPair *dupKeyPair = nullptr; 58e41f4b71Sopenharmony_ci Crypto_DataBlob pubBlob = { .data = sm2PubKeyBlobData, .len = sizeof(sm2PubKeyBlobData) }; 59e41f4b71Sopenharmony_ci ret = OH_CryptoAsymKeyGenerator_Convert(ctx, CRYPTO_DER, &pubBlob, nullptr, &dupKeyPair); 60e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 61e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(ctx); 62e41f4b71Sopenharmony_ci return ret; 63e41f4b71Sopenharmony_ci } 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci OH_CryptoPubKey *pubKey1 = OH_CryptoKeyPair_GetPubKey(dupKeyPair); 66e41f4b71Sopenharmony_ci Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 }; 67e41f4b71Sopenharmony_ci ret = OH_CryptoPubKey_Encode(pubKey1, CRYPTO_PEM, "PKCS1", &retBlob); 68e41f4b71Sopenharmony_ci if (ret != CRYPTO_SUCCESS) { 69e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(ctx); 70e41f4b71Sopenharmony_ci OH_CryptoKeyPair_Destroy(dupKeyPair); 71e41f4b71Sopenharmony_ci return ret; 72e41f4b71Sopenharmony_ci } 73e41f4b71Sopenharmony_ci OH_Crypto_FreeDataBlob(retBlob); 74e41f4b71Sopenharmony_ci OH_CryptoAsymKeyGenerator_Destroy(ctx); 75e41f4b71Sopenharmony_ci OH_CryptoKeyPair_Destroy(dupKeyPair); 76e41f4b71Sopenharmony_ci return ret; 77e41f4b71Sopenharmony_ci} 78e41f4b71Sopenharmony_ci``` 79