[thelist] MD5 Encryption Problem (C#)

Anthony Baratta anthony at baratta.com
Mon Jan 15 22:42:35 CST 2007


You can't convert the Bytes to String that way. You end up dropping 
characters. You need to to do it sort of like this:

   SHA1 shaHash = new SHA1CryptoServiceProvider();
   ASCIIEncoding ByteConverter = new ASCIIEncoding();
   byte[] bCookieList = ByteConverter.GetBytes(cookieList.ToString());
   byte[] bHashResult = shaHash.ComputeHash(bCookieList);

   return BitConverter.ToString(bHashResult);

This uses SHA1, but your should find your MD5 process similar.


bart at aspnetapps.com wrote:
> 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
> different
> 
> 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