Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to ENCRYPT and DECRYPT file using SYSTEM.IO namespace in c# (.net 2.0)

Author: Zunnair
Download Source Code : 839_io.zip

In this simple article you will learn how to encrypt and decrypt file using FILE class

 File class provide us a static functions Encrypt() and Decrypt(). These are static functions so they can be called by class name.

Encrypt a File:

We can encrypt a file by using file class function Encrypt(). This function only works on NTFS file system. Other wise it will through an exception.

Encrypt() function takes file path as a parameter and encrypts the file. It returns nothing.

To demonstrate make a window application. Drag one text box and one button on the form. Write file path (like c:\\abc.txt) in the textbox and press button.

Now write the following code on button click event:

C#

private void btn_Encrypt_Click(object sender, EventArgs e)

        {

            str_Path = txtPath.Text;

            if (str_Path != null)

            {

                if (File.Exists(str_Path) == true)

                {

                    File.Encrypt(str_Path);

                    MessageBox.Show("ENCRYPTED");

                }

                else

                {

                    MessageBox.Show("FILE DOES NOT EXIST");

                }

            }

            else

            {

                MessageBox.Show("please write path");

            }

        }

VB

Private Sub btn_Encrypt_Click(ByVal sender As Object, ByVal e As EventArgs)

        str_Path = txtPath.Text

        If str_Path IsNot Nothing Then

            If File.Exists(str_Path) = True Then

                File.Encrypt(str_Path)

                MessageBox.Show("ENCRYPTED")

            Else

                MessageBox.Show("FILE DOES NOT EXIST")

            End If

        Else

            MessageBox.Show("please write path")

        End If

    End Sub

This is a simple code to encrypt a file.

Decrypt a File:

File class also provide us a facility to decrypt the encrypted file. Like encryption function Decrypt() also work on NTFS file system. Decryption can only be done by same account by which encryption was done.

It takes file path as a parameter and decrypts the file. It returns nothing.

To demonstrate make a window application. Drag one text box and one button on the form. Write file path (like c:\\abc.txt) in the textbox and press button.

Now write the following code on button click event:

C#

private void btn_Decrypt_Click(object sender, EventArgs e)

        {

            str_Path = txtPath.Text;

            if (str_Path != null)

            {

                if (File.Exists(str_Path) == true)

                {

                    File.Decrypt(str_Path);

                    MessageBox.Show("DCRYPTED");

                }

                else

                {

                    MessageBox.Show("FILE DOES NOT EXIST");

                }

            }

            else

            {

                MessageBox.Show("please write path");

            }

        }

VB

rivate Sub btn_Decrypt_Click(ByVal sender As Object, ByVal e As EventArgs)

        str_Path = txtPath.Text

        If str_Path IsNot Nothing Then

            If File.Exists(str_Path) = True Then

                File.Decrypt(str_Path)

                MessageBox.Show("DCRYPTED")

            Else

                MessageBox.Show("FILE DOES NOT EXIST")

            End If

        Else

            MessageBox.Show("please write path")

        End If

    End Sub

This simple code decrypts a file.

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {

 

            this.Text = "Devasp.net FILE HANDLING Application";

        }

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

 

        Me.Text = "Devasp.net FILE HANDLING Application"

 End Sub

 

This simple article tells how to encrypt and decrypt file using FILE class in C# .net 2.0.

Article Comments
this article Encrypt & Decrypt Not Working

Posted on 4/21/2010 8:05:14 AM by suresh

hello zunnair, what kind of encryption uses File.Encrypt(), is the strength similar to algorhytms used by gpg

Posted on 4/22/2010 6:28:59 AM by Horst Huepenbecker

very good it helps me lot ,Thanks

Posted on 5/20/2010 7:50:53 AM by zahid

Seriously,
Because the file has been encrypted and the message says something's encrypted I' supposed see that this is how something works? How about oipening the encrypted file and show me the gobblydegook that noone can understand because it's been encrypted?

Posted on 11/12/2010 5:16:51 PM by na

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to get and set file creation date and time using “FILE CLASS” in c# (.net 2.0)

Disclaimer - Privacy
© 2002-2017 DevASP.net