how to generate random int value using System.Security.Cryptography
Asked Answered
M

3

6

i want to generate random int values using System.Security.Cryptography from 0 to 26 how can i do that ? i know can use system.random to do this but i want to use System.Security.Cryptography

Mentholated answered 29/7, 2016 at 23:17 Comment(0)
H
8

You can use this to generate a random int from a Crypto RNG. However, I'd be hard pressed to explain a scenario, outside of cryptography, where such a tool is useful.

RNGCryptoServiceProvider CprytoRNG = new RNGCryptoServiceProvider();

// Return a random integer between a min and max value.
int RandomIntFromRNG(int min, int max)
{
    // Generate four random bytes
    byte[] four_bytes = new byte[4];
    CprytoRNG.GetBytes(four_bytes);

    // Convert the bytes to a UInt32
    UInt32 scale = BitConverter.ToUInt32(four_bytes, 0);

    // And use that to pick a random number >= min and < max
    return (int)(min + (max - min) * (scale / (uint.MaxValue + 1.0)));
}
Hypertrophy answered 30/7, 2016 at 0:19 Comment(2)
Aside from the typo in CprytoRNG ("crytpo" is my usual offender) the preferred way is to use RandomNumberGenerator rng = RandomNumberGenerator.Create() instead of depending on the RNGCryptoServiceProvider type directly (unless you're concerned with the factory having been repointed to a non-CSPRNG via CryptoConfig). RandomNumberGenerator.Create() exists on .NET Core, but the RNGCryptoServiceProvider class does not.Archducal
Unit tested and found values 0,9 never generates 9 due to dotnetperls.com/cast-int Ended up with the following code: (int)Math.Round(min + (max - min) * (scale / (uint.MaxValue + 1.0)), 0, MidpointRounding.AwayFromZero)Eponymous
G
0
byte[] four_bytes = new byte[4];             

System.Security.Cryptography.RandomNumberGenerator.Create().GetBytes(four_bytes);

uint rand = BitConverter.ToUInt32(four_bytes, 0);

//range is 0 to 2^32-1, divide some number to limit it to the range you want
Gabbie answered 8/8, 2022 at 17:36 Comment(1)
Since the desire is for secure random numbers, that last step that you've only got as a comment is critical.Hustings
A
-2

Then you should use RandomNumberGenerator class for that purpose. But per what you have posted from 0 to 26 I believe you should be using Random class for that purpose.

For your specific purpose to generate a cryptographic strong random numbers you should use RNGCryptoServiceProvider class which inherits from RandomNumberGenerator class and provides mechanism for generating cryptographic random numbers instead of using RandomNumberGenerator class directly.

You can see an example in the linked MSDN documentation.

Amelia answered 29/7, 2016 at 23:30 Comment(3)
@ArtjomB., it is using one of the provider class RNGCryptoServiceProvider . See edited answer. But yes it's as straight forward as using Random class.Amelia
Yes, the example is the kicker, but you should describe in general how it is done.Restate
Poster should not use Random() because he wants to generate a number fom 0 to 26. The range does not dictate the methodology, the use does. If he needs a highly random value in that range he should use System.Security.Cryptography to do it. Anon Coward's reply is a good example to work with.Phaeton

© 2022 - 2024 — McMap. All rights reserved.