Dont be caught out by the tobuffer method which also

Info icon This preview shows pages 469–472. Sign up to view the full content.

View Full Document Right Arrow Icon
Don’t be caught out by the ToBuffer method, which also returns a byte[] array. ToBuffer returns the whole buffer including any “extra” bytes that have been allocated but not yet used. Finally, we call Convert.ToBase64String to get a string representation of the underlying data, passing it the byte[] , along with a start offset into that buffer of zero (so that we start with the first byte), and the length. That takes care of encryption. How about decryption? That’s actually a little bit easier. Example 11-55 shows how. Example 11-55. Decryption private static string DecryptString(string cipherText, byte[] key, byte[] iv) { // Create a crypto service provider for the TripleDES algorithm var serviceProvider = new TripleDESCryptoServiceProvider(); // Decode the cipher-text bytes back from the base-64 encoded string Streams That Aren’t Files | 445
Image of page 469

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
byte[] cipherTextBytes = Convert.FromBase64String(cipherText); // Create a memory stream over those bytes using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes)) // And create a cryptographic stream over the memory stream, // using the specified algorithm // (with the provided key and initialization vector) using (var cryptoStream = new CryptoStream( memoryStream, serviceProvider.CreateDecryptor(key, iv), CryptoStreamMode.Read)) // Finally, create a stream reader over the stream, and recover the // original text using (StreamReader reader = new StreamReader(cryptoStream)) { return reader.ReadToEnd(); } } First, we use Convert.FromBase64String to convert our Base64 encoded string back to an array of bytes. We then construct a MemoryStream over that byte[] by passing it to the appropriate constructor. As before, we wrap the MemoryStream with a CryptoStream , this time passing it the ICryptoTransform created by a call to CreateDecryptor on our TripleDESCryptoService Provider , and putting it into CryptoStreamMode.Read . Finally, we construct our old friend the StreamReader over the CryptoStream , and read the content back as a string. So, what’s actually happening here? CryptoStream uses the ICryptoTransform to take care of turning the cipher text in the MemoryStream back into plain text. If you remember, that plain text is actually the set of UTF-8 encoded bytes we originally wrote to the stream with the StreamWriter back in the encryption phase. So, the StreamReader takes those and converts them back into a string for us. You can see that illustrated in Figure 11-14 . This is a very powerful example of how we can plug together various components in a kind of pipeline to achieve quite complex processing, from simple, easily understood building blocks that conform to a common pattern, but which have no dependencies on each other’s implementation details. The Stream abstraction is the key to this flexibility. 446 | Chapter 11: Files and Streams
Image of page 470
Figure 11-14. Encryption and decryption pipeline using streams Summary In this chapter we looked at the classes in the System.IO namespace that relate to files and streams. We saw how we can use static methods on the File , Directory , and Path
Image of page 471

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 472
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern