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.

610 ❘ CHAPTER 27 Cryptography<br />

Next, the code creates the variable cipherbytes to hold the encrypted data later. It then creates an<br />

encryptor object that it can use to encrypt the string.<br />

To use the encryptor, the program must create one stream from which to read data and a second<br />

stream into which to write results. The program creates a MemoryStream to hold the results.<br />

The program then creates a CryptoStream object to transform any data written into the input<br />

stream. It passes the memory stream and the encryptor into the CryptoStream’s constructor, so it<br />

knows where to write results and what encryptor to use to transform the data. It also passes the<br />

constructor the value CryptoStreamMode.Write so the CryptoStream knows it will be writing<br />

into the memory stream.<br />

Next, the code creates a StreamWriter into which it can write the plaintext data. It associates the<br />

writer with the CryptoStream.<br />

Finally, the code calls the StreamWriter’s Write method to write the string.<br />

Figure 27-2 shows the process schematically. The plaintext is written into the StreamWriter. The<br />

StreamWriter writes the data into the CryptoStream. The CryptoStream uses the associated<br />

encryptor to transform the data and writes the result into the MemoryStream.<br />

This is the secret<br />

message.<br />

StreamWriter<br />

CryptoStream<br />

Encryptor<br />

MemoryStream<br />

6E-56-4B-B1-05-4C-90-DC-45-E1-EC-3D-9D-. . .<br />

Figure 27-2: To encrypt a message, a StreamWriter writes data into<br />

a CryptoStream, which transforms the data and writes the result into a<br />

MemoryStream.<br />

After the program writes the data, it saves the MemoryStream’s contents in a byte array.<br />

Next, the program displays the AesManaged object’s Key and IV values, and the encrypted bytes.<br />

(The following section says more about the Key and IV values.) It uses the BitConverter.ToString<br />

method to convert each of those values, all of which are byte arrays, into strings displaying the<br />

bytes as sequences of hexadecimal numbers.<br />

After it encrypts the message string, the program uses the following code to decrypt the<br />

encrypted bytes.<br />

// Decrypt. Create a decryptor.<br />

using (ICryptoTransform decryptor =<br />

provider.CreateDecryptor(provider.Key, provider.IV))<br />

{<br />

// Create a memory stream from which to read the encrypted bytes.<br />

using (MemoryStream ms = new MemoryStream(cipherbytes))<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!