gpt4 book ai didi

openssl - 在 C++Builder 中使用 OpenSSL

转载 作者:行者123 更新时间:2023-12-01 22:50:12 27 4
gpt4 key购买 nike

我正在 C++Builder 10 Seattle 中编译一个应用程序,并尝试使用 OpenSSL 进行 RSA 工作。

我遵循了本教程:

How to Use OpenSSL to Generate RSA Keys in C/C++

这是代码:

#include <stdio.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>

bool generate_key()
{
int ret = 0;
RSA *r = NULL;
BIGNUM *bne = NULL;
BIO *bp_public = NULL, *bp_private = NULL;

int bits = 2048;
unsigned long e = RSA_F4;

// 1. generate rsa key
bne = BN_new();
ret = BN_set_word(bne,e);
if(ret != 1){
goto free_all;
}

r = RSA_new();
ret = RSA_generate_key_ex(r, bits, bne, NULL);
if(ret != 1){
goto free_all;
}

// 2. save public key
bp_public = BIO_new_file("public.pem", "w+");
ret = PEM_write_bio_RSAPublicKey(bp_public, r);
if(ret != 1){
goto free_all;
}

// 3. save private key
bp_private = BIO_new_file("private.pem", "w+");
ret = PEM_write_bio_RSAPrivateKey(bp_private, r, NULL, NULL, 0, NULL, NULL);

// 4. free
free_all:

BIO_free_all(bp_public);
BIO_free_all(bp_private);
RSA_free(r);
BN_free(bne);

return (ret == 1);
}

int main(int argc, char* argv[])
{
generate_key();
return 0;
}

当我将 libeay32.libssleay32.lib 添加到我的项目时,我收到一条错误消息:

[ilink32 Error] Error: 'C:\USERS\ERICWANG\DESKTOP\TESTOPENSSL2\LIB\LIBEAY32.LIB' contains invalid OMF record, type 0x21 (possibly COFF)

我见过一些技巧,例如使用 coff2omfimplib 工具,但两者都不起作用。

  1. 我使用coff2omf.exe来转换libeay32.lib。我将 coff2omf.exelibeay32.lib 放在同一文件夹中,并输入以下命令:

    coff2omf libeay32.lib Blibeay32.lib

    它说:

    ERROR: COFF error: libeay32.lib : invalid machine type detected

  2. 我尝试使用 implib.exelibeay32.lib 转换为 .dll 文件。我输入了这个命令:

    implib libeay32.lib Blibeay32.dll

    它说:

    Error : unable to open file

    我的 libeay32.lib 将其大小更改为 1KB 文件。这意味着该文件是错误的。

最佳答案

OpenSSL 在 C++Builder 中工作得很好。我在自己的 C++Builder 应用程序中使用它。

您不能使用.lib预编译的 OpenSSL DLL 中包含的文件。那些.lib文件适用于 Visual Studio。

在C++Builder中,需要使用C++Builder的implibmkexp用于从 DLL 创建与 C++Builder 兼容的导入库的命令行实用程序。使用implib对于 32 位 DLL 和 mkexp对于 64 位 DLL。

它工作得很好,当新的 OpenSSL 版本发布时,我已经使用这种技术很多年了。

您的implib命令错误。您无法“libeay32.lib 转换为 .dll 文件”。第一个参数是输出参数,第二个参数是输入参数。您需要创建一个.lib 现有 DLL 的文件。没有Blibeay32.dll文件,这就是您收到“无法打开文件”错误的原因。删除 B并使用正确的 DLL 文件名:

implib libeay32.lib libeay32.dll
implib ssleay32.lib ssleay32.dll

mkexp libeay32.a libeay32.dll
mkexp ssleay32.a ssleay32.dll

这将创建 .lib.a包含从 libeay32.dll 导入函数的引用的文件和ssleay32.dll ,分别。

关于openssl - 在 C++Builder 中使用 OpenSSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38582334/

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