
#use wml::openssl-macros area=docs page=RSA_public_encrypt

<title>Documents, RSA_public_encrypt(3)</title>

<h1>RSA_public_encrypt(3)</h1>

#use wml::imp::generic

{:
## What's this? [[s|(<STRONG>[^<].+?)</A>(</STRONG><DD>)|$1$2|sg]]
[[s|<P>\s+<P>|<P>|sg]]
[[s|<P>\s+</|</|sg]]
[[s|<DD>\s*<DT>|<DD>&nbsp;<DT>|sg]]
[[s|<DD>\s*</DL>|<DD>&nbsp;</DL>|sg]]
[[s|\[|&#91;|sg]]
[[s|\]|&#93;|sg]]

<!-- INDEX BEGIN -->

<UL>

	<LI><A HREF="#NAME">NAME</A>
	<LI><A HREF="#SYNOPSIS">SYNOPSIS</A>
	<LI><A HREF="#DESCRIPTION">DESCRIPTION</A>
	<LI><A HREF="#RETURN_VALUES">RETURN VALUES</A>
	<LI><A HREF="#CONFORMING_TO">CONFORMING TO</A>
	<LI><A HREF="#SEE_ALSO">SEE ALSO</A>
	<LI><A HREF="#HISTORY">HISTORY</A>
</UL>
<!-- INDEX END -->

<HR>
<P>
<HR>
<H1><A NAME="NAME">NAME</A></H1>
<P>
RSA_public_encrypt, RSA_private_decrypt - RSA public key cryptography

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/rsa.h&gt;
</PRE>
<PRE> int RSA_public_encrypt(int flen, unsigned char *from,
    unsigned char *to, RSA *rsa, int padding);
</PRE>
<PRE> int RSA_private_decrypt(int flen, unsigned char *from,
     unsigned char *to, RSA *rsa, int padding);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
<CODE>RSA_public_encrypt()</CODE> encrypts the <STRONG>flen</STRONG> bytes at <STRONG>from</STRONG> (usually a session key) using the public key <STRONG>rsa</STRONG> and stores the ciphertext in
<STRONG>to</STRONG>. <STRONG>to</STRONG> must point to <CODE>RSA_size(</CODE><STRONG>rsa</STRONG>) bytes of memory.

</P>
<P>
<STRONG>padding</STRONG> denotes one of the following modes:

</P>
<DL>
<DT><STRONG><A NAME="item_RSA_PKCS1_PADDING">RSA_PKCS1_PADDING</A></STRONG><DD>
<P>
PKCS #1 v1.5 padding. This currently is the most widely used mode.

</P>
<DT><STRONG><A NAME="item_RSA_PKCS1_OAEP_PADDING">RSA_PKCS1_OAEP_PADDING</A></STRONG><DD>
<P>
EME-OAEP as defined in PKCS #1 v2.0 with SHA-1, MGF1 and an empty encoding
parameter. This mode is recommended for all new applications.

</P>
<DT><STRONG><A NAME="item_RSA_SSLV23_PADDING">RSA_SSLV23_PADDING</A></STRONG><DD>
<P>
PKCS #1 v1.5 padding with an SSL-specific modification that denotes that
the server is SSL3 capable.

</P>
<DT><STRONG><A NAME="item_RSA_NO_PADDING">RSA_NO_PADDING</A></STRONG><DD>
<P>
Raw RSA encryption. This mode should <EM>only</EM> be used to implement cryptographically sound padding modes in the
application code. Encrypting user data directly with RSA is insecure.

</P>
</DL>
<P>
<STRONG>flen</STRONG> must be less than <CODE>RSA_size(</CODE><STRONG>rsa</STRONG>) - 11 for the PKCS #1 v1.5 based padding modes, less than
<CODE>RSA_size(</CODE><STRONG>rsa</STRONG>) - 41 for RSA_PKCS1_OAEP_PADDING and exactly <CODE>RSA_size(</CODE><STRONG>rsa</STRONG>) for RSA_NO_PADDING. The random number generator must be seeded prior to
calling <CODE>RSA_public_encrypt().</CODE>

</P>
<P>
<CODE>RSA_private_decrypt()</CODE> decrypts the <STRONG>flen</STRONG> bytes at <STRONG>from</STRONG> using the private key <STRONG>rsa</STRONG> and stores the plaintext in <STRONG>to</STRONG>. <STRONG>to</STRONG> must point to a memory section large enough to hold the decrypted data
(which is smaller than <CODE>RSA_size(</CODE><STRONG>rsa</STRONG>)). <STRONG>padding</STRONG> is the padding mode that was used to encrypt the data.

</P>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
<CODE>RSA_public_encrypt()</CODE> returns the size of the encrypted data
(i.e., <CODE>RSA_size(</CODE><STRONG>rsa</STRONG>)). <CODE>RSA_private_decrypt()</CODE> returns the size of the recovered
plaintext.

</P>
<P>
On error, -1 is returned; the error codes can be obtained by <A HREF="../crypto/ERR_get_error.html#">ERR_get_error(3)</A>.

</P>
<P>
<HR>
<H1><A NAME="CONFORMING_TO">CONFORMING TO</A></H1>
<P>
SSL, PKCS #1 v2.0

</P>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
<A HREF="../crypto/ERR_get_error.html#">ERR_get_error(3)</A>, <A HREF="../crypto/rand.html#">rand(3)</A>, <A HREF="../crypto/rsa.html#">rsa(3)</A>,
<A HREF="../crypto/RSA_size.html#">RSA_size(3)</A>



</P>
<P>
<HR>
<H1><A NAME="HISTORY">HISTORY</A></H1>
<P>
The <STRONG>padding</STRONG> argument was added in SSLeay 0.8. RSA_NO_PADDING is available since SSLeay
0.9.0, OAEP was added in OpenSSL 0.9.2b.

</P>
:}


