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

<p>I followed this tutorial:</p>

<p><a href="http://www.codepool.biz/how-to-use-openssl-generate-rsa-keys-cc.html" rel="nofollow">How to Use OpenSSL to Generate RSA Keys in C/C++</a></p>

<p>Here is the code:</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;openssl/rsa.h&gt;
#include &lt;openssl/pem.h&gt;

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;
}
</code></pre>

<p>When I added <code>libeay32.lib</code> and <code>ssleay32.lib</code> to my project, I got an error message:</p>

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

<p>I've seen some tips like using <code>coff2omf</code> and <code>implib</code> tools, but both didn't work.</p>

<ol>
<li><p>I used <code>coff2omf.exe</code> to convert <code>libeay32.lib</code>. I put <code>coff2omf.exe</code> and <code>libeay32.lib</code> in the same folder, and entered this command:</p>

<pre><code>coff2omf libeay32.lib Blibeay32.lib
</code></pre>

<p>It said:</p>

<blockquote>
  <p>ERROR: COFF error: libeay32.lib  : invalid machine type detected</p>
</blockquote></li>
<li><p>I tried to convert <code>libeay32.lib</code> to a <code>.dll</code> file using <code>implib.exe</code>. I entered this command:</p>

<pre><code>implib libeay32.lib Blibeay32.dll
</code></pre>

<p>It said:</p>

<blockquote>
  <p>Error : unable to open file</p>
</blockquote>

<p>And my <code>libeay32.lib</code> change its size to 1KB file. Which means the file was wrong.</p></li>
</ol>