News:

MyKidsDiary.in :: Capture your kids magical moment and create your Online Private Diary for your kids

Main Menu

Encryption and decryption in vb.net

Started by dhilipkumar, Jun 12, 2009, 06:44 PM

Previous topic - Next topic

dhilipkumar

Encryption and decryption in vb.net


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
                key.Close()
            End If
            If Not ivfile Is Nothing Then
                ivfile.Close()
            End If
            If Not cs Is Nothing Then
                cs.Close()
            End If

            If Not fs Is Nothing Then
                fs.Close()
            End If


        End Try

dhilipkumar

Declare another subroutine called decrypt which will take two arguments for decryption. This subroutine is called from the click event of bttndecrypt.


Sub decrypt(ByVal encr As SymmetricAlgorithm, ByVal include As Boolean)
        Dim fout, key, ivfile As FileStream
        Dim cs As CryptoStream
        Dim readbyte, newbyte As Integer
        fout = New FileStream(txtfilepath.Text.ToString, FileMode.Open, FileAccess.ReadWrite)

        Dim bytes1(CInt(fout.Length) - 1) As Byte


        key = New FileStream("C:\key.enc", FileMode.Open, FileAccess.ReadWrite)
        ivfile = New FileStream("C:\ivfile.enc", FileMode.Open, FileAccess.ReadWrite)
        If include Then
            Dim key1(key.Length - 1) As Byte
            Dim iv(ivfile.Length - 1) As Byte
            key.Read(key1, 0, key.Length)
            encr.Key = key1
            ivfile.Read(iv, 0, iv.Length)
            encr.IV = iv
        End If
        Try
            cs = New CryptoStream(fout, encr.CreateDecryptor(encr.Key, encr.IV), CryptoStreamMode.Read)

            Dim ascii As Encoding = Encoding.ASCII
            Do
                readbyte = cs.Read(bytes1, 0, bytes1.Length)
                If readbyte = 0 Then Exit Do
            Loop

            TextBox2.Text = "AFTER DECRYPTION YOUR FILE CONTAINS : " & vbCrLf & ascii.GetChars(bytes1)
            fout.SetLength(0)

            For newbyte = 0 To bytes1.Length - 1
                fout.WriteByte(bytes1(newbyte))
            Next
        Catch my As Exception
            MsgBox(my.Message)
        Finally
            If Not key Is Nothing Then
                key.SetLength(0)
                key.Close()
            End If
            If Not ivfile Is Nothing Then
                ivfile.SetLength(0)
                ivfile.Close()
            End If
            If Not fout Is Nothing Then
                fout.Close()
            End If


            If Not cs Is Nothing Then
                cs.Close()
            End If

        End Try


    End Sub