• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Converting a PEM String 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 walks you through on how to convert a string in PEM format into an RSA asymmetric key pair (**OH_CryptoKeyPair**).
11
12> **NOTE**
13>
14> The **convertPemKey** operation must comply with the following requirements:
15>
16> - The public key must comply with X.509 specifications, PKCS\#1 specifications, and PEM encoding format.
17>
18> - The private key must comply with the PKCS\#8, PKCS\#1 specifications, and the PEM encoding format.
19>
20> - Currently, only RSA asymmetric keys can be converted.
21
22## Adding the Dynamic Library in the CMake Script
23```txt
24target_link_libraries(entry PUBLIC libohcrypto.so)
25```
26
27## Converting a String in PEM Format into an RSA Key Pair
28
29For details about the algorithm specifications, see [RSA](crypto-asym-key-generation-conversion-spec.md#rsa).
30
311. 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.
32
33   The default number of primes for creating an RSA asymmetric key is **2**. The **PRIMES_2** parameter is omitted in the string parameter here.
34
352. 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**).
363. Call [OH_CryptoPubKey_Encode](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-key-h.md#oh_cryptopubkey_encode) to convert the public key in the asymmetric key object into the PKCS #1 or X.509 format.
37
38- Example: Convert binary data into an RSA key pair.
39
40```c++
41#include "CryptoArchitectureKit/crypto_common.h"
42#include "CryptoArchitectureKit/crypto_asym_key.h"
43
44static OH_Crypto_ErrCode doTestPemDataCovertAsymKey()
45{
46   OH_CryptoAsymKeyGenerator *ctx = nullptr;
47   OH_Crypto_ErrCode ret;
48
49   ret = OH_CryptoAsymKeyGenerator_Create("RSA1024", &ctx);
50   if (ret != CRYPTO_SUCCESS) {
51      return ret;
52   }
53
54   uint8_t pubKeyBlobData[] = { 48,129,159,48,13,6,9,42,134,72,134,247,13,1,1,1,5,0,3,129,
55      141,0,48,129,137,2,129,129,0,235,184,151,247,130,216,140,187,64,124,219,137,140,184,53,
56      137,216,105,156,141,137,165,30,80,232,55,96,46,23,237,197,123,121,27,240,190,14,111,237,
57      172,67,42,47,164,226,248,211,157,213,194,131,109,181,41,173,217,127,252,121,126,26,130,
58      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,
59      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,
60      215,99,199,66,131,150,31,143,56,252,2,73,41,70,159,2,3,1,0,1 };
61
62   OH_CryptoKeyPair *dupKeyPair = nullptr;
63   Crypto_DataBlob pubBlob = { .data = pubKeyBlobData, .len = sizeof(pubKeyBlobData) };
64   ret = OH_CryptoAsymKeyGenerator_Convert(ctx, CRYPTO_DER, &pubBlob, nullptr, &dupKeyPair);
65   if (ret != CRYPTO_SUCCESS) {
66      OH_CryptoAsymKeyGenerator_Destroy(ctx);
67      return ret;
68   }
69
70   OH_CryptoPubKey *pubKey1 = OH_CryptoKeyPair_GetPubKey(dupKeyPair);
71   Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 };
72   ret = OH_CryptoPubKey_Encode(pubKey1, CRYPTO_PEM, "PKCS1", &retBlob);
73   if (ret != CRYPTO_SUCCESS) {
74      OH_CryptoAsymKeyGenerator_Destroy(ctx);
75      OH_CryptoKeyPair_Destroy(dupKeyPair);
76      return ret;
77   }
78   OH_Crypto_FreeDataBlob(&retBlob);
79   OH_CryptoAsymKeyGenerator_Destroy(ctx);
80   OH_CryptoKeyPair_Destroy(dupKeyPair);
81   return ret;
82}
83```
84