Click here to Skip to main content
Click here to Skip to main content

Using CryptoStream in C#

By , 19 Mar 2004
Rate this:
Please Sign up or sign in to vote.

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:

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:

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

About the Author

WillemM
Web Developer
Netherlands Netherlands
WillemM is a 25 year old software developer working for Info Support. He loves new technology and spends most of his free time finding new ways to do things with his computer.
 
When not working on computers you can find him outside with his camera taking pictures.

Comments and Discussions

 
GeneralReadToEnd PinmemberMichael L Perry27-Jul-10 4:10 
QuestionWhat about asymmetric algorithms ? Pinmembertomlev2-Nov-09 3:56 
QuestionCryptoStream in C++/CLI Pinmemberbollwerkj25-Aug-08 4:10 
AnswerRe: CryptoStream in C++/CLI PinmemberWillemM25-Aug-08 7:35 
GeneralRe: CryptoStream in C++/CLI Pinmemberbollwerkj25-Aug-08 7:51 
By golly, I think you are close to being right.   Here's what I found that works (based on your reply):
 
array<Byte,1>^ key;
array<Byte,1>^ iv;
 
The compiler didn't like char.   The array<Type> syntax came about with the move from VS2003 to VS2005 & 2008.
 
Cheers and thanks for the help.
GeneralRe: CryptoStream in C++/CLI PinmemberWillemM25-Aug-08 8:05 
GeneralRe: CryptoStream in C++/CLI Pinmemberbollwerkj25-Aug-08 8:07 
GeneralA ready to use function PinmemberElmue29-Jan-08 7:42 
NewsTwo other related encryption articles in CodeProject ... PinmemberTony Selke27-Sep-07 7:07 
GeneralRe: Two other related encryption articles in CodeProject ... PinmemberWillemM27-Sep-07 20:40 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 20 Mar 2004
Article Copyright 2004 by WillemM
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid