Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there any fast implementation of cryptographically secure pseudorandom number generator (CSPRNG) for C# 3.0 (.NET Framework 3.5), for authentication tokens?

like image 462
Alon Gubkin Avatar asked Nov 03 '09 16:11

Alon Gubkin


People also ask

How do we generate cryptographically secure pseudorandom numbers?

A secure block cipher can be converted into a CSPRNG by running it in counter mode. This is done by choosing a random key and encrypting a 0, then encrypting a 1, then encrypting a 2, etc. The counter can also be started at an arbitrary number other than zero.

What module can be used to generate pseudo random numbers that are cryptographically secure?

The secrets module is used for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets.

What makes a random number generator cryptographically secure?

A PRNG is said to be cryptographically secure if, assuming that it operates over a wide enough unknown n-bit key, its output is computationally indistinguishable from uniformly random bits.

What generates pseudorandom integer?

The seed() function will seed the pseudorandom number generator, taking an integer value as an argument, such as 1 or 7.


1 Answers

using System.Security.Cryptography; ... using(RandomNumberGenerator rng = new RNGCryptoServiceProvider()) {     byte[] tokenData = new byte[32];     rng.GetBytes(tokenData);      string token = Convert.ToBase64String(tokenData); } 
like image 69
John Gietzen Avatar answered Oct 14 '22 17:10

John Gietzen