【问题标题】:Dumping public key from private using libopenssl使用 libopenssl 从私有转储公钥
【发布时间】:2013-04-10 17:37:21
【问题描述】:

假设我想在我的 c++ 应用程序中使用类似 openssl 命令的代码。

openssl rsa -in private.pem -pubout -outform der -out ./out.pub

我该怎么做?

我在 github 上找了一个示例并想出了以下方案。

key  = PEM_read_bio_RSAPrivateKey(bio, NULL, 0, NULL);
len = i2d_RSAPublicKey(key, &bufp);

它返回的值与我从命令行工具获得的值不同。我想没有从私钥到公钥的转换,它只是保存了私钥。谁能告诉我使用 openssl lib 从私有获取 pub 密钥的正确方法。我也非常感谢任何指向 openssl 的 pub\priv 密钥示例的链接。

【问题讨论】:

  • 或许有一个程序可以在某处查看源代码,可以进行这种转换...

标签: c++ openssl rsa private-key public-key


【解决方案1】:

最后,我刚刚在 openssl 本身中找到了合适的来源。这正是在

期间发生的事情

openssl rsa -in private.pem -pubout -outform der -out ./out.pub

我已经从原始代码中删除了很多检查以使其更小。

#include <openssl/pem.h>
#include <openssl/x509.h>

EVP_PKEY *load_key(const char *file)
{
    BIO *key=NULL;
    EVP_PKEY *pkey=NULL;

    key=BIO_new(BIO_s_file());
    BIO_read_filename(key,file);
    pkey=PEM_read_bio_PrivateKey(key,NULL,NULL,NULL);

    return pkey;
}

int _tmain(int argc, _TCHAR* argv[])
{
    BIO *out=NULL;
    out=BIO_new(BIO_s_file());
    EVP_PKEY    *pkey;
    RSA *rsa=NULL;

    char *infile = path_to_pem;
    char *outfile = path_to_der;

    pkey = load_key(infile);

    if (pkey != NULL)
        rsa = EVP_PKEY_get1_RSA(pkey);
    EVP_PKEY_free(pkey);

    BIO_write_filename(out,outfile);

    i2d_RSA_PUBKEY_bio(out,rsa);
}

【讨论】:

    猜你喜欢
    • 2015-04-12
    • 1970-01-01
    • 2019-12-13
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多