在C ++ Builder中使用OpenSSL [英] Use OpenSSL in C++Builder

查看:401
本文介绍了在C ++ Builder中使用OpenSSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm compiling an app in C++Builder 10 Seattle, and trying to use OpenSSL for RSA work.

我遵循了本教程:

如何使用OpenSSL生成RSA密钥在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时,出现错误消息:

When I added libeay32.lib and ssleay32.lib to my project, I got an error message:

[ilink32错误]错误:"C:\ USERS \ ERICWANG \ DESKTOP \ TESTOPENSSL2 \ LIB \ LIBEAY32.LIB"包含无效的OMF记录,类型为0x21(可能为COFF)

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

我已经看到了一些使用coff2omfimplib工具的技巧,但是都没有用.

I've seen some tips like using coff2omf and implib tools, but both didn't work.

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

  1. I used coff2omf.exe to convert libeay32.lib. I put coff2omf.exe and libeay32.lib in the same folder, and entered this command:

coff2omf libeay32.lib Blibeay32.lib

它说:

错误:COFF错误:libeay32.lib:检测到无效的机器类型

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

  • 我尝试使用implib.exelibeay32.lib转换为.dll文件.我输入了以下命令:

  • I tried to convert libeay32.lib to a .dll file using implib.exe. I entered this command:

    implib libeay32.lib Blibeay32.dll
    

    它说:

    错误:无法打开文件

    Error : unable to open file

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

    And my libeay32.lib change its size to 1KB file. Which means the file was wrong.

    推荐答案

    OpenSSL在C ++ Builder中可以正常工作.我在自己的C ++ Builder应用程序中使用它.

    OpenSSL works just fine in C++Builder. I use it in my own C++Builder apps.

    您不能使用预编译的OpenSSL DLL附带的.lib文件.这些.lib文件用于Visual Studio.

    You cannot use the .lib files that are included with the pre-compiled OpenSSL DLLs. Those .lib files are meant for Visual Studio.

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

    In C++Builder, you need to use C++Builder's implib or mkexp command-line utility to create C++Builder-compatible import libs from the DLLs. Use implib for 32bit DLLs and mkexp for 64bit DLLs.

    效果很好,当新的OpenSSL版本发布时,我已经使用这种技术很多年了.

    It works fine, I have been using this technique for years when new OpenSSL versions are released.

    您的implib命令错误.您不能"libeay32.lib转换为.dll文件".第一个参数是 output 参数,第二个参数是 input 参数.您需要创建用于现有 DLL的.lib文件.没有Blibeay32.dll文件,这就是为什么出现"无法打开文件"错误的原因.删除B并使用正确的DLL文件名:

    Your implib command is wrong. You cannot "convert libeay32.lib to a .dll file". The first parameter is an output parameter, the second parameter is an input parameter. You need to create a .lib file for an existing DLL. There is no Blibeay32.dll file, which is why you are getting the "unable to open file" error. Drop the B and use the correct DLL filenames:

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

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

    这将创建.lib.a文件,其中包含分别从libeay32.dllssleay32.dll导入功能的引用.

    This will create .lib or .a files containing references for importing the functions from libeay32.dll and ssleay32.dll, respectively.

    这篇关于在C ++ Builder中使用OpenSSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆