<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Arial,Helvetica,sans-serif;" dir="ltr">
<p></p>
<p>Hi guys,</p>
<p><br>
</p>
<p>I generated RSA private key and public key as below,</p>
<p><span>openssl genpkey -algorithm RSA -out key.pem -pkeyopt rsa_keygen_bits:2048</span></p>
<p><span>openssl rsa -pubout -in pri.key -out pub.key</span></p>
<p><br>
</p>
<p>And encrypted text file as below,</p>
<p><span>openssl pkeyutl -encrypt -pubin -inkey ~/pub.key -in ~/1.txt -out ~/1e.txt</span></p>
<p><br>
</p>
<p>Then I wrote below program to decrypt the encryted file. However, it didn't work as  expected.</p>
<p><br>
</p>
<div>#include <openssl/evp.h><br>
#include <openssl/rsa.h><br>
#include <openssl/pem.h><br>
#include <openssl/err.h><br>
#include <openssl/conf.h><br>
#include <iostream><br>
<br>
using namespace std;<br>
<br>
void<br>
cleanup()<br>
{<br>
    EVP_cleanup();<br>
    CRYPTO_cleanup_all_ex_data();<br>
    ERR_free_strings();<br>
}<br>
<br>
int<br>
main(int argc, char** argv)<br>
{<br>
    ERR_load_crypto_strings();<br>
    OpenSSL_add_all_algorithms();<br>
    OPENSSL_config(nullptr);<br>
<br>
    cout<<"Initialize crypto library done"<<endl;<br>
<br>
    EVP_PKEY * key = EVP_PKEY_new();<br>
    if (key == nullptr) {<br>
        cout<<"Failed to contruct new key"<<endl;<br>
        return 1;<br>
    }<br>
    FILE * fpri = nullptr;<br>
    fpri = fopen("/home/stack/pri.key", "r");<br>
    if (fpri == nullptr) {<br>
        cout<<"Failed to load private key"<<endl;<br>
        return 1;<br>
    }<br>
    key = PEM_read_PrivateKey(fpri, &key, nullptr, nullptr);<br>
    if (key == nullptr) {<br>
        std::cout<<"Read private key failed"<<endl;<br>
        return 1;<br>
    }<br>
</div>
<div>    cout<<"load private key successfully"<<endl;<br>
    EVP_PKEY_CTX *ctx = nullptr;<br>
    ctx = EVP_PKEY_CTX_new(key, nullptr);<br>
    EVP_PKEY_decrypt_init(ctx);<br>
    EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING);<br>
<br>
    size_t outlen = 0, inlen = 0;<br>
    unsigned char * out = nullptr, * in = nullptr;<br>
<br>
    char buf[1024];<br>
    FILE * fe = nullptr;<br>
    fe = fopen("/home/stack/1e.txt", "r");<br>
    size_t len = fread(buf, 1, sizeof(buf),  fe);<br>
    cout<<"data input length is "<<len<<endl;<br>
    EVP_PKEY_decrypt(ctx, NULL, &outlen, in, inlen);<br>
    cout<<"outlen is "<<outlen<<endl;<br>
<br>
    out = (unsigned char*)OPENSSL_malloc(outlen);<br>
    EVP_PKEY_decrypt(ctx, out, &outlen, in, inlen);<br>
    cout<<"decrypted data "<<out<<endl;<br>
    cleanup();<br>
<br>
    return 0;<br>
<br>
}<br>
</div>
<p><br>
</p>
<p>When executing the code, the result is as below,</p>
<div>[stack@agent ~]$ ./test<br>
Initialize crypto library done<br>
load private key successfully<br>
data input length is 256<br>
outlen is 256<br>
decrypted data <br>
</div>
<p><br>
</p>
<p>Any advice?<br>
</p>
<p><br>
</p>
<p><br>
</p>
Thanks,
<p></p>
<div id="Signature">
<div id="divtagdefaultwrapper" style="font-size:12pt; color:#000000; background-color:#FFFFFF; font-family:Calibri,Arial,Helvetica,sans-serif">
<p>Jared, (ΤìÏ£©<br>
Software developer<br>
Interested in open source software, big data, Linux<br>
</p>
</div>
</div>
</div>
</body>
</html>