.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Code Snippets >> Encryption & Decryption >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Encrypt/Decrypt text files using Rijndael or TripleDES

Posted By:Deepika Haridas       Posted Date: August 30, 2009    Points: 10    Category: Encryption & Decryption    URL: http://www.dotnetspark.com  

This sample code shows you how to use Rijndael or TripleDES to encrypt and decrypt text files that you can browse for and load into a TextBox.

using System.Security.Cryptography;
private SampleCrypto crpSample;
private bool FormHasLoaded = false;
private string strCurrentKeyFile;
private string strSourcePath;
private string strRijndaelSaltIVFile;
private string strTripleDESSaltIVFile;

This routine handles the "Load" button click event.

private void btnLoad_Click(object sender, System.EventArgs e)
//Open fle dialog control used
odlgSourceFile.InitialDirectory = @"C:\";

// The file could be of any type. The Filter is restricted to Text Format
// files only for demonstration purposes.

odlgSourceFile.Filter = "Text Format (*.txt)|*.txt";
odlgSourceFile.FilterIndex = 1;

// The OpenFileDialog control only has an Open button, not an OK button.
// However, there is no DialogResult.Open enum so use DialogResult.OK.

if (odlgSourceFile.ShowDialog() == DialogResult.OK) {
txtCrypto.Text = ReadFileAsstring(odlgSourceFile.FileName);
strSourcePath = odlgSourceFile.FileName;
catch( ArgumentException exp)
MessageBox.Show(exp.Message, this.Text,MessageBoxButtons.OK, MessageBoxIcon.Warning);

This routine handles the "Encrypt" and "Decrypt" button click events.

private void EncryptDecrypt_Click(object sender, System.EventArgs e)
Button btn = (Button) sender;
crpSample.SourceFileName = strSourcePath;
if (btn.Name == "btnEncrypt")

txtCrypto.Text = ReadFileAsstring(strSourcePath);
catch(CryptographicException expCrypto)
MessageBox.Show("The file could not be decrypted. Make sure you entered " +
"the correct password. " + Environment.NewLine + "This error can also be caused by changing " +
"crypto type between encryption and decryption.",
this.Text,MessageBoxButtons.OK, MessageBoxIcon.Warning);
catch( Exception exp)
MessageBox.Show(exp.Message, this.Text,MessageBoxButtons.OK, MessageBoxIcon.Warning);

This routine encrypts a file.

public void EncryptFile()
// Create a FileStream object to read in the source file.
FileStream fsInput = new FileStream(strSourceFile, FileMode.Open, FileAccess.Read);
// Create a byte array from the FileStream object by reading in the source file.

byte[] abytInput = new byte[Convert.ToInt32(fsInput.Length)];
fsInput.Read(abytInput, 0, Convert.ToInt32(fsInput.Length));

// Create a FileStream object to write to a temp file.

FileStream fsCipherText = new FileStream("temp.dat", FileMode.Create, FileAccess.Write);

// Create a Crypto Stream that transforms the file stream using the chosen
// encryption and writes it to the output FileStream object.

CryptoStream csEncrypted = new CryptoStream(fsCipherText, crpSym.CreateEncryptor(),
csEncrypted.Write(abytInput, 0, abytInput.Length);
// Clean up. There is no need to call fsCipherText.Close() because closing the
// crypto stream automatically encloses the stream that was passed into it.

This routine decrypts a file.

public void DecryptFile()
// Create a FileStream object to read back the encrypted file.
FileStream fsCipherText = new FileStream(strSourceFile, FileMode.Open, FileAccess.Read);
// Create a FileStream object to write to the temp file.
FileStream fsPlainText = new FileStream("temp.dat", FileMode.Create, FileAccess.Write);
// Read in the encrypted file and decrypt.
CryptoStream csDecrypted = new CryptoStream(fsCipherText, crpSym.CreateDecryptor(),
// Create a StreamWriter to write to the temp file.
StreamWriter swWriter = new StreamWriter(fsPlainText);
// Read the decrypted stream into a StreamReader.
StreamReader srReader = new StreamReader(csDecrypted);
// Write out the decrypted stream.
catch( CryptographicException expCrypto)
throw new CryptographicException();
// Close and clean up.


Further Readings:

Author: Admiral Ackbar         Company URL: http://www.dotnetspark.com
Posted Date: November 22, 2013

Thank you for posting an incomplete article

hungarian notation is dead

please do not post anything again

this was useless

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend