SecureRandom in C #

Here is the java code:

SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed(someBytes);//someBytes is the seed 

Is there an equal method in C #? What I get is wrong:

 RandomNumberGenerator rng = RNGCryptoServiceProvider.Create(); rng.GetBytes(someBytes);// out someBytes 

I need seed, because java code did, I need to translate Java code in C #. When I pass the same seed, the sequence I get from C # should match java.

+6
source share
3 answers

The abstract System.Security.Cryptography.RandomNumberGenerator class and its specific implementations do not expose the method for setting the seed to the developer (although internally I suspect that they really use it.)

The rationale for the design was, I suspect, that repeatability does not create a “cryptographically strong” stream of random values.

If you look at a specific implementation, RNGCryptoServiceProvider , while it exposes a constructor that takes byte[] to presumably initialize PRNG, its documentation says

This value is ignored.

And in the comments it says:

This method does not directly initialize the RNGCryptoServiceProvider class. Calling this method is equivalent to calling the RNGCryptoServiceProvider constructor and passing null .

For information about what is used in the seed used, see the MSDN documentation for CryptGenRandom

+8
source

According to the MSDN RNGCryptoServiceProvider for RNGCryptoServiceProvider , there seems to be no way to manually seed it with values. There are constructors that accept byte[] and string , but both of these arguments are ignored.

This does not matter, because any random number generator that has weight in salt will correctly sow when created. Any value you provide is unlikely to be better than an internal sowing mechanism (this is probably a high resolution time value).

+6
source

The RNGCryptoServiceProvider class RNGCryptoServiceProvider not need to be loaded manually.

0
source

All Articles