Click here to Skip to main content
Licence 
First Posted 19 Mar 2004
Views 88,258
Bookmarked 38 times

Using CryptoStream in C#

By | 19 Mar 2004 | Article
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:

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

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralReadToEnd PinmemberMichael L Perry4:10 27 Jul '10  
QuestionWhat about asymmetric algorithms ? Pinmembertomlev3:56 2 Nov '09  
QuestionCryptoStream in C++/CLI Pinmemberbollwerkj4:10 25 Aug '08  
AnswerRe: CryptoStream in C++/CLI PinmemberWillemM7:35 25 Aug '08  
GeneralRe: CryptoStream in C++/CLI Pinmemberbollwerkj7:51 25 Aug '08  
GeneralRe: CryptoStream in C++/CLI PinmemberWillemM8:05 25 Aug '08  
GeneralRe: CryptoStream in C++/CLI Pinmemberbollwerkj8:07 25 Aug '08  
GeneralA ready to use function PinmemberElmue7:42 29 Jan '08  
NewsTwo other related encryption articles in CodeProject ... PinmemberTony Selke7:07 27 Sep '07  
GeneralRe: Two other related encryption articles in CodeProject ... PinmemberWillemM20:40 27 Sep '07  
GeneralGood for getting started quickly... Pinmembermube.co.uk3:44 30 Apr '06  
GeneralPoorly researched article PinmemberSteven Campbell11:04 20 Sep '04  
GeneralRe: Poorly researched article Pinmemberheysky21:19 14 May '08  
GeneralRe: Poorly researched article PinmemberWillemM19:56 20 Sep '04  
GeneralRe: Poorly researched article PinmemberRune Hunter17:09 16 Dec '06  
I think this is a great article and it does exactly as it was suposed to. This was ment to be an introduction and wich has done for me. I'd recomend this article to all of my friends.

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 20 Mar 2004
Article Copyright 2004 by WillemM
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid