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