foobarfuzzbizz
foobarfuzzbizz

Reputation: 58657

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?

Upvotes: 3

Views: 1683

Answers (2)

poupou
poupou

Reputation: 43553

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);
}

Upvotes: 1

Jonas Elfström
Jonas Elfström

Reputation: 31428

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.".

Upvotes: 3

Related Questions