How to Generate a Cryptographically Secure Pseudorandom Number in C#

How can I generate a cryptographically secure pseudorandom number in C#?

using System.Security.Cryptography;
...
using(RandomNumberGenerator rng = new RNGCryptoServiceProvider())
{
byte[] tokenData = new byte[32];
rng.GetBytes(tokenData);

string token = Convert.ToBase64String(tokenData);
}

How can I generate a cryptographically secure random integer within a range?

You can have a look at the CryptoRandom class taken from niik/CryptoRandom.cs which is the original version by Stephen Toub and Shawn Farkas. In this class they implement several random generators that seem to be cryptographically secure.

I have used the following version in my projects for random integer generation.

public class RandomGenerator
{
readonly RNGCryptoServiceProvider csp;

public RandomGenerator()
{
csp = new RNGCryptoServiceProvider();
}

public int Next(int minValue, int maxExclusiveValue)
{
if (minValue >= maxExclusiveValue)
throw new ArgumentOutOfRangeException("minValue must be lower than maxExclusiveValue");

long diff = (long)maxExclusiveValue - minValue;
long upperBound = uint.MaxValue / diff * diff;

uint ui;
do
{
ui = GetRandomUInt();
} while (ui >= upperBound);
return (int)(minValue + (ui % diff));
}

private uint GetRandomUInt()
{
var randomBytes = GenerateRandomBytes(sizeof(uint));
return BitConverter.ToUInt32(randomBytes, 0);
}

private byte[] GenerateRandomBytes(int bytesNumber)
{
byte[] buffer = new byte[bytesNumber];
csp.GetBytes(buffer);
return buffer;
}
}

How can I generate a cryptographically secure pseudorandom number in C#?

using System.Security.Cryptography;
...
using(RandomNumberGenerator rng = new RNGCryptoServiceProvider())
{
byte[] tokenData = new byte[32];
rng.GetBytes(tokenData);

string token = Convert.ToBase64String(tokenData);
}

Alternative Cryptographic Secure Pseudo Random Number Generator for C#

There are two random number generators in Bouncy Castle - C# version that may be of use: DigestRandomGenerator and VmpcRandomGenerator, both in the crypto.prng name space. The trick is to seed those random entropy from a source you can trust.

Note that I am unfamiliar with the VMPC algorithm. The DigestRandomGenerator largely depends on the security of the underlying hash function (SHA-512 is pretty secure and fast on 64 bit hardware). It is a rather simple implementation, but it should be secure none-the-less.

Generating Salts Via Cryptographically Secure Pseudo-Random Numbers Using RNGCryptoServiceProvider

A salt consists of bytes. These bytes can be used as input for e.g. the PBKDF2 function to derive a password hash. If you want to convert the salt to characters, use base 64 encoding. Please do not create your own scheme for this. 8 bytes is about the minimum size for a salt, 16 is plenty and 64 is quite over the top.

How to generate a cryptographically secure Double between 0 and 1?

It appears to me that the solutions so far will have uneven distribution due to taking the inverse. For an even distribution I'd think you want something like this.

// Step 1: fill an array with 8 random bytes
var rng = new RNGCryptoServiceProvider();
var bytes = new Byte[8];
rng.GetBytes(bytes);
// Step 2: bit-shift 11 and 53 based on double's mantissa bits
var ul = BitConverter.ToUInt64(bytes, 0) / (1 << 11);
Double d = ul / (Double)(1UL << 53);

Note that you can't just divide the UInt64 into UInt64.MaxValue, because a double doesn't have enough bits, and there's no way to get unique outputs for all your inputs. So you can/must throw some bits away.

Why use the C# class System.Random at all instead of System.Security.Cryptography.RandomNumberGenerator?

Speed and intent. If you're generating a random number and have no need for security, why use a slow crypto function? You don't need security, so why make someone else think that the number may be used for something secure when it won't be?

Cryptographically Secure Pseudo-Random Number Generator in Qt/C++ (Cross platform)

If you think you need a cryptographically secure PRNG for generating salts then I must tell you that you do not understand what the salt is and how and why it works and against which kinds of attacks it is useful.

The simple fact that the salt must be stored in plaintext alongside the hash of the salted password should have given away that you do not really need a cryptographically secure PRNG for salt - or any PRNG for that matter. Frankly, you could have a simple 64-bit number, which you increase by one every time you need a new salt and it would be just as secure as a salt generated by a cryptographically secure PRNG.

Fast pseudorandom number generator for cryptography in C

ISAAC (http://www.burtleburtle.net/bob/rand/isaacafa.html) is probably one of the fastest cryptographically secure PRNGs (code at site). Another approach is to use a block cipher in counter mode. Something like TwoFish, which is reasonably fast and freely available, would be effective.

If you don't need a lot of numbers, all modern operating systems have built-in RNGs suitable for cryptographic use, though they typically can't produce lots of numbers because they rely on accumulating entropy from sources like input timings. Unix-like systems (Linux, OSX) have /dev/random, Windows has CryptGenRandom. Even if these aren't suitable for your needs, you probably should use them to seed the PRNG you do end up using.



Related Topics



Leave a reply



Submit