Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the C# RSACryptoServiceProvider Encrypt() method work?

I am curious since RSA is not a block cipher, yet the Encrypt() method can take an arbitrary amount of data to encrypt.

Does it use AES+RSA hybrid encryption? Or does it simply use RSA (incorrectly) as a block cipher?

like image 316
samoz Avatar asked Dec 17 '25 20:12

samoz


2 Answers

yet the Encrypt() method can take an arbitrary amount of data to encrypt

According to MSDN it can't

Maximum Length of rgb Parameter
Modulus size -2 -2*hLen, where hLen is the size of the hash.

It even has a CryptographicException that reads "The length of the rgb parameter is greater than the maximum allowed length.".

like image 99
Jonas Elfström Avatar answered Dec 19 '25 08:12

Jonas Elfström


Does it use AES+RSA hybrid encryption?

No it does not. If this is what you're looking for then you have to do it yourself or check my old blog entry on the subject.

Or does it simply use RSA (incorrectly) as a block cipher?

No it does not. It will apply a padding (either PKCS#1 1.5 or OAEP) to the supplied byte[] and encrypt it. As such is does have length limitations (as others already pointed out).

Here's how it looks like (from Mono's BCL source code).

public byte[] Encrypt (byte[] rgb, bool fOAEP) 
{
    // choose between OAEP or PKCS#1 v.1.5 padding
    AsymmetricKeyExchangeFormatter fmt = null;
    if (fOAEP)
        fmt = new RSAOAEPKeyExchangeFormatter (rsa);
    else
        fmt = new RSAPKCS1KeyExchangeFormatter (rsa);

    return fmt.CreateKeyExchange (rgb);
}
like image 31
poupou Avatar answered Dec 19 '25 08:12

poupou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!