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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 27 ❘ 729<br />

is decrypting, the method subtracts a random value between 0 and 25 from each character.<br />

It takes the new value modulo 26 and converts the result back into a letter.<br />

The oddest trick here is that the code adds 26 to the character’s number if it is decoding. For<br />

example, if the ciphertext letter is B = 1 and the random value subtracted is 10, then 1 – 10 =<br />

–9. In <strong>C#</strong> the modulus operator % doesn’t care if its result is negative, so it leaves this value –9,<br />

which does not convert back into a letter. The code adds 26, so the program calculates (–9 +<br />

26) % 26 = 17 % 26 = 17, which converts to the letter R.<br />

2. The BreakRandomCipher example program does this. It’s mostly straightforward, so download<br />

the example to see how it works. The following code shows the trickiest part, which<br />

uses LINQ to find the largest letter frequency.<br />

// Get the letter frequencies.<br />

var query =<br />

from char ch in plaintext<br />

group ch by ch into g<br />

select g.Count();<br />

// See how big the largest frequency is.<br />

if (query.Max() / (float)plaintext.Length > maxFrequency)<br />

{<br />

// Hopefully this is it.<br />

...<br />

}<br />

This code uses LINQ to get the letter counts in the plaintext. It then divides the largest<br />

count by the number of letters in the message and compares that to the test frequency.<br />

3. This approach doesn’t work well for short messages because the letter frequencies in short<br />

messages may not match normal language usage. For example, in English the letter E appears<br />

most often, but it doesn’t appear at all in the message “All good plans will bring victory!”<br />

4. You could look at a measure of the distribution of the letters in the plaintext to see if it matches<br />

what you expect for a correct decryption. For example, you could see if the top three letters<br />

have frequencies around 12 percent, 9 percent, and 8 percent, the frequencies of the letters<br />

E, T, and A in English, respectively. Or you could see if the standard deviations of the letters<br />

are large. Unfortunately, both of those techniques are relatively time-consuming. The method<br />

described in the exercise is less reliable, so it won’t work well with short messages, but it is<br />

much faster.<br />

You might make the test a bit more reliable if you look at the largest and smallest frequencies.<br />

5. This program looks only at relative letter frequencies, so it should work with any language<br />

where messages have uneven frequency distributions. The biggest change would be to allow<br />

the program to work with characters that don’t lie between A and Z such as ö and ß.<br />

6. The RandomIntegers example program uses the following code to provide the GetInt method.<br />

public static class MyRandom<br />

{<br />

// The RNG.<br />

private static RNGCryptoServiceProvider Rand = new RNGCryptoServiceProvider();<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!