Glossary Item Box
SymmetricCryptoStream is a PipeStream type (see PowerTCP Secure Implementation Explained for more info on PipeStreams) which encrypts/decrypts data passing to/from the SymmetricCryptoStream to another stream. This implementation is useful for encrypting/decrypting any type of stream-based data. For this example, data will be encrypted and written to a file, then decrypted and displayed.
To encrypt/decrypt data.
First create a key and some test data to encrypt.
[C#] // Create a key and some data. byte[] key = System.Text.Encoding.Default.GetBytes("test key"); byte[] data = System.Text.Encoding.Default.GetBytes("This is the plain text data"); [Visual Basic] ' Create a key and some data. Dim key As Byte() = System.Text.Encoding.Default.GetBytes("test key") Dim data As Byte() = System.Text.Encoding.Default.GetBytes("This is the plain text data")
Create a FileStream to write the encrypted data to and a SymmetricCryptoStream to use to encrypt the data. SymmetricCryptoStream.Write the data to the file. It will be encrypted.
[C#] // Create a FileStream to hold the data System.IO.FileStream f = new System.IO.FileStream("C:\\encrypted", System.IO.FileMode.Create); Dart.PowerTCP.SslSockets.SymmetricCryptoStream sc = new Dart.PowerTCP.SslSockets.SymmetricCryptoStream(key, f); // Write to the file. This encrypts the data. sc.Write(data, 0, data.Length); sc.Position=0; f.Close(); [Visual Basic] ' Create a FileStream to hold the data Dim f As System.IO.FileStream = New System.IO.FileStream("C:\encrypted", System.IO.FileMode.Create) Dim sc As New Dart.PowerTCP.SslSockets.SymmetricCryptoStream(key, f) ' Write to the file. This encrypts the data. sc.Write(data, 0, data.Length) sc.Position = 0 f.Close()
Now to decrypt the data. Reinitialize the FileStream to read the file and reinitialize the SymmetricCryptoStream. Read data from the file. It will automatically be decrypted.
[C#] // Now read the data from the encrypted file to decrypt f = new System.IO.FileStream("C:\\encrypted", System.IO.FileMode.Open); sc = new Dart.PowerTCP.SslSockets.SymmetricCryptoStream(key, f); data = new byte[sc.Length]; sc.Read(data, 0, data.Length); sc.Position = 0; f.Close(); // Display results Debug.WriteLine(System.Text.Encoding.Default.GetString(data)); [Visual Basic] ' Now read the data from the encrypted file to decrypt f = new System.IO.FileStream("C:\encrypted", System.IO.FileMode.Open) sc = new Dart.PowerTCP.SslSockets.SymmetricCryptoStream(key, f) data = new byte(sc.Length) sc.Read(data, 0, data.Length) sc.Position = 0 f.Close() ' Display results Debug.WriteLine(System.Text.Encoding.Default.GetString(data))
The decrypted ("plain text") data should be displayed. To see the encrypted text, open the file "C:\Encrypted". Also, it is often useful to change the FileStream to another type of stream. For example, use a MemoryStream to encrypt/decrypt data in memory only.
Send comments on this topic.
Documentation version 1.1.2.0.
© 2008 Dart Communications. All rights reserved.