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 text file in ASP.NET

Author: Richard Wand
Download Source Code : 1343_EncryptDecryptTextFile.zip

This article will explain how we can encrypt and decrypt text file in ASP.NET.

 

ASP.NET has DesCryptoServiceProvider class which provides access to cryptographic service provider of data encryption standard algorithm. This class has CreateEncryptor() and CreateDescryptor() methods to use in encryption and decryption purposes respectively. We will use FileStream class to access to our input and output file in case of encryption and StreamReader and StreamWriter classes to read and write in case of decryption. ICryptoTransform class defines basic operations for cryptographic transformation and CryptoStream class defines a stream that links data stream to cryptographic transformation.

 

  1. Create new website in Visual Studio 2010
  2. Add two Buttons and a Label in aspx page of a web form

     
    <asp:Button ID="btnEncrypt" runat="server" Text="Encrypt File"
     
        onclick="btnEncrypt_Click" />
     
    <br />
     
    <br />
     
    <asp:Button ID="btnDecrypt" runat="server" Text="Decrypt File"
     
        onclick="btnDecrypt_Click" />
     
    <br />
     
    <br />
     
    <asp:Label ID="lblMessage" runat="server" ></asp:Label>
     
  3. Create three text files in your C drive with these names

    MyFile.txt (File with some text data to encrypt)
    EncryptedMyFile.txt (File that will have Encrypted data after encryption, empty at the start)
    DecryptedMyFile.txt (File that will have same text as MyFile.txt after decryption, empty at the start)
     
  4. We have to use following namespaces in our code

    C#
     
    using System.Security.Cryptography;
    using System.Text;
    using System.IO;
     
    VB.NET
     
    Imports System.Security.Cryptography
    Imports System.IO
    Imports System.Text
     
  5. Now Add a Button click event for btnEncrypt and write code below in it.

    C#
     
    protected void btnEncrypt_Click(object sender, EventArgs e)
    {
        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
        provider.Key = ASCIIEncoding.ASCII.GetBytes("11224488");
        provider.IV = ASCIIEncoding.ASCII.GetBytes("11224488");
     
        //DESCryptoServiceProvider provider = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
     
        ICryptoTransform transform = provider.CreateEncryptor(provider.Key, provider.IV);
     
        FileStream inputStream = new FileStream("C:\\MyFile.txt", FileMode.Open, FileAccess.Read);
        FileStream outputStream = new FileStream("C:\\EncryptedMyFile.txt", FileMode.Open, FileAccess.Write);
     
        CryptoStream cryptoStream = new CryptoStream(outputStream, transform, CryptoStreamMode.Write);
     
        byte[] byteInput = new byte[inputStream.Length];
        inputStream.Read(byteInput, 0, byteInput.Length);
        cryptoStream.Write(byteInput, 0, byteInput.Length);
        cryptoStream.Close();
        outputStream.Close();
        inputStream.Close();
     
        lblMessage.Text = "File is Encrypted Successfully";
    }
     
    VB.NET
     
    Protected Sub btnEncrypt_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnEncrypt.Click
        Dim provider As New DESCryptoServiceProvider()
        provider.Key = ASCIIEncoding.ASCII.GetBytes("11224488")
        provider.IV = ASCIIEncoding.ASCII.GetBytes("11224488")
     
       'DESCryptoServiceProvider provider = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
     
        Dim transform As ICryptoTransform = provider.CreateEncryptor(provider.Key, provider.IV)
     
        Dim inputStream As New FileStream("C:\MyFile.txt", FileMode.Open, FileAccess.Read)
        Dim outputStream As New FileStream("C:\EncryptedMyFile.txt", FileMode.Open, FileAccess.Write)
     
        Dim cryptoStream As New CryptoStream(outputStream, transform, CryptoStreamMode.Write)
     
        Dim byteInput As Byte() = New Byte(inputStream.Length - 1) {}
        inputStream.Read(byteInput, 0, byteInput.Length)
        cryptoStream.Write(byteInput, 0, byteInput.Length)
        cryptoStream.Close()
        outputStream.Close()
        inputStream.Close()
     
        lblMessage.Text = "File is Encrypted Successfully"
    End Sub
     
    Create an instance of DesCryptoServiceProvider class and set the “Key” and “IV” properties of the class. Secret key must be 64 bits or 8 byte long. You can also create Key and IV automatically by using Create() method of DesCryptoServiceProvider class. Uncomment the line commented above to create automatic key and IV. Create an instance of ICryptoTransform class and call the CreateEncryptor() method by providing secret Key and IV. Create two instances of FileStream class for input file and output file. We have to create CryptoStream class instance to write encrypted data to file.
     
  6. Start debugging and click on Encrypt button. Open EncryptedMyFile.txt and see encrypted data.
     
  7. Now Add another Button click event for btnDecrypt and write code below in it.

    C#
     
    protected void btnDecrypt_Click(object sender, EventArgs e)
    {
        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
     
        provider.Key = ASCIIEncoding.ASCII.GetBytes("11224488");
        provider.IV = ASCIIEncoding.ASCII.GetBytes("11224488");
           
        FileStream encryptStream = new FileStream("C:\\EncryptedMyFile.txt", FileMode.Open, FileAccess.Read);
        ICryptoTransform transform = provider.CreateDecryptor(provider.Key, provider.IV);
        CryptoStream cryptoStream = new CryptoStream(encryptStream, transform, CryptoStreamMode.Read);
     
        StreamReader reader = new StreamReader(cryptoStream);
        StreamWriter writer = new StreamWriter("C:\\DecryptedMyFile.txt");
        writer.Write(reader.ReadToEnd());
        cryptoStream.Close();
        encryptStream.Close();
        reader.Close();
        writer.Close();
     
        lblMessage.Text = "File is Decrypted Successfully";
    }
     
    VB.NET
     
    Protected Sub btnDecrypt_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDecrypt.Click
        Dim provider As New DESCryptoServiceProvider()
     
        provider.Key = ASCIIEncoding.ASCII.GetBytes("11224488")
        provider.IV = ASCIIEncoding.ASCII.GetBytes("11224488")
     
        Dim encryptStream As New FileStream("C:\EncryptedMyFile.txt", FileMode.Open, FileAccess.Read)
        Dim transform As ICryptoTransform = provider.CreateDecryptor(provider.Key, provider.IV)
        Dim cryptoStream As New CryptoStream(encryptStream, transform, CryptoStreamMode.Read)
     
        Dim reader As New StreamReader(cryptoStream)
        Dim writer As New StreamWriter("C:\DecryptedMyFile.txt")
        writer.Write(reader.ReadToEnd())
        cryptoStream.Close()
        encryptStream.Close()
        reader.Close()
        writer.Close()
     
        lblMessage.Text = "File is Decrypted Successfully"
    End Sub
     
    Create an instance of DesCryptoServiceProvider class and set the “Key” and “IV” properties of the class in same as we have in encryption code. The Key and IV must be same for encryption and decryption. In case you have created Key and IV automatically, they must be same for decryption purposes. Create an instance of ICryptoTransform class and call CreateDecryptor() method. We have to create instances for CreateReader and CreateWrite classes to read encrypted data from a file and write decrypted data to a file.
     
  8. Now start debugging again and click on Decrypt button. Open DecryptedMyFile.txt and see text data.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to bind multidimensional array to GridView in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net