c++ - 如何在没有 p、q 等的情况下加载 RSA key 对

标签 c++ c openssl

我正在尝试找到一种仅使用 n、e、d 将 RSA key 对加载到 Openssl 的方法。 根据 RSA 的 openssl 文档,这些组件(p、q 等)可以为 NULL,但我设法找到的唯一用于加载 key 的函数是 i2d_RSAPrivateKey/i2d_RSAPublicKey。不幸的是,这些函数仅适用于 DER 格式的 key 。

那么除了直接将它们复制到 RSA 结构中之外,还有什么方法可以加载我的 key (n, e, d) 吗?

最佳答案

... these functions work only with keys in DER format.

OpenSSL 的 app.c 具有该实用程序用于从文件加载 key 的代码(出于实用目的,文件或内存之间没有区别,因为您可以使用不同的 BIO)。其转载如下,并提供多种格式。

So is there any way to load my keys (n, e, d) except coping them directly into the RSA structure?

你的 key 是什么格式的?


EVP_PKEY *load_key(BIO *err, const char *file, int format, int maybe_stdin,
                   const char *pass, ENGINE *e, const char *key_descrip)
{
    BIO *key=NULL;
    EVP_PKEY *pkey=NULL;
    PW_CB_DATA cb_data;

    cb_data.password = pass;
    cb_data.prompt_info = file;

    if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE))
    {
        BIO_printf(err,"no keyfile specified\n");
        goto end;
    }
#ifndef OPENSSL_NO_ENGINE
    if (format == FORMAT_ENGINE)
    {
        if (!e)
            BIO_printf(err,"no engine specified\n");
        else
        {
            pkey = ENGINE_load_private_key(e, file,
                                           ui_method, &cb_data);
            if (!pkey)
            {
                BIO_printf(err,"cannot load %s from engine\n",key_descrip);
                ERR_print_errors(err);
            }
        }
        goto end;
    }
#endif
    key=BIO_new(BIO_s_file());
    if (key == NULL)
    {
        ERR_print_errors(err);
        goto end;
    }
    if (file == NULL && maybe_stdin)
    {
#ifdef _IONBF
# ifndef OPENSSL_NO_SETVBUF_IONBF
        setvbuf(stdin, NULL, _IONBF, 0);
# endif /* ndef OPENSSL_NO_SETVBUF_IONBF */
#endif
        BIO_set_fp(key,stdin,BIO_NOCLOSE);
    }
    else
        if (BIO_read_filename(key,file) <= 0)
        {
            BIO_printf(err, "Error opening %s %s\n",
                       key_descrip, file);
            ERR_print_errors(err);
            goto end;
        }
    if (format == FORMAT_ASN1)
    {
        pkey=d2i_PrivateKey_bio(key, NULL);
    }
    else if (format == FORMAT_PEM)
    {
        pkey=PEM_read_bio_PrivateKey(key,NULL,
                                     (pem_password_cb *)password_callback, &cb_data);
    }
#if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
    else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC)
        pkey = load_netscape_key(err, key, file, key_descrip, format);
#endif
    else if (format == FORMAT_PKCS12)
    {
        if (!load_pkcs12(err, key, key_descrip,
                         (pem_password_cb *)password_callback, &cb_data,
                         &pkey, NULL, NULL))
            goto end;
    }
#if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA) && !defined (OPENSSL_NO_RC4)
    else if (format == FORMAT_MSBLOB)
        pkey = b2i_PrivateKey_bio(key);
    else if (format == FORMAT_PVK)
        pkey = b2i_PVK_bio(key, (pem_password_cb *)password_callback,
                           &cb_data);
#endif
    else
    {
        BIO_printf(err,"bad input format specified for key file\n");
        goto end;
    }
end:
    if (key != NULL) BIO_free(key);
    if (pkey == NULL)
    {
        BIO_printf(err,"unable to load %s\n", key_descrip);
        ERR_print_errors(err);
    }
    return(pkey);
}

关于c++ - 如何在没有 p、q 等的情况下加载 RSA key 对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18829485/

相关文章:

C 语言 - 将字符串参数插入 sqlite 数据库 %s 不起作用

c++ - 如何使 OpenSSL 使用 http 而不是 https?

github - 找不到 OpenSSL,尝试设置 OpenSSL 根文件夹的路径

c++ - 两个 vector 的并集

C++ 提升信号 2

在 C 中检查越界

c - 在c中找到一组数字的范围

c++ - 如何将动态矩阵复制到 CUDA 中的设备内存?

c++ - 当对象类型是模板参数时,有没有办法将模板参数传递给对象上的函数?

c++ - 使用 AES-256-CBC 模式 openssl 的数据加密,不会返回不需要填充的相同大小的数据吗?