Description:-
In this Article we will see FileTamperingCheck using windows form. Here we will also check for byte test to check file bytes or hash code using windows form.
FileStream fs1 = new FileStream(txtTestFile.Text, FileMode.OpenOrCreate);
In this Article we will see FileTamperingCheck using windows form. Here we will also check for byte test to check file bytes or hash code using windows form.
Now Generate Source
Browse button click event to open Dialog for Open File.
private void btnBrowseSrc_Click(System.Object sender, System.EventArgs
e)
{
OpenFileDialog1.Title = "Open File";
OpenFileDialog1.Filter = "Files (*.*)|*.*";
if
(OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string
sFilePath = OpenFileDialog1.FileName;
if
(System.IO.File.Exists(sFilePath) == false)
{
sFilePath = "";
return;
}
else
{
txtSourceFile.Text =
sFilePath;
}
}
Same here to Generate
Test browse button to open Test file.
private void btnBrowseTest_Click(System.Object sender, System.EventArgs
e)
{
OpenFileDialog1.Title = "Open File";
OpenFileDialog1.Title = "Open File";
OpenFileDialog1.Filter = "Files (*.*)|*.*";
if
(OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string
sFilePath = OpenFileDialog1.FileName;
if
(System.IO.File.Exists(sFilePath) == false)
{
sFilePath = "";
return;
}
else
{
txtTestFile.Text =
sFilePath;
}
}
Now generate Hash
Test button click event to Code for Check Length, Bytes, and HashCode to compare
it to Other File.
private void btnTest_Click(System.Object
sender, System.EventArgs e)
{
HashAlgorithm
myHash;
myHash = HashAlgorithm.Create();
if
(txtTestFile.Text == string.Empty || this.txtSourceFile.Text == string.Empty)
{
MessageBox.Show("Set all form fields prior to initiating a
test", "Missing Form Data",
MessageBoxButtons.OK);
}
FileStream fs1 = new FileStream(txtTestFile.Text, FileMode.OpenOrCreate);
byte[]
fs1Bytes = new byte[fs1.Length
+ 1];
fs1.Read(fs1Bytes, 0, (int)fs1.Length);
byte[]
arr1 = myHash.ComputeHash(fs1Bytes);
fs1.Close();
FileStream
fs2 = new FileStream(txtSourceFile.Text,
FileMode.OpenOrCreate);
byte[]
fs2Bytes = new byte[fs2.Length
+ 1];
fs2.Read(fs2Bytes, 0, (int)fs2.Length);
byte[]
arr2 = myHash.ComputeHash(fs2Bytes);
fs2.Close();
if
(BitConverter.ToString(arr1) == BitConverter.ToString(arr2))
{
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");
}
}
Now Generate Byte
Test button Click event to compare Bytes, Length of Both Files.
private void btnByteCompare_Click(System.Object sender, System.EventArgs
e)
{
FileStream
fs1 = new FileStream(txtTestFile.Text,
FileMode.OpenOrCreate);
byte[]
fs1Bytes = new byte[fs1.Length+
1];
fs1.Read(fs1Bytes, 0, (int) fs1.Length);
fs1.Close();
FileStream
fs2 = new FileStream(txtSourceFile.Text,
FileMode.OpenOrCreate);
byte[]
fs2Bytes = new byte[fs2.Length+
1];
fs2.Read(fs2Bytes, 0, (int) fs2.Length);
fs2.Close();
int
i = 0;
for
(i = 0; i <= fs1Bytes.Length - 1; i++)
{
if (!System.Convert.ToBoolean(fs1Bytes[i]
== fs2Bytes[i]))
{
MessageBox.Show("The
file examined has been tampered with at position " + i.ToString(), "Byte Test Failed");
return;
}
}
MessageBox.Show("The file examined has not been tampered with.",
"Byte Test Passed");
}
Now you are done to
Test Application run your windows application and Select both files and test
it.
Thanks for comments.....