C++ OpenSSL 导出私钥
到目前为止,我成功地使用了 SSL,但遇到了令人困惑的障碍。我生成了 RSA 密钥对,之前使用 PEM_write_bio_RSAPrivateKey(...) 来导出它们。然而,手册页声称该格式已经过时(实际上它看起来与通常的 PEM 格式不同)。相反,它建议使用 PEM_write_bio_PKCS8PrivateKey(...)。
但是 PEM_write_bio_PKCS8PrivateKey 接受 EVP_PKEY 对象。如何将 RSA* 密钥对转换为 EVP_PKEY* 结构以供该函数使用?
EVP_PKEY* evpkey = EVP_PKEY_new();
if (!EVP_PKEY_assign_RSA(evpkey, keypair))
throw ReadError();
int ret = PEM_write_bio_PKCS8PrivateKey(bio, evpkey, EVP_aes_256_cbc(),
pass_char_str, pass_len, NULL, NULL);
ret 始终为 0。使用旧的 PEM_write_bio_RSAPrivateKey 对我有用。我希望导出我的密钥,以便它们看起来像:
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCvdbGZes3N/v3EqbbwYHW4rr4Wgav9eD36kVD7Hn5LIKwhfAqd
...
-----END RSA PRIVATE KEY-----
谢谢。
I'm using SSL successfully so far, but have come up against a confusing roadblock. I generate an RSA keypair, and previously was using PEM_write_bio_RSAPrivateKey(...) to export them. However the man pages claim that format is outdated (and indeed it looks different to the usual PEM format). Instead it recommends PEM_write_bio_PKCS8PrivateKey(...).
However PEM_write_bio_PKCS8PrivateKey accepts an EVP_PKEY object. How can I convert my RSA* keypair into an EVP_PKEY* structure for usage in that function?
EVP_PKEY* evpkey = EVP_PKEY_new();
if (!EVP_PKEY_assign_RSA(evpkey, keypair))
throw ReadError();
int ret = PEM_write_bio_PKCS8PrivateKey(bio, evpkey, EVP_aes_256_cbc(),
pass_char_str, pass_len, NULL, NULL);
ret is always 0. Using the older PEM_write_bio_RSAPrivateKey works for me. I'm looking to export my keys so they look like:
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCvdbGZes3N/v3EqbbwYHW4rr4Wgav9eD36kVD7Hn5LIKwhfAqd
...
-----END RSA PRIVATE KEY-----
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅此处的论坛帖子(下面的片段):
完整源代码:
See this forum posting here (snippet below):
Full Source: