Professional Web Applications Themes

MD5 implementation - ASP.NET Security

I need to use MD5 to generate a hash of a string. It needs to be compatible with the MD5 implementation in PHP. public string ComputeMD5(string plain) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] bMD5Hash = Encoding.ASCII.GetBytes(plain); return Encoding.ASCII.GetString(md5.ComputeHash(bMD5Hash) ); //return Convert.ToBase64String(md5.ComputeHash(bMD5Hash)); } With PHP I get: md5('test1test1test1test1test1test1') = '0b2fc97f2a37500552d805c4727295a8' In .NET I get: ComputeMD5('test1test1test1test1test1test1') = ' /I*7PRXDrr(' ComputeMD5('test1test1test1test1test1test1') = 'Cy/Jfyo3UAVS2AXEcnKVqA==' (base64) Am I doing it wrong or are the PHP and .NET implementations not compatible? Thanks in advance, Casper Hornstrup...

  1. #1

    Default MD5 implementation

    I need to use MD5 to generate a hash of a string. It needs to be
    compatible with the MD5 implementation in PHP.

    public string ComputeMD5(string plain)
    {
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    byte[] bMD5Hash = Encoding.ASCII.GetBytes(plain);
    return Encoding.ASCII.GetString(md5.ComputeHash(bMD5Hash) );
    //return Convert.ToBase64String(md5.ComputeHash(bMD5Hash));
    }


    With PHP I get:
    md5('test1test1test1test1test1test1') = '0b2fc97f2a37500552d805c4727295a8'

    In .NET I get:
    ComputeMD5('test1test1test1test1test1test1') = ' /I*7PRXDrr('
    ComputeMD5('test1test1test1test1test1test1') = 'Cy/Jfyo3UAVS2AXEcnKVqA=='
    (base64)

    Am I doing it wrong or are the PHP and .NET implementations not compatible?

    Thanks in advance,
    Casper Hornstrup



    Casper Hornstrup Guest

  2. #2

    Default Re: MD5 implementation

    Hi,

    If you compare both hashes in the same encoding (the PHP version is in
    Hexadecimal and the .NET version is in base64 or binary.), they will match.

    --
    Hernan de Lahitte
    Lagash Systems S.A.
    [url]http://www.lagash.com[/url]



    "Casper Hornstrup" <msdncsite.com> wrote in message
    news:OZ$uuYw6DHA.1804TK2MSFTNGP12.phx.gbl...
    > I need to use MD5 to generate a hash of a string. It needs to be
    > compatible with the MD5 implementation in PHP.
    >
    > public string ComputeMD5(string plain)
    > {
    > MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    > byte[] bMD5Hash = Encoding.ASCII.GetBytes(plain);
    > return Encoding.ASCII.GetString(md5.ComputeHash(bMD5Hash) );
    > //return Convert.ToBase64String(md5.ComputeHash(bMD5Hash));
    > }
    >
    >
    > With PHP I get:
    > md5('test1test1test1test1test1test1') = '0b2fc97f2a37500552d805c4727295a8'
    >
    > In .NET I get:
    > ComputeMD5('test1test1test1test1test1test1') = ' /I*7PRXDrr('
    > ComputeMD5('test1test1test1test1test1test1') = 'Cy/Jfyo3UAVS2AXEcnKVqA=='
    > (base64)
    >
    > Am I doing it wrong or are the PHP and .NET implementations not
    compatible?
    >
    > Thanks in advance,
    > Casper Hornstrup
    >
    >
    >

    Hernan de Lahitte Guest

  3. #3

    Default Re: MD5 implementation

    Thanks for you answer.
    How do I get a string in the .NET version that I can compare to the PHP
    generated string and have a match?

    Casper

    "Hernan de Lahitte" <hernanlagash.com> wrote in message
    news:uLq9jnx6DHA.2404TK2MSFTNGP12.phx.gbl...
    > Hi,
    >
    > If you compare both hashes in the same encoding (the PHP version is in
    > Hexadecimal and the .NET version is in base64 or binary.), they will
    match.
    >
    > --
    > Hernan de Lahitte
    > Lagash Systems S.A.
    > [url]http://www.lagash.com[/url]
    >
    >
    >
    > "Casper Hornstrup" <msdncsite.com> wrote in message
    > news:OZ$uuYw6DHA.1804TK2MSFTNGP12.phx.gbl...
    > > I need to use MD5 to generate a hash of a string. It needs to be
    > > compatible with the MD5 implementation in PHP.
    > >
    > > public string ComputeMD5(string plain)
    > > {
    > > MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    > > byte[] bMD5Hash = Encoding.ASCII.GetBytes(plain);
    > > return Encoding.ASCII.GetString(md5.ComputeHash(bMD5Hash) );
    > > //return Convert.ToBase64String(md5.ComputeHash(bMD5Hash));
    > > }
    > >
    > >
    > > With PHP I get:
    > > md5('test1test1test1test1test1test1') =
    '0b2fc97f2a37500552d805c4727295a8'
    > >
    > > In .NET I get:
    > > ComputeMD5('test1test1test1test1test1test1') = ' /I*7PRXDrr('
    > > ComputeMD5('test1test1test1test1test1test1') =
    'Cy/Jfyo3UAVS2AXEcnKVqA=='
    > > (base64)
    > >
    > > Am I doing it wrong or are the PHP and .NET implementations not
    > compatible?
    > >
    > > Thanks in advance,
    > > Casper Hornstrup
    > >
    > >
    > >
    >
    >

    Casper Hornstrup Guest

  4. #4

    Default Re: MD5 implementation

    This code will encode in Hexa your hashed byte array.

    public static string EncodeHexString(byte[] sArray)
    {
    StringBuilder sb = new StringBuilder( sArray.Length * 2 );
    for(int index = 0; index < sArray.Length; index++)
    {
    sb.AppendFormat( "{0:X2}", sArray[index] );
    }
    return sb.ToString();
    }

    --
    Hernan de Lahitte
    Lagash Systems S.A.
    [url]http://www.lagash.com[/url]



    "Casper Hornstrup" <msdncsite.com> wrote in message
    news:#Id1qyx6DHA.2044TK2MSFTNGP10.phx.gbl...
    > Thanks for you answer.
    > How do I get a string in the .NET version that I can compare to the PHP
    > generated string and have a match?
    >
    > Casper
    >
    > "Hernan de Lahitte" <hernanlagash.com> wrote in message
    > news:uLq9jnx6DHA.2404TK2MSFTNGP12.phx.gbl...
    > > Hi,
    > >
    > > If you compare both hashes in the same encoding (the PHP version is in
    > > Hexadecimal and the .NET version is in base64 or binary.), they will
    > match.
    > >
    > > --
    > > Hernan de Lahitte
    > > Lagash Systems S.A.
    > > [url]http://www.lagash.com[/url]
    > >
    > >
    > >
    > > "Casper Hornstrup" <msdncsite.com> wrote in message
    > > news:OZ$uuYw6DHA.1804TK2MSFTNGP12.phx.gbl...
    > > > I need to use MD5 to generate a hash of a string. It needs to be
    > > > compatible with the MD5 implementation in PHP.
    > > >
    > > > public string ComputeMD5(string plain)
    > > > {
    > > > MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    > > > byte[] bMD5Hash = Encoding.ASCII.GetBytes(plain);
    > > > return Encoding.ASCII.GetString(md5.ComputeHash(bMD5Hash) );
    > > > //return Convert.ToBase64String(md5.ComputeHash(bMD5Hash));
    > > > }
    > > >
    > > >
    > > > With PHP I get:
    > > > md5('test1test1test1test1test1test1') =
    > '0b2fc97f2a37500552d805c4727295a8'
    > > >
    > > > In .NET I get:
    > > > ComputeMD5('test1test1test1test1test1test1') = ' /I*7PRXDrr('
    > > > ComputeMD5('test1test1test1test1test1test1') =
    > 'Cy/Jfyo3UAVS2AXEcnKVqA=='
    > > > (base64)
    > > >
    > > > Am I doing it wrong or are the PHP and .NET implementations not
    > > compatible?
    > > >
    > > > Thanks in advance,
    > > > Casper Hornstrup
    > > >
    > > >
    > > >
    > >
    > >
    >
    >

    Hernan de Lahitte Guest

Similar Threads

  1. web cam implementation
    By wschwisow in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 4
    Last Post: March 25th, 12:37 PM
  2. RSA implementation for PHP
    By Daniel Albisser in forum PHP Development
    Replies: 1
    Last Post: October 23rd, 12:01 PM
  3. RSA implementation of PHP
    By Daniel Albisser in forum PHP Development
    Replies: 0
    Last Post: October 23rd, 10:06 AM
  4. PB & Implementation Files & C++
    By Will Oram in forum Mac Programming
    Replies: 2
    Last Post: October 6th, 10:19 PM
  5. NAT implementation on AIX 5.x
    By Bala in forum AIX
    Replies: 0
    Last Post: October 2nd, 02:44 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139