How to Encrypt a File in C#

Submitted by: 
Language: 
Visitors have accessed this post 1163 times.

In this tutorial, I will teach you how to encrypt a file using C#. This is different in encrypting just only a textbox, but here we will encrypt the content of a file.

Now, let's start this tutorial!

1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your project as Encrypt and Decrypt.

2. Next, add a Button named btnBrowse to browse text files for encrypting it, the insert an OpenFileDialog named openFileDialog1. Add also a button for the encryption named btnEncrypt and a textbox named txtFile for displaying the path of the textfile.
You must design your interface like this:

design

3. For btnBrowse that is used to browse text files, put this code below. This will open a file dialog and after choosing the text file, the path is displayed into txtfile.

  1. private void btnBrowse_Click(object sender, EventArgs e)
  2. {
  3. // instantiate an openfiledialog with a variable named openFileDialog1
  4. OpenFileDialog openFileDialog1 = new OpenFileDialog();
  5. //filter dialog to text
  6. openFileDialog1.Filter = "Text|*.txt|All|*.*";
  7. //show the dialog
  8. openFileDialog1.ShowDialog();
  9. //display the filename in txtFile after choosing the text file
  10. txtFile.Text = openFileDialog1.FileName;
  11. }

4. For btnEncrypt that will encrypt the data of the file, put this code below.

  1. private void btnEncrypt_Click(object sender, EventArgs e)
  2. {
  3. //Create a filestream that opens the path of the text file
  4. FileStream fsOut = File.Open(txtFile.Text, FileMode.Open);
  5. // uses the TripleDES encyption
  6. TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
  7. //links data streams to cryptographic transformations of the text file.
  8. CryptoStream cs = new CryptoStream(fsOut, tdes.CreateEncryptor(), CryptoStreamMode.Write);
  9. // instantiate a StreamWriter to write on the textfile
  10. StreamWriter sw = new StreamWriter(cs);
  11. //write content of the text file
  12. sw.WriteLine("Encrypted");
  13. // closed and flushed the streamwriter
  14. sw.Flush();
  15. sw.Close();
  16.  
  17. //create a key to as a registry entry
  18. FileStream fsKeyOut = File.Create(@"c:\encrypted.key");
  19.  
  20. //create a binary writer to a stream that supports writing strings in a specific encoding.
  21. BinaryWriter bw = new BinaryWriter(fsKeyOut);
  22.  
  23. //write the key using BinaryWriter
  24. bw.Write(tdes.Key);
  25. bw.Write(tdes.IV);
  26.  
  27. //closed and flushed the binaryWriter
  28. bw.Flush();
  29. bw.Close();
  30. }

We have created a filestream that opens the path of the text file with the function File.Open(). Then we used the TripleDES encyption and CryptoStream that links data streams to cryptographic transformations of the text file. Next, we have instantiated a StreamWriter to be able to write on the textfile and write content of the text file as "Encrypted". Therefore, the true content of the file will be "Encrypted". Then we closed and flushed the streamwriter. Next, we have created a key to as a registry entry named encrypted.key inside the C directory. We have also created a binary writer to a stream that supports writing strings in a specific encoding. And then write the key using BinaryWriter and then closed and flushed.

Note: Create a text file named data.txt as a sample. Leave this text file as empty and save it wherever you want.

Now, browse your data.txt text file.

output

Then click the button Encrypt. And you will see that the file is encrypted like this:

output

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.