<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="Generator" content="Microsoft Exchange Server">
<!-- converted from text --><style><!-- .EmailQuote { margin-left: 1pt; padding-left: 4pt; border-left: #800000 2px solid; } --></style>
</head>
<body>
<meta content="text/html; charset=UTF-8">
<style type="text/css" style="">
<!--
p
        {margin-top:0;
        margin-bottom:0}
-->
</style>
<div dir="ltr">
<div id="x_divtagdefaultwrapper" dir="ltr" style="font-size:12pt; color:#000000; font-family:Calibri,Arial,Helvetica,sans-serif">
<p>Hi Matt,</p>
<p><br>
</p>
<p>I checked openssl source code. It seems that PKCS#1 is default padding mode.<br>
</p>
<p>For encrypting user data such as user's password, could I use PKCS#1 or OAEP padding mode?</p>
<p><br>
</p>
<p><br>
</p>
<div id="x_Signature">
<div id="x_divtagdefaultwrapper" style="font-size:12pt; color:#000000; background-color:#FFFFFF; font-family:Calibri,Arial,Helvetica,sans-serif">
<p>Thanks,</p>
<p>Jared, (ΤìÏ£©<br>
Software developer<br>
Interested in open source software, big data, Linux<br>
</p>
</div>
</div>
</div>
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="x_divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> openssl-users <openssl-users-bounces@openssl.org> on behalf of Matt Caswell <matt@openssl.org><br>
<b>Sent:</b> Thursday, March 23, 2017 6:05:43 PM<br>
<b>To:</b> openssl-users@openssl.org<br>
<b>Subject:</b> Re: [openssl-users] One question about RSA decrypt with private key</font>
<div> </div>
</div>
</div>
<font size="2"><span style="font-size:10pt;">
<div class="PlainText"><br>
<br>
On 23/03/17 05:29, Yu Wei wrote:<br>
> After commented out the line "EVP_PKEY_CTX_set_rsa_padding(ctx,<br>
> RSA_NO_PADDING)",  it worked well.<br>
> <br>
> <br>
> However, I still quite understand the usage of "RSA_NO_PADDING".<br>
> <br>
> <br>
> Who could kindly explain this?<br>
> <br>
<br>
RSA_NO_PADDING gives you "raw" RSA encryption. From the manual:<br>
<br>
    RSA_NO_PADDING<br>
    Raw RSA encryption. This mode should only be used to implement<br>
    cryptographically sound padding modes in the application code.<br>
    Encrypting user data directly with RSA is insecure.<br>
<br>
<a href="https://www.openssl.org/docs/man1.1.0/crypto/RSA_public_encrypt.html">https://www.openssl.org/docs/man1.1.0/crypto/RSA_public_encrypt.html</a><br>
<br>
Basically, unless you are implementing a new RSA padding mode, or really<br>
know what you are doing, don't use it.<br>
<br>
Matt<br>
<br>
<br>
> Thanks,<br>
> <br>
> Jared, (ΤìÏ£©<br>
> Software developer<br>
> Interested in open source software, big data, Linux<br>
> <br>
> ------------------------------------------------------------------------<br>
> *From:* openssl-users <openssl-users-bounces@openssl.org> on behalf of<br>
> Yu Wei <yu2003w@hotmail.com><br>
> *Sent:* Thursday, March 23, 2017 1:20:42 AM<br>
> *To:* openssl-users@openssl.org<br>
> *Subject:* [openssl-users] One question about RSA decrypt with private key<br>
>  <br>
> <br>
> Hi guys,<br>
> <br>
> <br>
> I generated RSA private key and public key as below,<br>
> <br>
> openssl genpkey -algorithm RSA -out key.pem -pkeyopt rsa_keygen_bits:2048<br>
> <br>
> openssl rsa -pubout -in pri.key -out pub.key<br>
> <br>
> <br>
> And encrypted text file as below,<br>
> <br>
> openssl pkeyutl -encrypt -pubin -inkey ~/pub.key -in ~/1.txt -out ~/1e.txt<br>
> <br>
> <br>
> Then I wrote below program to decrypt the encryted file. However, it<br>
> seemed that decrypt didn't work as  expected.<br>
> <br>
> <br>
> #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>
> 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>
> <br>
> <br>
> When executing the code, the result is as below,<br>
> <br>
> [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>
> <br>
> <br>
> Is there anything missed?<br>
> <br>
> <br>
> Thanks,<br>
> <br>
> Jared, (ΤìÏ£©<br>
> Software developer<br>
> Interested in open source software, big data, Linux<br>
> <br>
> <br>
> <br>
-- <br>
openssl-users mailing list<br>
To unsubscribe: <a href="https://mta.openssl.org/mailman/listinfo/openssl-users">
https://mta.openssl.org/mailman/listinfo/openssl-users</a><br>
</div>
</span></font>
</body>
</html>