---
title: "Encrypting and Decrypting Files Using C sharp"  
description: "/* Style Definitions */   table.MsoNormalTable  	{mso-style-name:\"Table Normal\";  	mso-tstyle-rowband-size:0;  	mso-tstyle-colband-size:0;  	mso-st"  
author: "Anonymous User"  
published: 2010-07-14  
updated: 2020-05-09  
canonical: https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp  
category: "security in .net"  
tags: ["security in .net"]  
reading_time: 2 minutes  

---

# Encrypting and Decrypting Files Using C sharp

Here I’m going to show how to encrypt and decrypt text file using C#.Net

![Encrypting and Decrypting Files Using C sharp](https://www.mindstick.com/mindstickarticle/371797d3-16fe-4722-aa16-ae7a08a21992/images/db9f8e42-3f92-4ef8-a268-d5d82ee39d45.png)\

##### Example

```
//namespace for Input Output file streamusing System.IO;//name space for Cryptographyusing System.Security.Cryptography;private void btnSelFile_Click(object sender, EventArgs e)        {            try            {               //opening file.                openFileDialog1.Title = "Open File";                if (openFileDialog1.ShowDialog() == DialogResult.OK)                    txtFileName.Text = openFileDialog1.FileName;            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }        }           //Encrypting         private void btnEncrypt_Click(object sender, EventArgs e)        {            try            {                saveKeyDialog2.Title = "Save Key";                saveFileDialog1.Title = "Save Encrypted file";               // after the user chose where he wants the key file saved                if (saveKeyDialog2.ShowDialog() == DialogResult.OK)                {                // after the user chose where he wants the encrypted file saved                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)                    {                      FileStream fs = File.Create(saveFileDialog1.FileName);                      // the cryptographic service provider  we're going to use                     TripleDESCryptoServiceProvider tc = new TripleDESCryptoServiceProvider();                      // this object links data streams to cryptographic values                    CryptoStream cs = new CryptoStream(fs, tc.CreateEncryptor(), CryptoStreamMode.Write);                     // This stream reader will read the file to encrypt                        StreamReader sr = new StreamReader(txtFileName.Text);                      // this stream writer will write the new file                        StreamWriter sw = new StreamWriter(cs);                        string curline = sr.ReadLine();                        while (curline != null)                        {                        // Write to the encryption stream                            sw.Write(curline);                            curline = sr.ReadLine();                        }                        sr.Close();                        sw.Flush();                        sw.Close();                        //creating key file                        FileStream fsKey = File.Create(saveKeyDialog2.FileName);                        BinaryWriter bw = new BinaryWriter(fsKey);                        bw.Write(tc.Key);                        bw.Write(tc.IV);                        bw.Flush();                        bw.Close();                        MessageBox.Show("File Encrypted Successfully");                        txtFileName.Text = "";                    }                }            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }        }          //Decrypting         private void btnDecrypt_Click(object sender, EventArgs e)        {            try            {                openFileDialog1.Title = "Open Key File";                saveFileDialog1.Title = "Save Decrypted File";                if (openFileDialog1.ShowDialog() == DialogResult.OK)                {                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)                    {                        // The encrypted file                        FileStream fsFile = File.OpenRead(txtFileName.Text);                     // The key                        FileStream fsKey = File.OpenRead(openFileDialog1.FileName);                       // The decrypted file                        FileStream fsSave = File.Create(saveFileDialog1.FileName);                        //Prepare the encryption algorithm and read the key from the key file                        TripleDESCryptoServiceProvider csAlgo = new TripleDESCryptoServiceProvider();                        BinaryReader br = new BinaryReader(fsKey);                        csAlgo.Key = br.ReadBytes(24);                        csAlgo.IV = br.ReadBytes(8);                      // The cryptographic stream takes in the encrypted file                        CryptoStream cs = new CryptoStream(fsFile, csAlgo.CreateDecryptor(), CryptoStreamMode.Read);                         // Write the new unecrypted file                        StreamReader srCleanStream = new StreamReader(cs);                        StreamWriter swCleanStream = new StreamWriter(fsSave);                        swCleanStream.Write(srCleanStream.ReadToEnd());                        swCleanStream.Close();                        fsSave.Close();                        srCleanStream.Close();                        MessageBox.Show("File Decrypted Successfully");                    }                }            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }                    }     
```

\

![Encrypting and Decrypting Files Using C sharp](https://www.mindstick.com/mindstickarticle/371797d3-16fe-4722-aa16-ae7a08a21992/images/bf6253ad-0b22-42d1-bc8f-555eb6ee5178.png) ![Encrypting and Decrypting Files Using C sharp](https://www.mindstick.com/mindstickarticle/371797d3-16fe-4722-aa16-ae7a08a21992/images/eb139955-d605-41a7-bfc0-5e63f23484b5.png)

---

Original Source: https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
