By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

Checksum, CRC or something better?

P: n/a
Bob
Hi there,

I am working on an application to be used in our local Forensics
department...

Among other things the app will connect to a digital camera and download the
images to the hard drive. For various reasons I need to be able to say with
certainty that the image on the hard drive is exactly what was on the
camera... any ideas on best way to achieve this...?

I could do a checksum of each image and compare this but not sure how to
implement in code or if there is a better way. Merely checking the file size
matches is not sufficient to stand up in Court.

Any suggestions appreciated, especially if sample code included (can be
classic VB if necessary). Does .Net perhaps have something like this built
into the Framework?

Cheers
Nov 20 '05 #1
Share this Question
Share on Google+
24 Replies


P: n/a

"Bob" <bo*@nospam.com> skrev i en meddelelse
news:eq**************@TK2MSFTNGP12.phx.gbl...
Hi there,

I am working on an application to be used in our local Forensics
department...

Among other things the app will connect to a digital camera and download the images to the hard drive. For various reasons I need to be able to say with certainty that the image on the hard drive is exactly what was on the
camera... any ideas on best way to achieve this...?

I could do a checksum of each image and compare this but not sure how to
implement in code or if there is a better way. Merely checking the file size matches is not sufficient to stand up in Court.

Any suggestions appreciated, especially if sample code included (can be
classic VB if necessary). Does .Net perhaps have something like this built
into the Framework?

Cheers


Hi Bob,
Have a look at
http://www.vbaccelerator.com/home/NE...32/article.asp ,
maybe you can use some of this.

--
Lasse
Nov 20 '05 #2

P: n/a
Hi Bob,

Here is write a sample for you.

Imports System
Imports System.IO
Imports System.Security.Cryptography
Module Module1
Sub Main()
Dim fs As FileStream = New FileStream("c:\dafen.jpg",
FileMode.Open, FileAccess.Read)
Dim arr(fs.Length) As Byte
fs.Read(arr, 0, fs.Length)
fs.Close()
Dim tmpHash() As Byte
tmpHash = New MD5CryptoServiceProvider().ComputeHash(arr)
fs = New FileStream("c:\dafen1.jpg", FileMode.Open, FileAccess.Read)
fs.Read(arr, 0, fs.Length)
Dim tmpNewHash() As Byte
tmpNewHash = New MD5CryptoServiceProvider().ComputeHash(arr)
Dim bEqual As Boolean
If tmpNewHash.Length = tmpHash.Length Then
Dim i As Integer
Do While (i < tmpNewHash.Length) AndAlso (tmpNewHash(i) =
tmpHash(i))
i += 1
Loop
If i = tmpNewHash.Length Then
bEqual = True
End If
End If
If bEqual Then
Console.WriteLine("The two hash values are the same")
Else
Console.WriteLine("The two hash values are not the same")
End If
End Sub
End Module

HOW TO: Compute and Compare Hash Values by Using Visual Basic .NET
http://support.microsoft.com/default...b;en-us;301053

If you have any related question, you may post the question here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
From: "Bob" <bo*@nospam.com>
Subject: Checksum, CRC or something better?
Date: Tue, 21 Oct 2003 16:35:12 +1000
Lines: 21
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <eq**************@TK2MSFTNGP12.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: 202-44-189-210.nexnet.net.au 202.44.189.210
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:148560
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Hi there,

I am working on an application to be used in our local Forensics
department...

Among other things the app will connect to a digital camera and download theimages to the hard drive. For various reasons I need to be able to say with
certainty that the image on the hard drive is exactly what was on the
camera... any ideas on best way to achieve this...?

I could do a checksum of each image and compare this but not sure how to
implement in code or if there is a better way. Merely checking the file sizematches is not sufficient to stand up in Court.

Any suggestions appreciated, especially if sample code included (can be
classic VB if necessary). Does .Net perhaps have something like this built
into the Framework?

Cheers


Nov 20 '05 #3

P: n/a
Hi Bob,

You are right... what you want to do must be done correctly... no mistakes allowed.

Checksums should be used only for error correction, not where evil people may come in. Checksums are simple functions, that can be exploited to create fake documents with the same checksum.

You should use an hash algorithm. An hash algorithm works like a checksum; you provide the input (any file or any buffer with any length), and the algorithm returns the hash value (also called digest, a fixed size array of bytes). The difference is hash algorithms are complex cryptographic functions, and there are several commonly accepted as completely secure. Technically an hash algorithm is an one-way function; a mathematical function that can't be reversed; so that the digest can't be reversed to get the input (or know anything about it). A single bit difference in the input results in a completly different digest. Most important, it's not possible to find another input that computes to the same hash. Hash algorithms are widely used for data integrity verification.

You don't need to implement the hash algorithm, because the Framework already has several (MD5, SHA, with different digest sizes, in bits). SHA512Managed for instance returns a 64 bytes digest for any file or buffer you supply as input. Example:

Dim path As String = "c:\myfile.dat"
Dim stream As New IO.FileStream(path, IO.FileAccess.Read)
Dim sha As New Security.Cryptography.SHA512Managed
Dim digest() As Byte = sha.ComputeHash(stream)

If you are not going to store the digest as a file, and prefer a string for a database, convert it to Base64 encoding:
Dim hashString As String = Convert.ToBase64String(digest)

That's it. When you want to verify if a file if the same or was changed (trojan horse, for instance), you compute the hash and compare the new digest with the one you had stored. If they match, you can be sure the file is exactly the same, if they dont, you are also sure it was changed somehow.

Forensics is another story... hash is good to check data integrity, but that's only a small piece of the big puzzle; computer forensics is a very complex world. Most jobs must be done in a lab, and require special hardware, to make data recovery readings on hard disks. It's possible to compromise a system for a while, and then return it exactly the way it was, leaving no tracks... Hash can tell you the system is no longer compromised, but not what nasty stuff was done before settings and files returned to normal.

To stand up in Court... it depends of what is standing up for. If you want to prove two files are different, my answer is yes, the Court will accept it based on the hash values. Hashes are on the base of digital signatures and code signing certificates; where another major technology is present, public key cryptography; and that stand up too. But that's it. Hash values are no proof that it was Alice who hacked Bob's server, or that the keylogger found in the office PC was placed by the boss. Private forensics may not stand up in court, and are not recommended if you are taking legal actions. If you want to go the legal way, let the police do the forensics. If you just want to know fast what was done on your system, save any important data that may be there, and format so that the system returns quickly into business again, take it to a private lab... one way or another, dont do forensics on serious cases if you are no expert.

Regards,
Mario
"Bob" <bo*@nospam.com> wrote in message news:eq**************@TK2MSFTNGP12.phx.gbl...
Hi there,

I am working on an application to be used in our local Forensics
department...

Among other things the app will connect to a digital camera and download the
images to the hard drive. For various reasons I need to be able to say with
certainty that the image on the hard drive is exactly what was on the
camera... any ideas on best way to achieve this...?

I could do a checksum of each image and compare this but not sure how to
implement in code or if there is a better way. Merely checking the file size
matches is not sufficient to stand up in Court.

Any suggestions appreciated, especially if sample code included (can be
classic VB if necessary). Does .Net perhaps have something like this built
into the Framework?

Cheers

Nov 20 '05 #4

P: n/a
Hi,

Actually, the other answers that you received are on target, technically.
However, they do nothing to address the legal issue...

And, there is no way to do what you want -- UNLESS the camera ITSELF
generates the CRC. Your software could then compare and store (perhaps
encrypted) the results. The issue here is that there is no chain of
evidence with a conventional digital camera. You only have the image
information AFTER it is stored on the PC. And, the original image may be
purported to have come from a camera, but... How do you prove that?

A better legal remedy would be to capture the image, encrypt it with a
digital certificate, and include a copy of some sort of (other) written
document (probably a physically signed image) that certifies the actual
chain of handling. You should talk to you legal eagles to get a specific
recommendation on this additional work.

BTW, this all is IMO. I'm not a lawyer.

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
Nov 20 '05 #5

P: n/a
* "Bob" <bo*@nospam.com> scripsit:
Among other things the app will connect to a digital camera and download the
images to the hard drive. For various reasons I need to be able to say with
certainty that the image on the hard drive is exactly what was on the
camera... any ideas on best way to achieve this...?


You can use this code to calculate a CRC checksum:

<http://vbaccelerator.com/article.asp?id=930>

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #6

P: n/a
On Tue, 21 Oct 2003 16:35:12 +1000, "Bob" <bo*@nospam.com> wrote:
Hi there,

I am working on an application to be used in our local Forensics
department...

Among other things the app will connect to a digital camera and download the
images to the hard drive. For various reasons I need to be able to say with
certainty that the image on the hard drive is exactly what was on the
camera... any ideas on best way to achieve this...?

I could do a checksum of each image and compare this but not sure how to
implement in code or if there is a better way. Merely checking the file size
matches is not sufficient to stand up in Court.

Any suggestions appreciated, especially if sample code included (can be
classic VB if necessary). Does .Net perhaps have something like this built
into the Framework?

Cheers


You need a digital camera that supports watermarking and/or
signatures. As hinted by the other guys in this thread, the jury's
still out on aspects of this technology.

refs: http://citeseer.nj.nec.com/context/381517/0

I've had a quick look for suitable cameras, but haven't had any luck.
I'm sure someone manufactures them, as the problem was identified
years ago... Best of luck.

Rgds,
Nov 20 '05 #7

P: n/a
Bob
Hi Peter,

Thanks for the code... I'll check it out and see how I go...

Cheers

"Peter Huang [MSFT]" <v-******@online.microsoft.com> wrote in message
news:FO**************@cpmsftngxa06.phx.gbl...
Hi Bob,

Here is write a sample for you.

Imports System
Imports System.IO
Imports System.Security.Cryptography
Module Module1
Sub Main()
Dim fs As FileStream = New FileStream("c:\dafen.jpg",
FileMode.Open, FileAccess.Read)
Dim arr(fs.Length) As Byte
fs.Read(arr, 0, fs.Length)
fs.Close()
Dim tmpHash() As Byte
tmpHash = New MD5CryptoServiceProvider().ComputeHash(arr)
fs = New FileStream("c:\dafen1.jpg", FileMode.Open, FileAccess.Read) fs.Read(arr, 0, fs.Length)
Dim tmpNewHash() As Byte
tmpNewHash = New MD5CryptoServiceProvider().ComputeHash(arr)
Dim bEqual As Boolean
If tmpNewHash.Length = tmpHash.Length Then
Dim i As Integer
Do While (i < tmpNewHash.Length) AndAlso (tmpNewHash(i) =
tmpHash(i))
i += 1
Loop
If i = tmpNewHash.Length Then
bEqual = True
End If
End If
If bEqual Then
Console.WriteLine("The two hash values are the same")
Else
Console.WriteLine("The two hash values are not the same")
End If
End Sub
End Module

HOW TO: Compute and Compare Hash Values by Using Visual Basic .NET
http://support.microsoft.com/default...b;en-us;301053

If you have any related question, you may post the question here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
From: "Bob" <bo*@nospam.com>
Subject: Checksum, CRC or something better?
Date: Tue, 21 Oct 2003 16:35:12 +1000
Lines: 21
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <eq**************@TK2MSFTNGP12.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: 202-44-189-210.nexnet.net.au 202.44.189.210
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:148560
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Hi there,

I am working on an application to be used in our local Forensics
department...

Among other things the app will connect to a digital camera and download

the
images to the hard drive. For various reasons I need to be able to say withcertainty that the image on the hard drive is exactly what was on the
camera... any ideas on best way to achieve this...?

I could do a checksum of each image and compare this but not sure how to
implement in code or if there is a better way. Merely checking the file

size
matches is not sufficient to stand up in Court.

Any suggestions appreciated, especially if sample code included (can be
classic VB if necessary). Does .Net perhaps have something like this builtinto the Framework?

Cheers

Nov 20 '05 #8

P: n/a
Bob
Mario,

I was just after suggestions on how best to compare 2 files and guarantee they are identical... the actual use is for fingerprint images taken at crime scenes by Forensic Scene Of Crime Officers (SOCOs).

I am writing software that handles getting the images off the camera, making sure the local copy matches exactly and package it with case notes and the hash or whatever data etc.Then I'll be using remoting or similar to transfer back to base for fingerprint identification.

The chain of evidence will be preserved as much as is possible by ensuring the images are written to the hard drive in a folder that the SOCO has no access to - which just covers their butts really. Then once a job is submitted only the fingerprint experts will have access to it.

All the legal stuff has been sorted out but I couldn't give a rats about that... I'm just interested in the technology side of things...

Cheers for the info...

"Mario" <mz******@DONTWANTSPAMmail.pt> wrote in message news:eb*************@TK2MSFTNGP10.phx.gbl...
Hi Bob,

You are right... what you want to do must be done correctly... no mistakes allowed.

Checksums should be used only for error correction, not where evil people may come in. Checksums are simple functions, that can be exploited to create fake documents with the same checksum.

You should use an hash algorithm. An hash algorithm works like a checksum; you provide the input (any file or any buffer with any length), and the algorithm returns the hash value (also called digest, a fixed size array of bytes). The difference is hash algorithms are complex cryptographic functions, and there are several commonly accepted as completely secure. Technically an hash algorithm is an one-way function; a mathematical function that can't be reversed; so that the digest can't be reversed to get the input (or know anything about it). A single bit difference in the input results in a completly different digest. Most important, it's not possible to find another input that computes to the same hash. Hash algorithms are widely used for data integrity verification.

You don't need to implement the hash algorithm, because the Framework already has several (MD5, SHA, with different digest sizes, in bits). SHA512Managed for instance returns a 64 bytes digest for any file or buffer you supply as input. Example:

Dim path As String = "c:\myfile.dat"
Dim stream As New IO.FileStream(path, IO.FileAccess.Read)
Dim sha As New Security.Cryptography.SHA512Managed
Dim digest() As Byte = sha.ComputeHash(stream)

If you are not going to store the digest as a file, and prefer a string for a database, convert it to Base64 encoding:
Dim hashString As String = Convert.ToBase64String(digest)
That's it. When you want to verify if a file if the same or was changed (trojan horse, for instance), you compute the hash and compare the new digest with the one you had stored. If they match, you can be sure the file is exactly the same, if they dont, you are also sure it was changed somehow.

Forensics is another story... hash is good to check data integrity, but that's only a small piece of the big puzzle; computer forensics is a very complex world. Most jobs must be done in a lab, and require special hardware, to make data recovery readings on hard disks. It's possible to compromise a system for a while, and then return it exactly the way it was, leaving no tracks... Hash can tell you the system is no longer compromised, but not what nasty stuff was done before settings and files returned to normal.

To stand up in Court... it depends of what is standing up for. If you want to prove two files are different, my answer is yes, the Court will accept it based on the hash values. Hashes are on the base of digital signatures and code signing certificates; where another major technology is present, public key cryptography; and that stand up too. But that's it. Hash values are no proof that it was Alice who hacked Bob's server, or that the keylogger found in the office PC was placed by the boss. Private forensics may not stand up in court, and are not recommended if you are taking legal actions. If you want to go the legal way, let the police do the forensics. If you just want to know fast what was done on your system, save any important data that may be there, and format so that the system returns quickly into business again, take it to a private lab... one way or another, dont do forensics on serious cases if you are no expert.

Regards,
Mario
"Bob" <bo*@nospam.com> wrote in message news:eq**************@TK2MSFTNGP12.phx.gbl...
Hi there,

I am working on an application to be used in our local Forensics
department...

Among other things the app will connect to a digital camera and download the
images to the hard drive. For various reasons I need to be able to say with
certainty that the image on the hard drive is exactly what was on the
camera... any ideas on best way to achieve this...?

I could do a checksum of each image and compare this but not sure how to
implement in code or if there is a better way. Merely checking the file size
matches is not sufficient to stand up in Court.

Any suggestions appreciated, especially if sample code included (can be
classic VB if necessary). Does .Net perhaps have something like this built
into the Framework?

Cheers

Nov 20 '05 #9

P: n/a
Bob
Hi,

I don't need to prove it came from a certain camera... just that the local
(or server side) copy is exactly the same as the source image on the camera.
Otherwise you are right, it would be a great deal more complex.

Cheers

"Dick Grier" <di**************@msn.com> wrote in message
news:uB**************@TK2MSFTNGP10.phx.gbl...
Hi,

Actually, the other answers that you received are on target, technically.
However, they do nothing to address the legal issue...

And, there is no way to do what you want -- UNLESS the camera ITSELF
generates the CRC. Your software could then compare and store (perhaps
encrypted) the results. The issue here is that there is no chain of
evidence with a conventional digital camera. You only have the image
information AFTER it is stored on the PC. And, the original image may be
purported to have come from a camera, but... How do you prove that?

A better legal remedy would be to capture the image, encrypt it with a
digital certificate, and include a copy of some sort of (other) written
document (probably a physically signed image) that certifies the actual
chain of handling. You should talk to you legal eagles to get a specific
recommendation on this additional work.

BTW, this all is IMO. I'm not a lawyer.

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.

Nov 20 '05 #10

P: n/a
Bob
Yep, that would be ideal but no such camera (of the quality required) exists
just yet... at least one is under development apparently though... not sure
who is doing it though...

That link was interesting, thanks.

"_Andy_" <wi******@nospamthanks.gov> wrote in message
news:t3********************************@4ax.com...
On Tue, 21 Oct 2003 16:35:12 +1000, "Bob" <bo*@nospam.com> wrote:
Hi there,

I am working on an application to be used in our local Forensics
department...

Among other things the app will connect to a digital camera and download theimages to the hard drive. For various reasons I need to be able to say withcertainty that the image on the hard drive is exactly what was on the
camera... any ideas on best way to achieve this...?

I could do a checksum of each image and compare this but not sure how to
implement in code or if there is a better way. Merely checking the file sizematches is not sufficient to stand up in Court.

Any suggestions appreciated, especially if sample code included (can be
classic VB if necessary). Does .Net perhaps have something like this builtinto the Framework?

Cheers


You need a digital camera that supports watermarking and/or
signatures. As hinted by the other guys in this thread, the jury's
still out on aspects of this technology.

refs: http://citeseer.nj.nec.com/context/381517/0

I've had a quick look for suitable cameras, but haven't had any luck.
I'm sure someone manufactures them, as the problem was identified
years ago... Best of luck.

Rgds,

Nov 20 '05 #11

P: n/a
Bob
Thanks Lasse,

I'll have a look but I think the hasing method might be better as suggested
by the others...
Cheers
"Lasse Eskildsen" <Le****@DELETEwebspeed.dk> wrote in message
news:Oi**************@TK2MSFTNGP12.phx.gbl...

"Bob" <bo*@nospam.com> skrev i en meddelelse
news:eq**************@TK2MSFTNGP12.phx.gbl...
Hi there,

I am working on an application to be used in our local Forensics
department...

Among other things the app will connect to a digital camera and download

the
images to the hard drive. For various reasons I need to be able to say

with
certainty that the image on the hard drive is exactly what was on the
camera... any ideas on best way to achieve this...?

I could do a checksum of each image and compare this but not sure how to
implement in code or if there is a better way. Merely checking the file

size
matches is not sufficient to stand up in Court.

Any suggestions appreciated, especially if sample code included (can be
classic VB if necessary). Does .Net perhaps have something like this built into the Framework?

Cheers


Hi Bob,
Have a look at
http://www.vbaccelerator.com/home/NE...32/article.asp ,
maybe you can use some of this.

--
Lasse

Nov 20 '05 #12

P: n/a
Hi Bob,

I look forward to your result.
If you have any related question, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
From: "Bob" <bo*@nospam.com>
References: <eq**************@TK2MSFTNGP12.phx.gbl> <FO**************@cpmsftngxa06.phx.gbl>Subject: Re: Checksum, CRC or something better?
Date: Wed, 22 Oct 2003 22:45:40 +1000
Lines: 105
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <OP**************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: 202-44-189-210.nexnet.net.au 202.44.189.210
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP09.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:149013
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Hi Peter,

Thanks for the code... I'll check it out and see how I go...

Cheers

Nov 20 '05 #13

P: n/a
Bob
Hi again Peter,

Your code worked fine and even the tiniest change in the image is picked
up... as required. Thanks for your help.

I have a question which is sort of related - at least, it's for the same app
but it relates to Windows Image Acquisition 2.0... if you have any
experience with this then I'd be interested in hearing your input.

I have posted the questions with a subject of Windows Image Acquisition 2.0
for XP.

Thanks again

"Peter Huang [MSFT]" <v-******@online.microsoft.com> wrote in message
news:5J*************@cpmsftngxa06.phx.gbl...
Hi Bob,

I look forward to your result.
If you have any related question, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
From: "Bob" <bo*@nospam.com>
References: <eq**************@TK2MSFTNGP12.phx.gbl>

<FO**************@cpmsftngxa06.phx.gbl>
Subject: Re: Checksum, CRC or something better?
Date: Wed, 22 Oct 2003 22:45:40 +1000
Lines: 105
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <OP**************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: 202-44-189-210.nexnet.net.au 202.44.189.210
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP09.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:149013
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Hi Peter,

Thanks for the code... I'll check it out and see how I go...

Cheers


Nov 20 '05 #14

P: n/a
Hi Bob,

Did the project works?
If you have any related question, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #15

P: n/a
Hi Bob,

For the new question you post in the newsgroup
microsoft.public.dotnet.languages.vb,
Jeffery Tan has followed up in the new post.
If you have any more concern on this question, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #16

P: n/a
Bob
Hi again,

I need to be able to store and display the hash value... is there any way I
can represent it as text?

Cheers

"Peter Huang" <v-******@online.microsoft.com> wrote in message
news:gP*************@cpmsftngxa06.phx.gbl...
Hi Bob,

Did the project works?
If you have any related question, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #17

P: n/a
Bob
Never mind...

"Bob" <bo*@nospam.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi again,

I need to be able to store and display the hash value... is there any way I can represent it as text?

Cheers

"Peter Huang" <v-******@online.microsoft.com> wrote in message
news:gP*************@cpmsftngxa06.phx.gbl...
Hi Bob,

Did the project works?
If you have any related question, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Nov 20 '05 #18

P: n/a
Hi Boaz,

The hash value is a byte array, some of the bytes can not be represent as
recognizable ascii or unicode characters.
So you may try to store and display the byte array.

I modify the sample as following.

Imports System
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text

Module Module1
Sub Main()
Dim fs As FileStream = New FileStream("c:\dafen.jpg",
FileMode.Open, FileAccess.Read)
Dim arr(fs.Length) As Byte
fs.Read(arr, 0, fs.Length)
fs.Close()

Dim tmpHash() As Byte
tmpHash = New MD5CryptoServiceProvider().ComputeHash(arr)
fs = New FileStream("c:\dafen.jpg.dat", FileMode.OpenOrCreate,
FileAccess.Write)
fs.Write(tmpHash, 0, tmpHash.Length)
fs.Close()

'Dim uniDecoder As Decoder = Encoding.Unicode.GetDecoder()

'Dim charCount As Integer = uniDecoder.GetCharCount(tmpHash, 0,
tmpHash.Length)
'Dim chars() As Char = New Char(charCount - 1) {}
'Dim charsDecodedCount As Integer = _
' uniDecoder.GetChars(tmpHash, 0, tmpHash.Length, chars, 0)
'Console.WriteLine()

Dim b As Byte
For Each b In tmpHash
Console.Write("{0:X} ", b)
Next b
Console.WriteLine()

fs = New FileStream("c:\dafen1.jpg", FileMode.Open, FileAccess.Read)
fs.Read(arr, 0, fs.Length)
fs.Close()

Dim tmpNewHash() As Byte
tmpNewHash = New MD5CryptoServiceProvider().ComputeHash(arr)
fs = New FileStream("c:\dafen1.jpg.dat", FileMode.OpenOrCreate,
FileAccess.Write)
fs.Write(tmpHash, 0, tmpHash.Length)
fs.Close()

For Each b In tmpNewHash
Console.Write("{0:X} ", b)
Next b
Console.WriteLine()
'charCount = uniDecoder.GetCharCount(tmpNewHash, 0,
tmpNewHash.Length)
'chars = New Char(charCount - 1) {}
'charsDecodedCount = _
' uniDecoder.GetChars(tmpNewHash, 0, tmpNewHash.Length, chars, 0)
'Console.WriteLine(chars)
Dim bEqual As Boolean
If tmpNewHash.Length = tmpHash.Length Then
Dim i As Integer
Do While (i < tmpNewHash.Length) AndAlso (tmpNewHash(i) =
tmpHash(i))
i += 1
Loop
If i = tmpNewHash.Length Then
bEqual = True
End If
End If
If bEqual Then
Console.WriteLine("The two hash values are the same")
Else
Console.WriteLine("The two hash values are not the same")
End If
End Sub
End Module

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #19

P: n/a
Hi Bob,

If you have any question on this issue please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #20

P: n/a
Bob
Hi again Peter,

I have attempted to use this hashing code in the following scenario:

Get an image, base64 encode and write to xml file.

Reverse this process to get image file back.

Compare hashes of before and after - they do not match... image looks
identical visually, and has same filesize and other properties... can you
suggest a way I can store these images to an XML file and get the exact same
file back when I extract the image again...?

The XML code is something like this:

Private Sub WriteXMLImage(ByVal strSourceFilePathAndName As String,
Optional ByVal strTargetFilePathAndName As String = "")

If strSourceFilePathAndName <> "" Then

Dim strSourceExtension As String = ""
strSourceExtension = strSourceFilePathAndName.Substring
(strSourceFilePathAndName.Length - 3)

'Destination filename not supplied so make one up
If strTargetFilePathAndName = "" Then
strTargetFilePathAndName =
strSourceFilePathAndName.Substring(0, strSourceFilePathAndName.Length
- 3)
& "xml"
End If

Dim xmlTextWriter As New XmlTextWriter
(strTargetFilePathAndName, Encoding.UTF8)
xmlTextWriter.Formatting = Formatting.Indented
xmlTextWriter.WriteStartDocument()

' Copy the picture into a MemoryStream.
Dim memory_stream As New MemoryStream
Dim objImageOriginal As Image

objImageOriginal = New Bitmap(strSourceFilePathAndName)
'objImageOriginal = objImageOriginal.FromFile
(strSourceFilePathAndName)
objImageOriginal.Save(memory_stream, GetFormat
(strSourceExtension))

' Copy the MemoryStream data into a Byte array.
Dim objBytes(memory_stream.Length - 1) As Byte
objBytes = memory_stream.ToArray()

' Make a Picture element.
xmlTextWriter.WriteStartElement("Picture")
xmlTextWriter.WriteAttributeString("Encoding",
Nothing,
"Base64")
' Save Image details and encoding as Attributes

xmlTextWriter.WriteAttributeString("Bytes",
objBytes.Length)
xmlTextWriter.WriteAttributeString("Height",
objImageOriginal.Height)
xmlTextWriter.WriteAttributeString("Width",
objImageOriginal.Width)
xmlTextWriter.WriteAttributeString("Type",
strSourceExtension)
xmlTextWriter.WriteBase64(objBytes, 0, objBytes.Length)
xmlTextWriter.WriteEndElement()
xmlTextWriter.WriteEndDocument()

xmlTextWriter.Close()
objImageOriginal.Dispose()
MsgBox("XML file created successfully ! ",
MsgBoxStyle.Information, "Success !")

Else
MsgBox("A target file name must be supplied.",
MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Error")
Exit Sub
End If

End Sub

Private Sub ReadXMLImage(ByVal strSourceFilePathAndName As String)

If strSourceFilePathAndName <> "" Then

Dim objImageOriginal As Image
Dim blnValidXML As Boolean = False
Dim strImageExtension As String = ""
Dim strTargetFilePathAndName As String = ""

' Load the picture from XML
Dim xmlTextReader As New XmlTextReader
(strSourceFilePathAndName)

' Skip until we find the Picture node.
Do While xmlTextReader.Read()
' See if this is the Picture node.
If xmlTextReader.Name = "Picture" Then
blnValidXML = True
' Allocate room for the byte data and get all
attribute
values.
Dim intNumBytes As Integer =
xmlTextReader.GetAttribute
("Bytes")
Dim h As Integer =
xmlTextReader.GetAttribute("Height")
Dim w As Integer =
xmlTextReader.GetAttribute("Width")

strImageExtension =
xmlTextReader.GetAttribute("Type")

'Refresh the target file according to parameters
read
from XML file Type Attribute
strTargetFilePathAndName = Replace
(strSourceFilePathAndName, ".xml", "New." &
strImageExtension)
'lbl_Message.Text = "Source File : " &
strSourceFilePathAndName & vbCrLf & "Target File : " &
strTargetFilePathAndName

Dim bytes(intNumBytes - 1) As Byte

' Translate the encoded data back into byte data.
Select Case xmlTextReader.GetAttribute
("Encoding").ToLower
Case "base64"
xmlTextReader.ReadBase64(bytes, 0,
intNumBytes)
Case "binhex"
xmlTextReader.ReadBinHex(bytes, 0,
intNumBytes)
Case Else
MsgBox("Unknown image encoding
'" &
xmlTextReader.GetAttribute("Encoding") & "'", _
MsgBoxStyle.Exclamation, "Unknown
Encoding")
Exit Sub
End Select

' Allocate a MemoryStream and a BinaryWriter
attached
to it.
Dim memory_stream As New MemoryStream
Dim binary_writer As BinaryWriter = New
BinaryWriter
(memory_stream)

' Copy the bytes into the BinaryWriter and
MemoryStream.
binary_writer.Write(bytes, 0, intNumBytes)
binary_writer.Flush()

' Load the picture from the memory stream and
save it
and show in the Picture box.
memory_stream.Position = 0
objImageOriginal = Image.FromStream(memory_stream)

'objImageOriginal.Save(strTargetFilePathAndName,
GetFormat(strImageExtension))
objImageOriginal.Save(strTargetFilePathAndName)
'Me.PB_sample.Image =
Image.FromStream(memory_stream)
'PB_sample.SizeMode =
PictureBoxSizeMode.CenterImage
binary_writer.Close()
Exit Do
End If
Loop

xmlTextReader.Close()
If blnValidXML Then
MsgBox(strImageExtension.ToUpper & " file created
successfully ! ", MsgBoxStyle.Information, "Success !")
Else
MsgBox("Invalid XML file format ! ",
MsgBoxStyle.Critical,
"Error !")
End If

Else
MsgBox("An xml file containing the image must be
selected.",
MsgBoxStyle.OKOnly + MsgBoxStyle.Information, "File error")
Exit Sub
End If

End Sub

Private Function GetFormat(ByVal ImageType As String) As
System.Drawing.Imaging.ImageFormat
Select Case ImageType.ToUpper
Case "JPG"
Return ImageFormat.Jpeg
Case "GIF"
Return ImageFormat.Gif
Case "PNG"
Return ImageFormat.Png
Case "BMP"
Return ImageFormat.Bmp
End Select
End Function

Also, I want (need) to do this for image formats that aren't recognised by
the image object (Wavelet Scalar Quantization .wsq format)... any idea how I
can achieve this?

Cheers again for you help...
"Peter Huang" <v-******@online.microsoft.com> wrote in message
news:7y**************@cpmsftngxa06.phx.gbl...
Hi Bob,

If you have any question on this issue please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #21

P: n/a
Hi Bob,

Here is my code snipper.
Dim writer As New XmlTextWriter("c:\test.xml",
System.Text.Encoding.UTF8)
writer.WriteStartElement("ss")
writer.WriteBase64(arr, 0, len)
writer.WriteEndElement()
writer.Close()
Dim readoutbuffer(len) As Byte
Dim reader As New XmlTextReader("c:\test.xml")
reader.Read()
Dim rt As Integer = reader.ReadBase64(readoutbuffer, 0, len)
reader.Close()

I think the WriteBase64 of XmlTextWriter and the ReadBase64 of
XmlTextReader is what you want.
For more information, you may refer to the two methods in MSDN.
Please have a try and let me know the result.
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #22

P: n/a
Bob
Peter, you're too much...

Thanks again for your help... I was looking at this myself but hadn't got it
to work completely (was trying with filestream object)...

Cheers
"Peter Huang" <v-******@online.microsoft.com> wrote in message
news:wW**************@cpmsftngxa06.phx.gbl...
Hi Bob,

Here is my code snipper.
Dim writer As New XmlTextWriter("c:\test.xml",
System.Text.Encoding.UTF8)
writer.WriteStartElement("ss")
writer.WriteBase64(arr, 0, len)
writer.WriteEndElement()
writer.Close()
Dim readoutbuffer(len) As Byte
Dim reader As New XmlTextReader("c:\test.xml")
reader.Read()
Dim rt As Integer = reader.ReadBase64(readoutbuffer, 0, len)
reader.Close()

I think the WriteBase64 of XmlTextWriter and the ReadBase64 of
XmlTextReader is what you want.
For more information, you may refer to the two methods in MSDN.
Please have a try and let me know the result.
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #23

P: n/a
Hi Bob,

Actually your code works well on my machines.
e.g.
the WriteXMLImage will write dafen.jpg into the dafen.xml
the ReadXMLImage will read dafen.xml and create a new file dafenNew.jpg

I then computer the two files dafen.jpg and dafenNew.jpg 's hash code ,
they are match.

I post my code snipper in the previous post, I hope you can creatd a simple
test project as what I did.
To isolate the problem, my idea is that write the jpg file into the xml
file and then read it back from the xml file.
Then you will know if there is any wrong with XMLwrite and XMLread steps.

I look forward to hearing from you.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #24

P: n/a
Bob
Well, good to know you can get it to work... not sure why I had problems...
it did store the image to XML and recover it fine... the image opened and
looked identical... it was only the hash value that differed.

Still, your code is absolute simplicity compared to what I was using... it
works a treat...

If I can only get around the CD Writing issue I'll pretty much have a fully
functional prototype...

Cheers Peter...

"Peter Huang" <v-******@online.microsoft.com> wrote in message
news:gI*************@cpmsftngxa06.phx.gbl...
Hi Bob,

Actually your code works well on my machines.
e.g.
the WriteXMLImage will write dafen.jpg into the dafen.xml
the ReadXMLImage will read dafen.xml and create a new file dafenNew.jpg

I then computer the two files dafen.jpg and dafenNew.jpg 's hash code ,
they are match.

I post my code snipper in the previous post, I hope you can creatd a simple test project as what I did.
To isolate the problem, my idea is that write the jpg file into the xml
file and then read it back from the xml file.
Then you will know if there is any wrong with XMLwrite and XMLread steps.

I look forward to hearing from you.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #25

This discussion thread is closed

Replies have been disabled for this discussion.