Monday, January 03, 2011

Unleash the power of .NET with encryption

Introduction
Security is the prime issue for personal documents. How can we protect personal files and folders from malicious users? Is there any other method apart from the password protection of folder? Well we can encrypt our documents to provide better protection .Once we encrypt our document it is difficult for other user to read it�s content .Even if they add or append any thing to the document nothing harm will happened to the main content as when we will decrypt the same we will get the content in text format except the newly added part which will be displayed in some non formal text.
Ex. All files with .txt extension. This article depicts in detail how to encrypt and decrypt file or folder with �RIJNDAEL� encryption method. You can use this method to encrypt .txt, .bmp, .jpg, .mdb, .wav, MP3 files etc.

Create a new Windows Application project in Visual Studio.NET. The Form will contain five buttons, four textboxes, one imagelist, one treeview and three labels. Add controls to the Form with the following settings:

Type
Name
Type
Name
Textbox1
txtpasswd
Button2
bttnencrypt
Textbox2
Textbox1
Button3
bttndecrypt
Textbox3
Textbox2
Button4
bttnrefresh
Textbox4
txtfilepath
Button5
bttncls
Button1
bttnpath
Label1
lblfilepath

Code:
Now it�s time to delve into the code. First add the following import statements
Imports System.Security.Cryptography
Imports System.Security
Imports System.IO
Imports System.Text 'TO GET ASCII CONVERSION
Imports System.Drawing.Drawing2D 'TO GET ALL BRUSHES
Imports System.Drawing.Graphics
Imports System.Drawing
Imports System.Security.Permissions
First two import statements are required for encryption and decryption process. Last import statement is required for Code Access Security (CAS).

Next we will create a subroutine for encryption. This subroutine is called from bttnencrypt.

   Sub encrypt(ByVal encr As SymmetricAlgorithm)
       Dim fs, key, ivfile As FileStream
       Dim cs As CryptoStream
       Dim fileperm As New FileIOPermission(PermissionState.Unrestricted)
       Dim regperm As New RegistryPermission(PermissionState.Unrestricted)
       Dim uiperm As New UIPermission(PermissionState.Unrestricted)
       Dim newperm As New PermissionSet(PermissionState.None)
       newperm.AddPermission(fileperm)
       newperm.AddPermission(regperm)
       newperm.AddPermission(uiperm)
       newperm.PermitOnly()
       Try
           fs = New FileStream(txtfilepath.Text.ToString, FileMode.Open, FileAccess.ReadWrite)
       Catch my As Exception
           MsgBox("Following Errors Occured: " & vbCrLf & my.Message & vbCrLf & my.Source)
           Exit Sub
       End Try
       Dim bytes(CInt(fs.Length) - 1) As Byte
       key = New FileStream("C:\key.enc", FileMode.OpenOrCreate)
       ivfile = New FileStream("C:\ivfile.enc", FileMode.OpenOrCreate)
       Try
           encr.Key = keygen()
           key.Write(encr.Key, 0, encr.Key.Length)
           ivfile.Write(encr.IV, 0, encr.IV.Length)
           cs = New CryptoStream(fs, encr.CreateEncryptor(encr.Key, encr.IV), CryptoStreamMode.Write)
           Dim i As Integer
           Dim ascii As Encoding = Encoding.ASCII
           For i = 0 To fs.Length - 1
               bytes(i) = fs.ReadByte
           Next
           TextBox1.Text = "BEFORE ENCRYPTION YOUR FILE CONTAINS :" & vbCrLf & ascii.GetChars(bytes)
           fs.SetLength(0)
           MsgBox("CHECK FILE", MsgBoxStyle.OKOnly)
           cs.Write(bytes, 0, bytes.Length)
       Catch my As Exception
           MsgBox(my.Message)
       Finally
           If Not key Is Nothing Then

Read more: Codeproject