Click here to Skip to main content
15,867,305 members
Articles / Programming Languages / C#
Article

Using CryptoStream in C#

Rate me:
Please Sign up or sign in to vote.
2.84/5 (30 votes)
19 Mar 20041 min read 215K   52   15
This article explains how to use CryptoStream for simple on the fly cryptography.

Introduction

Encryption in C# is a lot easier using the standard components provided by the .NET framework. In this article, I will explain how it works.

.NET provides us with a standard set of cryptography providers. These are samples of standard cryptography providers:

  • Rijndael
  • RSA
  • DES
  • DSA
  • TripleDES

These cryptography providers can be used in combination with the CryptoStream class. This class can encrypt/decrypt data on the fly from an underlying stream. You can link up almost any stream to the CryptoStream, as long as it supports the standard stream functionality defined in the Stream base class.

How to use CryptoStream

It’s pretty straightforward. First, you need a base stream which you will use as buffer for the encryption/decryption. You also need a cryptographic transformer which is part of the CryptographicServiceProvider class. If you combine these parts into a CryptoStream, you can encrypt/decrypt on the fly.

The following example explains best how the whole process of encryption works:

C#
FileStream stream = new FileStream("C:\\test.txt", 
         FileMode.OpenOrCreate,FileAccess.Write);

DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();

cryptic.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");

CryptoStream crStream = new CryptoStream(stream,
   cryptic.CreateEncryptor(),CryptoStreamMode.Write);


byte[] data = ASCIIEncoding.ASCII.GetBytes("Hello World!");

crStream.Write(data,0,data.Length);

crStream.Close();
stream.Close();

The key and the initialization vector must be 8 characters long, that is 64 bits. This is because the DES cryptography provider uses a 64 bit key to encrypt data. If you use other algorithms for your CryptoStream, you will probably need another key size. Check the documentation on how large the key and the IV must be.

We could also decrypt data using CryptoStream. The following example shows how to decrypt data from a file:

C#
FileStream stream = new FileStream("C:\\test.txt", 
                              FileMode.Open,FileAccess.Read);

DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();

cryptic.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");

CryptoStream crStream = new CryptoStream(stream,
    cryptic.CreateDecryptor(),CryptoStreamMode.Read);

StreamReader reader = new StreamReader(crStream);

string data = reader.ReadToEnd();

reader.Close();
stream.Close();

Conclusion

The cryptography services in .NET are pretty simple and work very well. And I hope this article has contributed to a better knowledge about cryptography and how to use it in .NET.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Netherlands Netherlands
----

Comments and Discussions

 
GeneralReadToEnd Pin
Michael L Perry27-Jul-10 4:10
Michael L Perry27-Jul-10 4:10 
QuestionWhat about asymmetric algorithms ? Pin
Thomas Levesque2-Nov-09 3:56
professionalThomas Levesque2-Nov-09 3:56 
QuestionCryptoStream in C++/CLI Pin
bollwerkj25-Aug-08 4:10
bollwerkj25-Aug-08 4:10 
AnswerRe: CryptoStream in C++/CLI Pin
WillemM25-Aug-08 7:35
WillemM25-Aug-08 7:35 
GeneralRe: CryptoStream in C++/CLI Pin
bollwerkj25-Aug-08 7:51
bollwerkj25-Aug-08 7:51 
GeneralRe: CryptoStream in C++/CLI Pin
WillemM25-Aug-08 8:05
WillemM25-Aug-08 8:05 
The key and initialization vector are a bit short now, I think you need at least 16 bytes to create a combination for an 128 algorithm.
My advice is to check the MSDN documentation on the key size required.

WM.

What about weapons of mass-construction?

"What? Its an Apple MacBook Pro. They are sexy!" - Paul Watson
My blog

GeneralRe: CryptoStream in C++/CLI Pin
bollwerkj25-Aug-08 8:07
bollwerkj25-Aug-08 8:07 
GeneralA ready to use function Pin
Elmue29-Jan-08 7:42
Elmue29-Jan-08 7:42 
NewsTwo other related encryption articles in CodeProject ... Pin
Tony Selke27-Sep-07 7:07
Tony Selke27-Sep-07 7:07 
GeneralRe: Two other related encryption articles in CodeProject ... Pin
WillemM27-Sep-07 20:40
WillemM27-Sep-07 20:40 
GeneralGood for getting started quickly... Pin
mube.co.uk30-Apr-06 3:44
mube.co.uk30-Apr-06 3:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.