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.

606 ❘ CHAPTER 27 Cryptography<br />

Data Translations<br />

In the past, cryptography encrypted and decrypted only text messages. Modern<br />

cryptography must encrypt any kind of data including text, databases, images,<br />

audio, video, and anything else you can imagine. That means an encrypted message<br />

may not be a simple string of letters. In fact, most modern techniques produce<br />

seemingly random sequences of bytes.<br />

That makes the cryptographic algorithms strong and flexible, but it makes visualizing<br />

the encrypted data difficult. There are a few tools you can use to make visualizing<br />

and working with encrypted data easier.<br />

The UnicodeEncoding class provides GetBytes and GetString methods that let<br />

you convert between strings and byte arrays. You can use GetBytes to turn a<br />

string into a byte array so that you can encrypt the bytes. After you decrypt<br />

some encrypted data, the result is another byte array. You can then use the<br />

GetString method to turn the result back into a string.<br />

The BitConverter.ToString method converts many data types (including byte<br />

arrays) into a string that includes the bytes’ values. The string consists of a<br />

sequence of two-digit hexadecimal numbers representing the array’s byte values,<br />

separated by hyphens as in F9-9A-98-6F-BC-9F. This method lets you display byte<br />

arrays textually.<br />

Unfortunately, the BitConverter class doesn’t provide a reverse method for<br />

translating a string of the form F9-9A-98-6F-BC-9F back into a byte array.<br />

Fortunately, it’s not hard to write one.<br />

// Convert a string created by BitConverter.ToString into a byte[].<br />

private byte[] StringToBytes(string input)<br />

{<br />

string[] byteStrings = input.Split('-');<br />

byte[] result = new byte[byteStrings.Length];<br />

for (int i = 0; i < result.Length; i++)<br />

result[i] = Convert.ToByte(byteStrings[i], 16);<br />

return result;<br />

}<br />

This method uses the string class’s Split method to split the input string into an<br />

array of two-character strings holding the bytes’ hexadecimal values. Next, it creates<br />

a byte array long enough to hold the bytes. It then loops through the bytes’<br />

hexadecimal values and uses Convert.ToByte to convert them into their byte values.<br />

As you write cryptographic programs, you can use these tools to<br />

➤➤<br />

➤➤<br />

➤➤<br />

➤➤<br />

Convert plaintext strings into byte arrays so that you can encrypt them.<br />

Display textual representations of byte arrays.<br />

Convert textual representations of byte arrays back into arrays.<br />

Convert a byte array containing a string into the string.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!