Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is not a means to seed the <code>RNGCryptoServiceProvider</code>. One solution to generating deterministic values for debugging is to derive your own class that implements from <code>System.Security.Cryptography.RandomNumberGenerator</code> (the base class for <code>RNGCryptoServiceProvider</code>):</p> <pre><code>class DeterministicRandomGenerator : System.Security.Cryptography.RandomNumberGenerator { Random r = new Random(0); public override void GetBytes(byte[] data) { r.NextBytes(data); } public override void GetNonZeroBytes(byte[] data) { // simple implementation for (int i = 0; i &lt; data.Length; i++) data[i] = (byte)r.Next(1, 256); } } </code></pre> <p>Note the implementation uses the standard <code>Random</code> implementation with a seed of 0, ensuring deterministic results. Now you can use this class in place of <code>RNGCryptoServiceProvider</code> for debugging purposes:</p> <pre><code> RandomNumberGenerator rngCsp = #if DEBUG new DeterministicRandomGenerator(); // get deterministic values if debugging #else new RNGCryptoServiceProvider(); // otherwise, use CryptoRNG #endif byte[] randomBytes = new byte[20]; rngCsp.GetBytes(randomBytes); MessageBox.Show(Convert.ToBase64String(randomBytes)); </code></pre> <p><strong>Edited to add</strong></p> <blockquote> <p>I wonder if anybody knows exactly what I could do with my CPU to reset the RNGCryptoServiceProvider environment and trick RNGCryptoServiceProvider into using a prior seed</p> </blockquote> <p>Internally, <code>RNGCryptoServiceProvider</code> calls the Win32 <a href="http://msdn.microsoft.com/en-US/aa379942" rel="noreferrer"><code>CryptGenRandom</code></a> function to fill the buffer with cryptographically-random values <a href="http://msdn.microsoft.com/en-us/magazine/cc163367.aspx" rel="noreferrer">(Source and additional information)</a>. It is not based on a single seed. (Although the Win32 API allows the caller to provide a seed with supplemental random data, the .NET API does not expose this functionality. The purpose of a seed in this context is to provide additional entropy the application has access to, rather than to force a deterministic sequence.) The <code>CryptGenRandom</code> documentation states that:</p> <blockquote> <p>To form the seed for the random number generator, a calling application supplies bits it might have—for instance, mouse or keyboard timing input—that are then combined with both the stored seed and various system data and user data such as the process ID and thread ID, the system clock, the system time, the system counter, memory status, free disk clusters, the hashed user environment block. This result is used to seed the pseudorandom number generator (PRNG). In Windows Vista with Service Pack 1 (SP1) and later, an implementation of the AES counter-mode based PRNG specified in NIST Special Publication 800-90 is used. In Windows Vista, Windows Storage Server 2003, and Windows XP, the PRNG specified in Federal Information Processing Standard (FIPS) 186-2 is used.</p> </blockquote> <p>The result is that "resetting" the <code>RNGCryptoServiceProvider</code> to coerce it into repeating a former sequence is, by design, not a practical approach.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload