CryptoStream Class
Assembly: mscorlib (in mscorlib.dll)
'Declaration <ComVisibleAttribute(True)> _ Public Class CryptoStream Inherits Stream Implements IDisposable 'Usage Dim instance As CryptoStream
/** @attribute ComVisibleAttribute(true) */ public class CryptoStream extends Stream implements IDisposable
ComVisibleAttribute(true) public class CryptoStream extends Stream implements IDisposable
Not applicable.
The common language runtime uses a stream-oriented design for cryptography. The core of this design is CryptoStream. Any cryptographic objects that implement CryptoStream can be chained together with any objects that implement Stream, so the streamed output from one object can be fed into the input of another object. The intermediate result (the output from the first object) does not need to be stored separately.
You should always explicitly close your CryptoStream object after you are done using it by calling the Close method. Doing so flushes the stream and causes all remain blocks of data to be processed by the CryptoStream object. However, if an exception occurs before you call the Close method, the CryptoStream object might not be closed. To ensure that the Close method always gets called, place your call to the Close method within the finally block of a try/catch statement.
The following example demonstrates how to use a CryptoStream to encrypt a string. This method uses RijndaelManaged class with the specified Key and initialization vector (IV).
Imports System.Security.Cryptography Imports System.Text Imports System.IO Module RijndaelSample Sub Main() Try ' Create a new Rijndael object to generate a key ' and initialization vector (IV). Dim RijndaelAlg As Rijndael = Rijndael.Create ' Create a string to encrypt. Dim sData As String = "Here is some data to encrypt." Dim FileName As String = "CText.txt" ' Encrypt text to a file using the file name, key, and IV. EncryptTextToFile(sData, FileName, RijndaelAlg.Key, RijndaelAlg.IV) ' Decrypt the text from a file using the file name, key, and IV. Dim Final As String = DecryptTextFromFile(FileName, RijndaelAlg.Key, RijndaelAlg.IV) ' Display the decrypted string to the console. Console.WriteLine(Final) Catch e As Exception Console.WriteLine(e.Message) End Try Console.ReadLine() End Sub Sub EncryptTextToFile(ByVal Data As String, ByVal FileName As String, ByVal Key() As Byte, ByVal IV() As Byte) Try ' Create or open the specified file. Dim fStream As FileStream = File.Open(FileName, FileMode.OpenOrCreate) ' Create a new Rijndael object. Dim RijndaelAlg As Rijndael = Rijndael.Create ' Create a CryptoStream using the FileStream ' and the passed key and initialization vector (IV). Dim cStream As New CryptoStream(fStream, _ RijndaelAlg.CreateEncryptor(Key, IV), _ CryptoStreamMode.Write) ' Create a StreamWriter using the CryptoStream. Dim sWriter As New StreamWriter(cStream) Try ' Write the data to the stream ' to encrypt it. sWriter.WriteLine(Data) Catch e As Exception Console.WriteLine("An error occurred: {0}", e.Message) Finally ' Close the streams and ' close the file. sWriter.Close() cStream.Close() fStream.Close() End Try Catch e As CryptographicException Console.WriteLine("A Cryptographic error occurred: {0}", e.Message) Catch e As UnauthorizedAccessException Console.WriteLine("A file error occurred: {0}", e.Message) End Try End Sub Function DecryptTextFromFile(ByVal FileName As String, ByVal Key() As Byte, ByVal IV() As Byte) As String Try ' Create or open the specified file. Dim fStream As FileStream = File.Open(FileName, FileMode.OpenOrCreate) ' Create a new Rijndael object. Dim RijndaelAlg As Rijndael = Rijndael.Create ' Create a CryptoStream using the FileStream ' and the passed key and initialization vector (IV). Dim cStream As New CryptoStream(fStream, _ RijndaelAlg.CreateDecryptor(Key, IV), _ CryptoStreamMode.Read) ' Create a StreamReader using the CryptoStream. Dim sReader As New StreamReader(cStream) ' Read the data from the stream ' to decrypt it. Dim val As String = Nothing Try val = sReader.ReadLine() Catch e As Exception Console.WriteLine("An Cerror occurred: {0}", e.Message) Finally ' Close the streams and ' close the file. sReader.Close() cStream.Close() fStream.Close() End Try ' Return the string. Return val Catch e As CryptographicException Console.WriteLine("A Cryptographic error occurred: {0}", e.Message) Return Nothing Catch e As UnauthorizedAccessException Console.WriteLine("A file error occurred: {0}", e.Message) Return Nothing End Try End Function End Module
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Reference
CryptoStream MembersSystem.Security.Cryptography Namespace