使用RSA密钥对（PKCS1模式）签名恢复(C/C++)
============================

对应的算法规格请查看[验签算法规格：RSA](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/crypto-sign-sig-verify-overview#rsa)。

#### 在CMake脚本中链接相关动态库

target\_link\_libraries(entry PUBLIC libohcrypto.so)

#### 开发步骤

1.  调用[OH\_CryptoVerify\_Create](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-crypto-signature-h#oh_cryptoverify_create)，指定字符串参数'RSA1024|PKCS1|SHA256|Recover'，与签名的Sign实例保持一致。创建Verify实例，用于完成验签操作。
    
2.  调用[OH\_CryptoVerify\_Init](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-crypto-signature-h#oh_cryptoverify_init)，使用公钥（OH\_CryptoPubKey）初始化Verify实例。
    
3.  调用[OH\_CryptoVerify\_Recover](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-crypto-signature-h#oh_cryptoverify_recover)，对数据进行签名恢复。
    

#include "signing\_signature\_verification.h"

OH\_Crypto\_ErrCode DoTestRsaSignatureRecover()
{
    OH\_CryptoAsymKeyGenerator \*keyCtx = nullptr;
    OH\_CryptoKeyPair \*keyPair = nullptr;
    OH\_CryptoVerify \*verify = nullptr;

    uint8\_t plainText\[\] = {0xc4, 0xa5, 0xe5, 0x45, 0xee, 0x71, 0x5e, 0x3b, 0x24, 0x1d, 0x7e,
                           0x62, 0xd6, 0x6b, 0xab, 0x98, 0x88, 0x0f, 0xaf, 0x1e, 0x96, 0xa0,
                           0x6c, 0xa5, 0x0d, 0x29, 0xfd, 0xcc, 0xef, 0xf6, 0x2b, 0x92};
    Crypto\_DataBlob msgBlob = {.data = reinterpret\_cast<uint8\_t \*>(plainText), .len = sizeof(plainText)};

    uint8\_t pubKeyText\[\] = {
        0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x52, 0x53, 0x41, 0x20, 0x50, 0x55, 0x42,
        0x4c, 0x49, 0x43, 0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x47, 0x4a, 0x41,
        0x6f, 0x47, 0x42, 0x41, 0x4b, 0x72, 0x55, 0x74, 0x74, 0x64, 0x76, 0x73, 0x2b, 0x62, 0x6e, 0x4d, 0x2f, 0x6f,
        0x4e, 0x75, 0x39, 0x45, 0x42, 0x78, 0x35, 0x64, 0x49, 0x6d, 0x61, 0x70, 0x52, 0x67, 0x4d, 0x6a, 0x4b, 0x41,
        0x38, 0x51, 0x48, 0x4b, 0x61, 0x75, 0x2f, 0x6c, 0x58, 0x50, 0x50, 0x68, 0x76, 0x38, 0x30, 0x69, 0x59, 0x4c,
        0x46, 0x2b, 0x79, 0x35, 0x35, 0x0a, 0x6d, 0x42, 0x2f, 0x38, 0x2b, 0x4b, 0x68, 0x34, 0x34, 0x43, 0x2b, 0x5a,
        0x76, 0x6f, 0x78, 0x5a, 0x66, 0x38, 0x78, 0x34, 0x6e, 0x78, 0x6f, 0x71, 0x76, 0x4f, 0x6f, 0x73, 0x32, 0x44,
        0x55, 0x69, 0x51, 0x44, 0x4f, 0x4a, 0x35, 0x63, 0x57, 0x68, 0x5a, 0x62, 0x4d, 0x71, 0x4d, 0x42, 0x71, 0x62,
        0x39, 0x30, 0x4e, 0x39, 0x63, 0x2f, 0x44, 0x51, 0x67, 0x39, 0x34, 0x63, 0x52, 0x7a, 0x35, 0x66, 0x0a, 0x68,
        0x55, 0x66, 0x6d, 0x66, 0x6d, 0x54, 0x41, 0x46, 0x6a, 0x5a, 0x53, 0x33, 0x78, 0x6c, 0x78, 0x77, 0x6e, 0x50,
        0x77, 0x66, 0x66, 0x39, 0x71, 0x44, 0x79, 0x4c, 0x63, 0x5a, 0x55, 0x6b, 0x6e, 0x64, 0x43, 0x30, 0x50, 0x77,
        0x72, 0x6c, 0x38, 0x72, 0x70, 0x4b, 0x7a, 0x50, 0x47, 0x63, 0x71, 0x4e, 0x67, 0x33, 0x5a, 0x41, 0x67, 0x4d,
        0x42, 0x41, 0x41, 0x45, 0x3d, 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x52, 0x53, 0x41,
        0x20, 0x50, 0x55, 0x42, 0x4c, 0x49, 0x43, 0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a,
    };

    Crypto\_DataBlob keyBlob = {.data = reinterpret\_cast<uint8\_t \*>(pubKeyText), .len = sizeof(pubKeyText)};

    uint8\_t signText\[\] = {
        0x1f, 0xe3, 0xcf, 0x8d, 0x94, 0xb4, 0xa0, 0x9e, 0xf1, 0x0c, 0x38, 0x59, 0xcb, 0x5b, 0x89, 0xc9,
        0x66, 0x8b, 0xfd, 0x8d, 0x1e, 0x37, 0xfa, 0x5e, 0x1b, 0xb1, 0x51, 0x07, 0xf1, 0xb0, 0x7d, 0x18,
        0x2d, 0x82, 0x2a, 0x04, 0xa4, 0x4e, 0x94, 0x7e, 0x76, 0xb8, 0xa4, 0x78, 0x90, 0x2f, 0x43, 0x1d,
        0x95, 0x80, 0xd7, 0xb3, 0x46, 0x4d, 0x58, 0x4b, 0xcd, 0x1f, 0x1d, 0xb3, 0x1f, 0x6b, 0x15, 0xd8,
        0x33, 0x51, 0x1d, 0x36, 0x12, 0x39, 0x92, 0xb4, 0x4d, 0xe2, 0x89, 0x26, 0x01, 0xe9, 0x1f, 0xc0,
        0x9c, 0x7c, 0xd8, 0xeb, 0x47, 0xff, 0xfb, 0x5d, 0x98, 0x9a, 0x02, 0x6a, 0x16, 0x37, 0xb1, 0xf5,
        0x08, 0x4d, 0xd7, 0xa0, 0xf2, 0x9e, 0xbe, 0x4b, 0x54, 0x77, 0x94, 0x95, 0x4b, 0x97, 0x10, 0x22,
        0x49, 0xa5, 0x2e, 0x05, 0x86, 0xfd, 0x6f, 0x9a, 0x40, 0xe6, 0x43, 0xab, 0xc5, 0xbc, 0xac, 0x21,
    };

    Crypto\_DataBlob signBlob = {.data = reinterpret\_cast<uint8\_t \*>(signText), .len = sizeof(signText)};

    // keypair
    OH\_Crypto\_ErrCode ret = CRYPTO\_SUCCESS;
    ret = OH\_CryptoAsymKeyGenerator\_Create((const char \*)"RSA2048", &keyCtx);
    if (ret != CRYPTO\_SUCCESS) {
        return ret;
    }
    ret = OH\_CryptoAsymKeyGenerator\_Convert(keyCtx, CRYPTO\_PEM, &keyBlob, nullptr, &keyPair);
    if (ret != CRYPTO\_SUCCESS) {
        OH\_CryptoAsymKeyGenerator\_Destroy(keyCtx);
        return ret;
    }
    OH\_CryptoPubKey \*pubKey = OH\_CryptoKeyPair\_GetPubKey(keyPair);
    // verify
    ret = OH\_CryptoVerify\_Create((const char \*)"RSA1024|PKCS1|SHA256|Recover", &verify);
    if (ret != CRYPTO\_SUCCESS) {
        OH\_CryptoVerify\_Destroy(verify);
        OH\_CryptoAsymKeyGenerator\_Destroy(keyCtx);
        return ret;
    }
    ret = OH\_CryptoVerify\_Init(verify, pubKey);
    if (ret != CRYPTO\_SUCCESS) {
        OH\_CryptoVerify\_Destroy(verify);
        OH\_CryptoAsymKeyGenerator\_Destroy(keyCtx);
        return ret;
    }
    Crypto\_DataBlob rawSignData = {.data = nullptr, .len = 0};
    ret = OH\_CryptoVerify\_Recover(verify, (Crypto\_DataBlob \*)&signBlob, &rawSignData);
    if (ret != CRYPTO\_SUCCESS) {
        OH\_CryptoVerify\_Destroy(verify);
        OH\_CryptoAsymKeyGenerator\_Destroy(keyCtx);
        return ret;
    }

    OH\_CryptoVerify\_Destroy(verify);
    OH\_CryptoAsymKeyGenerator\_Destroy(keyCtx);
    OH\_CryptoKeyPair\_Destroy(keyPair);
    return ret;
}