0

谁能帮我找出为什么我在尝试从 unsigned char* 解析公钥/私钥时得到 -16000(错误的输入数据)?

这是我的代码(为简洁而编辑):

DataPoint* GetPublicKey(mbedtls_pk_context* pppctx)
{
    unsigned char* PKey = new unsigned char[16000];
    if (mbedtls_pk_write_pubkey_pem(pppctx, PKey, 16000) != 0)
    {
        delete[] PKey;
        return NULL;
    }
    DataPoint* Out = new DataPoint(strlen((char*)PKey) + 1); //Initializes an internal unsigned char* and size_t with the length of the key and the null byte
    memcpy(Out->Data, PKey, Out->Length);
    delete[] PKey;
    return Out;
}

void GenRSA(mbedtls_rsa_context* rs)
{
    mbedtls_rsa_gen_key(rs, mbedtls_ctr_drbg_random, &dctx, 2048, 65537);
}

int main()
{
    mbedtls_pk_context pctx;
    mbedtls_pk_init(&pctx);
    mbedtls_pk_setup(&pctx, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA));

    DataPoint* Key = GetPublicKey(&some_context_with_GenRSA_called);

    cout << mbedtls_pk_parse_public_key(&pctx, Key->Data, Key->Length) << endl; //Returns -16000

    return 0
}

和私钥一样,我做错了什么?

4

1 回答 1

0

The docs for mbedtls_pk_parse_public_key say:

On entry, ctx must be empty, either freshly initialised with mbedtls_pk_init() or reset with mbedtls_pk_free().

Your pseudo-code calls mbedtls_pk_setup on pctx. Perhaps this is the problem?

Can you check with other converters such as https://superdry.apphb.com/tools/online-rsa-key-converter to see if they can parse your PEM?

于 2017-04-20T19:21:18.177 回答