Tolga Evcimen
Tolga Evcimen

Reputation: 7352

How to get same result for same data with RSA encryption

I need an asymmetric encryption algorith which always encrypts the data with same result for same data. When I use RSACryptoServiceProvider it is always giving me different encrypted data for same input.

byte[] encrypted1, encrypted2; 

using ( var RSA1 = new RSACryptoServiceProvider() )
{    
    encrypted1 = RSA1.Encrypt(data, false);
    encrypted2 = RSA1.Encrypt(data, false); 
}

Here encrypted1 and encrypted2are different.

I heard this is caused by the random padding ability of new RSA implementation. But it happens even if I set padding to the false. So;

  1. Is there any way to change this behavior?
  2. Are there any other algorithms for this job? If there are what are them?

Upvotes: 1

Views: 1237

Answers (1)

Maarten Bodewes
Maarten Bodewes

Reputation: 93978

The boolean you are referring to is not meant to switch the padding on or off. It uses OAEP padding or PKCS#1 v1.5 padding (it should never have been a boolean in the first place, it should have been an enum value). Both padding mechanisms deploy (partially) randomized padding. As long as your random source is indeed random, the output won't be deterministic.

You can of course create your own padding, e.g. using the BlindedRSAEngine of Bouncy Castle, but if you remove the random padding, you invalidate the security of RSA. Only do this if you fully understand the implications, as for instance indicated here.

In other words: don't go there.

Upvotes: 1

Related Questions