c# net qr code generator CryptoStream itself, to ensure that the encrypted data is all written to the underlying in C#

Creator QR Code ISO/IEC18004 in C# CryptoStream itself, to ensure that the encrypted data is all written to the underlying

CryptoStream itself, to ensure that the encrypted data is all written to the underlying
Make QR Code JIS X 0510 In Visual C#.NET
Using Barcode encoder for .NET framework Control to generate, create Quick Response Code image in VS .NET applications.
www.OnBarcode.com
Quick Response Code Reader In C#.NET
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
stream. We can use any sort of stream for that underlying stream. We could use a file stream on disk, or one of the isolated storage file streams we saw earlier in this chapter, for example. We could even use one of the network streams we re going to see in 13. However, for this example we d like to do everything in memory, and the framework has just the class for us: the MemoryStream.
Paint Code128 In C#.NET
Using Barcode printer for .NET Control to generate, create Code-128 image in .NET framework applications.
www.OnBarcode.com
Create EAN / UCC - 14 In Visual C#.NET
Using Barcode creator for .NET Control to generate, create UCC-128 image in .NET applications.
www.OnBarcode.com
In Memory Alone: The MemoryStream
QR-Code Creator In Visual C#
Using Barcode generation for .NET Control to generate, create QR Code image in VS .NET applications.
www.OnBarcode.com
Printing Code-39 In Visual C#
Using Barcode generation for VS .NET Control to generate, create Code 3/9 image in Visual Studio .NET applications.
www.OnBarcode.com
MemoryStream is very simple in concept. It is just a stream that uses memory as its backing
Generate Barcode In Visual C#.NET
Using Barcode printer for VS .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
DUN - 14 Drawer In Visual C#.NET
Using Barcode creator for Visual Studio .NET Control to generate, create UPC Case Code image in .NET applications.
www.OnBarcode.com
store. We can do all of the usual things like reading, writing, and seeking. It s very useful when you re working with APIs that require you to provide a Stream, and you don t already have one handy. If we use the default constructor (as in our example), we can read and write to the stream, and it will automatically grow in size as it needs to accommodate the data being written. Other constructors allow us to provide a start size suitable for our purposes (if we know in advance what that might be). We can even provide a block of memory in the form of a byte[] array to use as the underlying storage for the stream. In that case, we are no longer able to resize the stream, and we will get a NotSupportedException if we try to write too much data. You would normally supply your own byte[] array when you already have one and need to pass it to something that wants to read from a stream. We can find out the current size of the underlying block of memory (whether we allocated it explicitly, or whether it is being automatically resized) by looking at the stream s Capacity property. Note that this is not the same as the maximum number of bytes we ve ever written to the stream. The automatic resizing tends to overallocate to avoid the overhead of constant reallocation when writing. In general, you can determine how many bytes you ve actually written to by looking at the Position in the stream at the beginning and end of your write operations, or the Length property of the MemoryStream. Having used the CryptoStream to write the cipher text into the stream, we need to turn that into a string we can show on the console.
Denso QR Bar Code Decoder In Visual C#.NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Paint QR Code In None
Using Barcode encoder for Online Control to generate, create QR Code 2d barcode image in Online applications.
www.OnBarcode.com
Representing Binary As Text with Base64 Encoding
Generating European Article Number 13 In .NET Framework
Using Barcode creator for .NET Control to generate, create UPC - 13 image in .NET applications.
www.OnBarcode.com
Code 3 Of 9 Scanner In C#.NET
Using Barcode decoder for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Unfortunately, the cipher text is not actually text at all it is just a stream of bytes. We can t use the UTF8Encoding.UTF8.GetString technique we saw in 10 to turn the bytes into text, because these bytes don t represent UTF-8 encoded characters. Instead, we need some other sort of text-friendly representation if we re going to be able to print the encrypted text to the console. We could write each byte out as hex digits. That would be a perfectly reasonable string representation.
Paint Denso QR Bar Code In Objective-C
Using Barcode creator for iPhone Control to generate, create Quick Response Code image in iPhone applications.
www.OnBarcode.com
USS-128 Generation In None
Using Barcode generation for Software Control to generate, create UCC-128 image in Software applications.
www.OnBarcode.com
However, that s not very compact (each byte is taking five characters in the string!):
USS Code 39 Reader In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Code 128C Drawer In Objective-C
Using Barcode creator for iPhone Control to generate, create Code 128 Code Set B image in iPhone applications.
www.OnBarcode.com
0x01 0x0F 0x03 0xFA 0xB3
PDF417 Reader In Visual C#.NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
DataMatrix Drawer In Objective-C
Using Barcode drawer for iPad Control to generate, create DataMatrix image in iPad applications.
www.OnBarcode.com
A much more compact textual representation is Base64 encoding. This is a very popular textual encoding of arbitrary data. It s often used to embed binary in XML, which is a fundamentally text-oriented format. And even better, the framework provides us with a convenient static helper method to convert from a byte[] to a Base64 encoded string: Convert.ToBase64String.
ECC200 Creator In None
Using Barcode printer for Office Excel Control to generate, create DataMatrix image in Office Excel applications.
www.OnBarcode.com
2D Barcode Creator In VS .NET
Using Barcode generator for ASP.NET Control to generate, create 2D image in ASP.NET applications.
www.OnBarcode.com
If you re wondering why there s no Encoding class for Base64 to correspond to the Unicode, ASCII, and UTF-8 encodings we saw in 10, it s because Base64 is a completely different kind of thing. Those other encodings are mechanisms that define binary representations of textual information. Base64 does the opposite it defines a textual representation for binary information.
Example 11-54 shows how we make use of that in our GetCipherText method.
private static string GetCipherText(MemoryStream memoryStream) { byte[] buffer = memoryStream.ToArray(); return System.Convert.ToBase64String(buffer, 0, buffer.Length); }
We use a method on MemoryStream called ToArray to get a byte[] array containing all the data written to the stream.
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.
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
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.
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 classes to manage and manipulate files and folders in the filesystem, including creating, deleting, appending, and truncating data, as well as managing their access permissions. We saw how to use StreamReader and StreamWriter to deal with reading and writing text from files, and how we can also read and write binary data using the underlying Stream objects themselves, including the ability to Seek backward and forward in the file.
We then looked at a special type of file stream called isolated storage. This gives us the ability to manage the scope of file access to particular users, machines, applications, or even assemblies. We gain control over quotas (the maximum amount of space any particular store is allowed to use), and get to use local file storage in normally restricted security contexts like that of a Silverlight application, for example. Finally, we looked at some streams that aren t files, including MemoryStream, which uses memory as its underlying storage mechanism, and CryptoStream, which has no storage of its own, delegating that responsibility to another stream. We showed how these patterns can be used to plug streams together into a processing pipeline.
Copyright © OnBarcode.com . All rights reserved.