How to Encrypt a File in C#
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.
- private void btnBrowse_Click(object sender, EventArgs e)
- // instantiate an openfiledialog with a variable named openFileDialog1
- OpenFileDialog openFileDialog1 = new OpenFileDialog();
- //filter dialog to text
- openFileDialog1.Filter = "Text|*.txt|All|*.*";
- //show the dialog
- //display the filename in txtFile after choosing the text file
- txtFile.Text = openFileDialog1.FileName;
4. For btnEncrypt that will encrypt the data of the file, put this code below.
- 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
- TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
- //links data streams to cryptographic transformations of the text file.
- CryptoStream cs = new CryptoStream(fsOut, tdes.CreateEncryptor(), CryptoStreamMode.Write);
- // instantiate a StreamWriter to write on the textfile
- StreamWriter sw = new StreamWriter(cs);
- //write content of the text file
- // closed and flushed the streamwriter
- //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.
- BinaryWriter bw = new BinaryWriter(fsKeyOut);
- //write the key using BinaryWriter
- //closed and flushed the binaryWriter
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.
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.
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware