1e41f4b71Sopenharmony_ci# Signature Recovery Using an RSA Key Pair (PKCS1 Mode) (C/C++)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [RSA](crypto-sign-sig-verify-overview.md#rsa).
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## Adding the Dynamic Library in the CMake Script
8e41f4b71Sopenharmony_ci```txt
9e41f4b71Sopenharmony_ci   target_link_libraries(entry PUBLIC libohcrypto.so)
10e41f4b71Sopenharmony_ci```
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci## How to Develop
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci1. Use [OH_CryptoVerify_Create](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_create) with the string parameter **'RSA1024|PKCS1|SHA256|Recover'** to create a **Verify** instance. The key parameters must be the same as that used to create the **Sign** instance.
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci2. Use [OH_CryptoVerify_Init](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_init) to initialize the **Verify** instance by using the public key (**OH_CryptoPubKey**).
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci3. Use [OH_CryptoVerify_Recover](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_recover) to restore the data.
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci**Example**
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci```c++
24e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_common.h"
25e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_signature.h"
26e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_asym_key.h"
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_cistatic OH_Crypto_ErrCode doTestRsaSignatureRecover()
29e41f4b71Sopenharmony_ci{
30e41f4b71Sopenharmony_ci   OH_CryptoAsymKeyGenerator *keyCtx = nullptr;
31e41f4b71Sopenharmony_ci   OH_CryptoKeyPair *keyPair = nullptr;
32e41f4b71Sopenharmony_ci   OH_CryptoVerify *verify = nullptr;
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci   uint8_t plainText[] = {
35e41f4b71Sopenharmony_ci      0xc4, 0xa5, 0xe5, 0x45, 0xee, 0x71, 0x5e, 0x3b, 0x24, 0x1d, 0x7e, 0x62, 0xd6, 0x6b, 0xab, 0x98,
36e41f4b71Sopenharmony_ci      0x88, 0x0f, 0xaf, 0x1e, 0x96, 0xa0, 0x6c, 0xa5, 0x0d, 0x29, 0xfd, 0xcc, 0xef, 0xf6, 0x2b, 0x92
37e41f4b71Sopenharmony_ci   };
38e41f4b71Sopenharmony_ci   Crypto_DataBlob msgBlob = {
39e41f4b71Sopenharmony_ci      .data = reinterpret_cast<uint8_t *>(plainText),
40e41f4b71Sopenharmony_ci      .len = sizeof(plainText)
41e41f4b71Sopenharmony_ci   };
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci   uint8_t pubKeyText[] = {
44e41f4b71Sopenharmony_ci      0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x52, 0x53, 0x41, 0x20, 0x50,
45e41f4b71Sopenharmony_ci      0x55, 0x42, 0x4c, 0x49, 0x43, 0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d,
46e41f4b71Sopenharmony_ci      0x49, 0x47, 0x4a, 0x41, 0x6f, 0x47, 0x42, 0x41, 0x4b, 0x72, 0x55, 0x74, 0x74, 0x64, 0x76, 0x73,
47e41f4b71Sopenharmony_ci      0x2b, 0x62, 0x6e, 0x4d, 0x2f, 0x6f, 0x4e, 0x75, 0x39, 0x45, 0x42, 0x78, 0x35, 0x64, 0x49, 0x6d,
48e41f4b71Sopenharmony_ci      0x61, 0x70, 0x52, 0x67, 0x4d, 0x6a, 0x4b, 0x41, 0x38, 0x51, 0x48, 0x4b, 0x61, 0x75, 0x2f, 0x6c,
49e41f4b71Sopenharmony_ci      0x58, 0x50, 0x50, 0x68, 0x76, 0x38, 0x30, 0x69, 0x59, 0x4c, 0x46, 0x2b, 0x79, 0x35, 0x35, 0x0a,
50e41f4b71Sopenharmony_ci      0x6d, 0x42, 0x2f, 0x38, 0x2b, 0x4b, 0x68, 0x34, 0x34, 0x43, 0x2b, 0x5a, 0x76, 0x6f, 0x78, 0x5a,
51e41f4b71Sopenharmony_ci      0x66, 0x38, 0x78, 0x34, 0x6e, 0x78, 0x6f, 0x71, 0x76, 0x4f, 0x6f, 0x73, 0x32, 0x44, 0x55, 0x69,
52e41f4b71Sopenharmony_ci      0x51, 0x44, 0x4f, 0x4a, 0x35, 0x63, 0x57, 0x68, 0x5a, 0x62, 0x4d, 0x71, 0x4d, 0x42, 0x71, 0x62,
53e41f4b71Sopenharmony_ci      0x39, 0x30, 0x4e, 0x39, 0x63, 0x2f, 0x44, 0x51, 0x67, 0x39, 0x34, 0x63, 0x52, 0x7a, 0x35, 0x66,
54e41f4b71Sopenharmony_ci      0x0a, 0x68, 0x55, 0x66, 0x6d, 0x66, 0x6d, 0x54, 0x41, 0x46, 0x6a, 0x5a, 0x53, 0x33, 0x78, 0x6c,
55e41f4b71Sopenharmony_ci      0x78, 0x77, 0x6e, 0x50, 0x77, 0x66, 0x66, 0x39, 0x71, 0x44, 0x79, 0x4c, 0x63, 0x5a, 0x55, 0x6b,
56e41f4b71Sopenharmony_ci      0x6e, 0x64, 0x43, 0x30, 0x50, 0x77, 0x72, 0x6c, 0x38, 0x72, 0x70, 0x4b, 0x7a, 0x50, 0x47, 0x63,
57e41f4b71Sopenharmony_ci      0x71, 0x4e, 0x67, 0x33, 0x5a, 0x41, 0x67, 0x4d, 0x42, 0x41, 0x41, 0x45, 0x3d, 0x0a, 0x2d, 0x2d,
58e41f4b71Sopenharmony_ci      0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x52, 0x53, 0x41, 0x20, 0x50, 0x55, 0x42, 0x4c, 0x49,
59e41f4b71Sopenharmony_ci      0x43, 0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a,
60e41f4b71Sopenharmony_ci   };
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci   Crypto_DataBlob keyBlob = {
63e41f4b71Sopenharmony_ci      .data = reinterpret_cast<uint8_t *>(pubKeyText),
64e41f4b71Sopenharmony_ci      .len = sizeof(pubKeyText)
65e41f4b71Sopenharmony_ci   };
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci   uint8_t signText[] = {
68e41f4b71Sopenharmony_ci      0x1f, 0xe3, 0xcf, 0x8d, 0x94, 0xb4, 0xa0, 0x9e, 0xf1, 0x0c, 0x38, 0x59, 0xcb, 0x5b, 0x89, 0xc9,
69e41f4b71Sopenharmony_ci      0x66, 0x8b, 0xfd, 0x8d, 0x1e, 0x37, 0xfa, 0x5e, 0x1b, 0xb1, 0x51, 0x07, 0xf1, 0xb0, 0x7d, 0x18,
70e41f4b71Sopenharmony_ci      0x2d, 0x82, 0x2a, 0x04, 0xa4, 0x4e, 0x94, 0x7e, 0x76, 0xb8, 0xa4, 0x78, 0x90, 0x2f, 0x43, 0x1d,
71e41f4b71Sopenharmony_ci      0x95, 0x80, 0xd7, 0xb3, 0x46, 0x4d, 0x58, 0x4b, 0xcd, 0x1f, 0x1d, 0xb3, 0x1f, 0x6b, 0x15, 0xd8,
72e41f4b71Sopenharmony_ci      0x33, 0x51, 0x1d, 0x36, 0x12, 0x39, 0x92, 0xb4, 0x4d, 0xe2, 0x89, 0x26, 0x01, 0xe9, 0x1f, 0xc0,
73e41f4b71Sopenharmony_ci      0x9c, 0x7c, 0xd8, 0xeb, 0x47, 0xff, 0xfb, 0x5d, 0x98, 0x9a, 0x02, 0x6a, 0x16, 0x37, 0xb1, 0xf5,
74e41f4b71Sopenharmony_ci      0x08, 0x4d, 0xd7, 0xa0, 0xf2, 0x9e, 0xbe, 0x4b, 0x54, 0x77, 0x94, 0x95, 0x4b, 0x97, 0x10, 0x22,
75e41f4b71Sopenharmony_ci      0x49, 0xa5, 0x2e, 0x05, 0x86, 0xfd, 0x6f, 0x9a, 0x40, 0xe6, 0x43, 0xab, 0xc5, 0xbc, 0xac, 0x21,
76e41f4b71Sopenharmony_ci   };
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci   Crypto_DataBlob signBlob = {
79e41f4b71Sopenharmony_ci      .data = reinterpret_cast<uint8_t *>(signText),
80e41f4b71Sopenharmony_ci      .len = sizeof(signText)
81e41f4b71Sopenharmony_ci   };
82e41f4b71Sopenharmony_ci   
83e41f4b71Sopenharmony_ci   // Key pair
84e41f4b71Sopenharmony_ci   OH_Crypto_ErrCode ret = CRYPTO_SUCCESS;
85e41f4b71Sopenharmony_ci   ret = OH_CryptoAsymKeyGenerator_Create((const char *)"RSA2048", &keyCtx);
86e41f4b71Sopenharmony_ci   if (ret != CRYPTO_SUCCESS) {
87e41f4b71Sopenharmony_ci      return ret;
88e41f4b71Sopenharmony_ci   }
89e41f4b71Sopenharmony_ci   ret = OH_CryptoAsymKeyGenerator_Convert(keyCtx, CRYPTO_PEM, &keyBlob, nullptr, &keyPair);
90e41f4b71Sopenharmony_ci   if (ret != CRYPTO_SUCCESS) {
91e41f4b71Sopenharmony_ci      OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
92e41f4b71Sopenharmony_ci      return ret;
93e41f4b71Sopenharmony_ci   }
94e41f4b71Sopenharmony_ci   OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair);
95e41f4b71Sopenharmony_ci   // verify
96e41f4b71Sopenharmony_ci   ret = OH_CryptoVerify_Create((const char *)"RSA1024|PKCS1|SHA256|Recover", &verify);
97e41f4b71Sopenharmony_ci   if (ret != CRYPTO_SUCCESS) {
98e41f4b71Sopenharmony_ci      OH_CryptoVerify_Destroy(verify);
99e41f4b71Sopenharmony_ci      OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
100e41f4b71Sopenharmony_ci      return ret;
101e41f4b71Sopenharmony_ci   }
102e41f4b71Sopenharmony_ci   ret = OH_CryptoVerify_Init(verify, pubKey);
103e41f4b71Sopenharmony_ci   if (ret != CRYPTO_SUCCESS) {
104e41f4b71Sopenharmony_ci      OH_CryptoVerify_Destroy(verify);
105e41f4b71Sopenharmony_ci      OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
106e41f4b71Sopenharmony_ci      return ret;
107e41f4b71Sopenharmony_ci   }
108e41f4b71Sopenharmony_ci   Crypto_DataBlob rawSignData = {.data = nullptr, .len = 0};
109e41f4b71Sopenharmony_ci   ret = OH_CryptoVerify_Recover(verify, (Crypto_DataBlob *)&signBlob, &rawSignData);
110e41f4b71Sopenharmony_ci   if (ret != CRYPTO_SUCCESS) {
111e41f4b71Sopenharmony_ci      OH_CryptoVerify_Destroy(verify);
112e41f4b71Sopenharmony_ci      OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
113e41f4b71Sopenharmony_ci      return ret;
114e41f4b71Sopenharmony_ci   }
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci   OH_CryptoVerify_Destroy(verify);
117e41f4b71Sopenharmony_ci   OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
118e41f4b71Sopenharmony_ci   OH_CryptoKeyPair_Destroy(keyPair);
119e41f4b71Sopenharmony_ci   return ret;
120e41f4b71Sopenharmony_ci}
121e41f4b71Sopenharmony_ci```
122