How to Encrypt a File in C#
Submitted by donbermoy on Saturday, June 28, 2014 - 18:36.
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:
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.
4. For btnEncrypt that will encrypt the data of the file, put this code below.
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.
Then click the button Encrypt. And you will see that the file is encrypted like this:
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
- private void btnBrowse_Click(object sender, EventArgs e)
- {
- // instantiate an openfiledialog with a variable named openFileDialog1
- //filter dialog to text
- openFileDialog1.Filter = "Text|*.txt|All|*.*";
- //show the dialog
- openFileDialog1.ShowDialog();
- //display the filename in txtFile after choosing the text file
- txtFile.Text = openFileDialog1.FileName;
- }
- private void btnEncrypt_Click(object sender, EventArgs e)
- {
- //Create a filestream that opens the path of the text file
- FileStream fsOut = File.Open(txtFile.Text, FileMode.Open);
- // uses the TripleDES encyption
- //links data streams to cryptographic transformations of the text file.
- // instantiate a StreamWriter to write on the textfile
- //write content of the text file
- sw.WriteLine("Encrypted");
- // closed and flushed the streamwriter
- sw.Flush();
- sw.Close();
- //create a key to as a registry entry
- FileStream fsKeyOut = File.Create(@"c:\encrypted.key");
- //create a binary writer to a stream that supports writing strings in a specific encoding.
- //write the key using BinaryWriter
- bw.Write(tdes.Key);
- bw.Write(tdes.IV);
- //closed and flushed the binaryWriter
- bw.Flush();
- bw.Close();
- }
Add new comment
- 857 views