Click here to Skip to main content
13,193,468 members (24,969 online)
Click here to Skip to main content
Add your own
alternative version


51 bookmarked
Posted 19 Mar 2004

Using CryptoStream in C#

, 19 Mar 2004
Rate this:
Please Sign up or sign in to vote.
This article explains how to use CryptoStream for simple on the fly cryptography.


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", 

DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();

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

CryptoStream crStream = new CryptoStream(stream,

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



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", 

DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();

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

CryptoStream crStream = new CryptoStream(stream,

StreamReader reader = new StreamReader(crStream);

string data = reader.ReadToEnd();



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.


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

Web Developer
Netherlands Netherlands

You may also be interested in...


Comments and Discussions

GeneralReadToEnd Pin
Michael L Perry27-Jul-10 4:10
memberMichael L Perry27-Jul-10 4:10 
QuestionWhat about asymmetric algorithms ? Pin
tomlev2-Nov-09 3:56
membertomlev2-Nov-09 3:56 
QuestionCryptoStream in C++/CLI Pin
bollwerkj25-Aug-08 4:10
memberbollwerkj25-Aug-08 4:10 
AnswerRe: CryptoStream in C++/CLI Pin
WillemM25-Aug-08 7:35
memberWillemM25-Aug-08 7:35 
GeneralRe: CryptoStream in C++/CLI Pin
bollwerkj25-Aug-08 7:51
memberbollwerkj25-Aug-08 7:51 
GeneralRe: CryptoStream in C++/CLI Pin
WillemM25-Aug-08 8:05
memberWillemM25-Aug-08 8:05 
GeneralRe: CryptoStream in C++/CLI Pin
bollwerkj25-Aug-08 8:07
memberbollwerkj25-Aug-08 8:07 
GeneralA ready to use function Pin
Elmue29-Jan-08 7:42
memberElmue29-Jan-08 7:42 

I wrote a tiny elegant function which does encryption and decryption.

If you just need rapidly a function to encrypt / decrypt, copy/paste this code and youre done.
If the decryption fails it returns null.


using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
public static string Crypt(string s_Data, string s_Password, bool b_Encrypt) 
	byte[] u8_Salt = new byte[] {0x26, 0x19, 0x81, 0x4E, 0xA0, 0x6D, 0x95, 0x34, 0x26, 0x75, 0x64, 0x05, 0xF6}; 
	PasswordDeriveBytes i_Pass = new PasswordDeriveBytes(s_Password, u8_Salt);
	Rijndael i_Alg = Rijndael.Create();
	i_Alg.Key = i_Pass.GetBytes(32);
	i_Alg.IV  = i_Pass.GetBytes(16);
	ICryptoTransform i_Trans = (b_Encrypt) ? i_Alg.CreateEncryptor() : i_Alg.CreateDecryptor();
	MemoryStream i_Mem   = new MemoryStream();
	CryptoStream i_Crypt = new CryptoStream(i_Mem, i_Trans, CryptoStreamMode.Write);
	byte[] u8_Data;
	if (b_Encrypt) u8_Data = Encoding.Unicode.GetBytes(s_Data);
	else           u8_Data = Convert.FromBase64String (s_Data);
		i_Crypt.Write(u8_Data, 0, u8_Data.Length);
	catch { return null; }
	if (b_Encrypt) return Convert.ToBase64String    (i_Mem.ToArray());
	else           return Encoding.Unicode.GetString(i_Mem.ToArray());

NewsTwo other related encryption articles in CodeProject ... Pin
Tony Selke27-Sep-07 7:07
memberTony Selke27-Sep-07 7:07 
GeneralRe: Two other related encryption articles in CodeProject ... Pin
WillemM27-Sep-07 20:40
memberWillemM27-Sep-07 20:40 
GeneralGood for getting started quickly... Pin 3:44 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.

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