gpt4 book ai didi

c++ - mbedTLS pk 解析错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:30:28 25 4
gpt4 key购买 nike

谁能帮我找出为什么我在尝试从 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
}

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

最佳答案

docs for mbedtls_pk_parse_public_key说:

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

您的伪代码调用 pctx 上的 mbedtls_pk_setup。也许这就是问题所在?

您可以检查其他转换器,例如 https://superdry.apphb.com/tools/online-rsa-key-converter看看他们是否可以解析您的 PEM?

关于c++ - mbedTLS pk 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43453159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com