articles

Home / DeveloperSection / Articles / Encrypting and Decrypting Files Using C sharp

Encrypting and Decrypting Files Using C sharp

Anonymous User32639 14-Jul-2010

Here I’m going to show how to encrypt and decrypt text file using C#.Net

Encrypting and Decrypting Files Using C sharp

Example
//namespace for Input Output file stream
using System.IO;
//name space for Cryptography
using System.Security.Cryptography;
privatevoid 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 
        privatevoid 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 = newTripleDESCryptoServiceProvider();
// this object links data streams to cryptographic values
CryptoStream cs = newCryptoStream(fs, tc.CreateEncryptor(), CryptoStreamMode.Write);
// This stream reader will read the file to encrypt
                        StreamReader sr = newStreamReader(txtFileName.Text);
// this stream writer will write the new file
                        StreamWriter sw = newStreamWriter(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 = newBinaryWriter(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 
        privatevoid 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 = newTripleDESCryptoServiceProvider();
                        BinaryReader br = newBinaryReader(fsKey);
                        csAlgo.Key = br.ReadBytes(24);
                        csAlgo.IV = br.ReadBytes(8);
// The cryptographic stream takes in the encrypted file
CryptoStream cs = newCryptoStream(fsFile, csAlgo.CreateDecryptor(), CryptoStreamMode.Read);
// Write the new unecrypted file
                        StreamReader srCleanStream = newStreamReader(cs);
StreamWriter swCleanStream = newStreamWriter(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 sharpEncrypting and Decrypting Files Using C sharp

 

 


I am a content writter !

Leave Comment

Comments

Liked By