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.

Exercises ❘ 619<br />

Length Limitations<br />

RSA is relatively slow compared to symmetric key algorithms, so the .NET<br />

Framework limits the size of the messages it encrypts with RSA. If you exceed the<br />

allowed length, the provider throws a CryptographicException with the rather<br />

unhelpful message “Bad Length.”<br />

Symmetric algorithms break messages into blocks and then encrypt them separately,<br />

so they can handle messages of any length.<br />

Together the two kinds of algorithms are effective. For example, you could use the<br />

following steps to send a long message to a receiver even if you have not exchanged<br />

private keys for symmetric encryption.<br />

1. Generate a private key for symmetric encryption.<br />

2. Use asymmetric encryption to send the private key to the receiver.<br />

3. Send the symmetrically encrypted message to the receiver.<br />

Summary<br />

Cryptography is a fascinating topic. Modern cryptographic tools use sophisticated mathematics to<br />

let you perform such operations as encrypting and decrypting files and strings, hashing files to see if<br />

they have been modified, digitally signing files to provide assurance that you actually wrote the file,<br />

and generating cryptographically secure random numbers.<br />

There isn’t room to cover all these topics here, so this chapter focused on three of the most useful<br />

tasks: generating cryptographically secure random numbers, symmetric encryption, and asymmetric<br />

encryption. This should provide enough introduction for you to continue your study of the .NET<br />

Framework’s cryptographic methods on your own.<br />

For more information about cryptographic tools in the .NET Framework, see “Cryptographic Services”<br />

at msdn.microsoft.com/library/92f9ye3s.aspx.<br />

Exercises<br />

1. Write a program that uses the Random class to encrypt and decrypt messages.<br />

Unfortunately, if you use the XOR operator ^ to combine a letter with a random byte, the<br />

result may not be a letter, so it’s hard to visualize the result. To make it easier to visualize<br />

the results, assume the message contains only uppercase letters A through Z. (For bonus points,<br />

use string methods and LINQ to convert the plaintext.) To encrypt a letter, shift it by a random<br />

number of letters between 0 and 25. For example, A + 1 = B, A + 2 = C, and so forth.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!