Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am encrypting a large text file. I am getting a hash value before in encryption and then attaching the hash value to the file though the encryption stream as the last part. I then am decrypting the file, attempting to extract the hash value and compare it to a hash value of the file after it has been decrypted. I am also keeping the old hash vlue temporarly to see how they all three compare. Now, the character count is the same except for the before and after encrypt/decrypt phase except that the decrypted file with the hash still on is 21 characters longer than the original, not 20 as I expected. So, not if I take off just the original file protion, use the same hash algorithm on it that I did the original file and then compare it to the hash value at the end of the file and the original hash value, all of these should in theory by the same. Well for some reason, they are all three coming up different. Here is my code where I read the decrypted file and extract the hash code, etc... I still do not see where my problem is.

 

      Try 


       Dim hashSHA1 As New SHA1CryptoServiceProvider 
           Dim objNewFile As FileStream = New FileStream("C:\TestCode\tblDetlBBB.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite) 
           Dim objFileName As FileStream = New FileStream(strOutputDecrypt, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) 
           Dim rdr As New StreamReader(objFileName) 
           Dim wrt As New StreamWriter(objNewFile) 
       Dim sArray As Byte() = (New UnicodeEncoding).GetBytes(rdr.ReadToEnd) 
       Dim hashsize As Integer = hashSHA1.HashSize / 8 


       Dim msNewStream As New MemoryStream 
       Dim msStreamHass As New MemoryStream 
           msNewStream.Position = 0 
           msNewStream.Write(sArray, 0, sArray.Length - hashsize) 
           msStreamHass.Write(sArray, sArray.Length - hashsize, hashsize) 



           Dim newHash As Byte() = hashSHA1.ComputeHash(msNewStream) 
           Dim oldhash As Byte() = msStreamHass.ToArray 

       Dim A, B, C As Integer 
       For i As Integer = 0 To hashsize - 1 
           A = bytHash(i) 
           B = oldhash(i) 
           C = newHash(i) 
           MessageBox.Show(A & " " & B & " " & C) 
       Next 

       Catch ex As Exception 
           MessageBox.Show(ex.Message) 
       End Try 
   End Sub 

 

Thanks for the help.

Chester

____________________________________________

http://www.pophamcafe.com

I am starting a developers section, more tutorials than anything.

  • 2 weeks later...
  • Administrators
Posted

Firstly you could simplify the reading of the file a bit by using

   Dim hashSHA1 As New SHA1CryptoServiceProvider
           Dim objNewFile As FileStream = New FileStream("C:\TestCode\tblDetlBBB.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
           Dim objFileName As FileStream = New FileStream(strOutputDecrypt, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)

           Dim sArray(objFileName.Length - 1) As Byte

and remove the need for the StreamReader object.

 

However the problem is probably the line

msNewStream.Write(sArray, 0, sArray.Length - hashsize)

as you are not including the last 20 bytes of the original sArray, you need to allocate 20bytes more than the filesize rather than remove the last 20 bytes.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Thanks PlausiblyDamp. This is for a larger project that I am wanting to use to learn how to take redirected output from another program as a memorystream and then hash that file stream and encrypt it before writing it out to disk. It is going to be for backup purposes. I am a little weak in hashing and encryption, so I thought that I would start out by learning how the hashing and encryption works with a simple file.

 

Although school has started back now and my course load is still hectic, this has now turned into a weekend project.

 

Thank you for the assist.

 

Chester

____________________________________________

http://www.pophamcafe.com

I am starting a developers section, more tutorials than anything.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...