03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Randomness ❘ 605<br />

A 32-bit integer can hold 2 32 (4.3 billion) different values, so there are approximately 4.3 billion ways<br />

you can initialize a Random object. To think of it another way, there are roughly 4.3 billion possible<br />

keys. That may seem like a lot of keys, but to a computer it’s actually not many. It wouldn’t be hard to<br />

write a program that tries every possible seed value.<br />

For each seed value, the program would try to decrypt the message. If the result looks like English (or<br />

whatever language you use), the seed is probably correct. For example, in English some letters such<br />

as E appear more often than others. If the seed is correct, some letters should appear more often than<br />

others. If the seed is incorrect, all the letters should appear with roughly the same frequency.<br />

Generating Random Numbers<br />

Generating cryptographically strong random numbers is actually fairly easy in <strong>C#</strong>. Simply create a<br />

cryptographic random number generator object and use its methods to generate the numbers. The<br />

.NET Framework’s RNGCryptoServiceProvider class provides this kind of random number generator.<br />

(The “RNG” at the beginning of the class’s name stands for “random number generator.”)<br />

The RandomNumbers example program, which is available for download on the book’s website,<br />

uses the following code to generate a list of random numbers.<br />

// Generate random numbers.<br />

private void generateButton_Click(object sender, EventArgs e)<br />

{<br />

numbersListBox.Items.Clear();<br />

int numNumbers = int.Parse(numNumbersTextBox.Text);<br />

// Make the RNG.<br />

RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();<br />

// Make a buffer to hold 4 random bytes.<br />

byte[] bytes = new byte[4];<br />

// Make the random numbers.<br />

for (int i = 0; i < numNumbers; i++)<br />

{<br />

// Get 4 random bytes.<br />

rand.GetBytes(bytes);<br />

// Convert the bytes into an integer.<br />

int number = BitConverter.ToInt32(bytes, 0);<br />

}<br />

}<br />

// Display the number.<br />

numbersListBox.Items.Add(number);<br />

The program starts by clearing its output ListBox and parsing the number of random numbers it<br />

should generate.<br />

The code then creates an RNGCryptoServiceProvider object. That object generates random bytes<br />

but this program needs to display random numbers. To convert bytes into numbers, the program<br />

creates a byte array large enough to hold the number of bytes it needs. This program generates int<br />

values, which are 4-byte integers, so it makes an array holding 4 bytes.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!