Wednesday, January 19, 2011

How to check file is Tempering or not with existing file


We can see if the file is tempering or not using Cryptography

Code:


Imports System.IO
Imports System.Security.Cryptography
Public Class Form1
   
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Application.Exit()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.Title = "Open File"
        OpenFileDialog1.Filter = "Files (*.*)|*.*"
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then
            Exit Sub
        End If
        Dim sFilePath As String = OpenFileDialog1.FileName
        If System.IO.File.Exists(sFilePath) = False Then
            sFilePath = ""
            Exit Sub
        Else
            TextBox1.Text = sFilePath
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        OpenFileDialog1.Title = "Open File"
        OpenFileDialog1.Filter = "Files (*.*)|*.*"
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then
            Exit Sub
        End If
        Dim sFilePath As String = OpenFileDialog1.FileName
        If System.IO.File.Exists(sFilePath) = False Then
            sFilePath = ""
            Exit Sub
        Else
            TextBox2.Text = sFilePath
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim myHash As HashAlgorithm
        myHash = HashAlgorithm.Create()
        If TextBox2.Text = String.Empty Or Me.TextBox1.Text = String.Empty Then
            MessageBox.Show("Set all form fields prior to initiating a test", _
            "Missing Form Data", MessageBoxButtons.OK)
        End If
        Dim fs1 As New FileStream(TextBox2.Text, FileMode.OpenOrCreate)
        Dim fs1Bytes As Byte() = New Byte(fs1.Length) {}
        fs1.Read(fs1Bytes, 0, fs1.Length)
        Dim arr1() As Byte = myHash.ComputeHash(fs1Bytes)
        fs1.Close()
        Dim fs2 As New FileStream(TextBox1.Text, FileMode.OpenOrCreate)
        Dim fs2Bytes As Byte() = New Byte(fs2.Length) {}
        fs2.Read(fs2Bytes, 0, fs2.Length)
        Dim arr2() As Byte = myHash.ComputeHash(fs2Bytes)
        fs2.Close()
        If BitConverter.ToString(arr1) = BitConverter.ToString(arr2) Then
            MessageBox.Show("The file examined has not been tampered with.", "Hash" & _
            "Test Passed")
            'display comparison
            MessageBox.Show("Original Hash: " & Environment.NewLine &
            BitConverter.ToString(arr1) & _
            Environment.NewLine & _
            "Test Hash: " & Environment.NewLine & _
            BitConverter.ToString(arr2), "Hash Test Results")
        Else
            MessageBox.Show("The file examined has been tampered with.", "Hash Test Failed")
            'display comparison
            MessageBox.Show("Original Hash: " & Environment.NewLine &
            BitConverter.ToString(arr1) & _
            Environment.NewLine & _
            "Test Hash: " & Environment.NewLine & _
            BitConverter.ToString(arr2), "Hash Test Results")
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'Dim myhash As Byte
        'Dim fs1 As New FileStream(TextBox2.Text, FileMode.OpenOrCreate)
        'Dim fs1Bytes As Byte() = New Byte(fs1.Length) {}
        'fs1.Read(fs1Bytes, 0, fs1.Length)
        'Dim arr1() As Byte = myhash.CompareTo(fs1Bytes)
        'fs1.Close()
        Dim fs1 As New FileStream(TextBox2.Text, FileMode.OpenOrCreate)
        Dim fs1Bytes As Byte() = New Byte(fs1.Length) {}
        fs1.Read(fs1Bytes, 0, fs1.Length)
        fs1.Close()
        Dim fs2 As New FileStream(TextBox1.Text, FileMode.OpenOrCreate)
        Dim fs2Bytes As Byte() = New Byte(fs2.Length) {}
        fs2.Read(fs2Bytes, 0, fs2.Length)
        fs2.Close()
        Dim i As Integer = 0
        For i = 0 To fs1Bytes.Length - 1
            If Not fs1Bytes(i) = fs2Bytes(i) Then
                MessageBox.Show("The file examined has been tampered with at position " & _
                i.ToString(), "Byte Test Failed")
                Exit Sub
            End If
        Next
        MessageBox.Show("The file examined has not been tampered with.", "Byte Test Passed")
    End Sub

End Class


No comments:

Post a Comment