Glossary Item Box

PowerTCP SSL Sockets for .NET

Simple Encryption/Decryption

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.

  1. 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")
    
  2. 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()
    
  3. 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))
    
  4. 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.

 

In This Section

Security Overview
Provides an overview of basic security concepts such as digital certificates and authentication.
PowerTCP Secure Implementation Explained
Discusses how security is implemented in PowerTCP SSL Sockets for .NET
Creating an SSL Client
Discusses how to create an SSL Client using the Tcp component.
Creating an SSL Server
Discusses how to create an SSL Server using the Server component.
Using the CertificateListForm Object
Demonstrates how to use the CertificateListForm to assist users in selecting a certificate.
Using MMC to Manage SSL Certificates
Discusses how to use the Microsoft Management Console to manage SSL certificates.
Simple Encryption/Decryption
Demonstrates how to use the SymmetricCryptoStream to encrypt/decrypt stream-based data.
Windows 95/98 Security Compatibility
Discusses compatibility issues with Windows 95 and 98.

 

 


Send comments on this topic.

Documentation version 1.1.2.0.

© 2008 Dart Communications.  All rights reserved.