[thelist] MD5 Encryption Problem (C#)

bart at aspnetapps.com bart at aspnetapps.com
Mon Jan 15 21:27:00 CST 2007

I'm trying to generate an MD5 hash string but I'm having problems getting
it to always have a length of 32 as it should.  I think the problem is
that I need to pad with zeroes if needed.   So here is what I used as the
original code I started with:

    foreach (byte b in barr)
        s = s + b.ToString("x");

Generates : 517feaf1bd7342810752f153d78454e //  only 31 characters

...and this is the modified code I use to attempt to pad with zeroes:

    foreach (byte b in barr)
        s = s + b.ToString("x").PadLeft(2,'0');// pad with zero if needed

Generates: c4aa8609a86e8b5f825d127c706c9b5b // 32 characters but very

However, as you can see the second string is 32 characters but is
completely different from the original.  What I was expecting was a single
'0' would be added somewhere to the original string to get it up to 32
characters.  I assume this is some problem related to mixing bytes and
strings in the same loop.  Anyone have an idea how I can do what I am
trying to do?

More information about the thelist mailing list