MD5

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PawelR

    #1

    MD5

    Hello Group

    I've problem with this function

    private string getMD5(string s1)
    {
    string s1 = "UserName"
    byte[] data1ToHash = (new UnicodeEncoding ()).GetBytes(s1 );
    byte[] hashvalue1 = ((HashAlgorithm ) CryptoConfig.Cr eateFromName
    ("MD5")).Comput eHash(data1ToHa sh);
    string s2=BitConverter .ToString(hashv alue1);
    for (int i=s2.Length;i>3 ;i-=3) s2=s2.Remove(i-3,1);
    s2=s2.Replace(" A","1");
    s2=s2.Replace(" 0","A");
    s2=s2.Replace(" 1","0");
    return s2;
    }

    This generate hash string - code to application.
    My question: How write this procedure in C++.Net?

    Thx
    PawelR
  • Vadim Melnik

    #2
    Re: MD5

    Hello,
    [color=blue]
    > I've problem with this function
    >
    > private string getMD5(string s1)
    >
    > My question: How write this procedure in C++.Net?
    >[/color]

    Hope it helps somehow:

    <code_sample>

    #include "stdafx.h"

    #using <mscorlib.dll >

    using namespace System;
    using namespace System::Text;
    using namespace System::Securit y::Cryptography ;

    String* getMD5(String* s1)
    {
    Byte data1ToHash[] = (new UnicodeEncoding ())->GetBytes(s1) ;
    Byte hashvalue1[] =
    static_cast<Has hAlgorithm*>(Cr yptoConfig::Cre ateFromName(S"M D5"))->ComputeHa
    sh(data1ToHash) ;
    String *s2 = BitConverter::T oString(hashval ue1);
    for (int i=s2->Length;i>3;i-=3) s2=s2->Remove(i-3,1);
    s2=s2->Replace('A','1 ');
    s2=s2->Replace('0','A ');
    s2=s2->Replace('1','0 ');
    return s2;
    }

    int _tmain()
    {
    Console::WriteL ine(getMD5("Hel lo World"));
    return 0;
    }

    </code_sample>

    ...
    Cheers,
    Vadim.


    Comment

    • Vadim Melnik

      #3
      Re: MD5

      Hello,
      [color=blue]
      > I've problem with this function
      >
      > private string getMD5(string s1)
      >
      > My question: How write this procedure in C++.Net?
      >[/color]

      Hope it helps somehow:

      <code_sample>

      #include "stdafx.h"

      #using <mscorlib.dll >

      using namespace System;
      using namespace System::Text;
      using namespace System::Securit y::Cryptography ;

      String* getMD5(String* s1)
      {
      Byte data1ToHash[] = (new UnicodeEncoding ())->GetBytes(s1) ;
      Byte hashvalue1[] =
      static_cast<Has hAlgorithm*>(Cr yptoConfig::Cre ateFromName(S"M D5"))->ComputeHa
      sh(data1ToHash) ;
      String *s2 = BitConverter::T oString(hashval ue1);
      for (int i=s2->Length;i>3;i-=3) s2=s2->Remove(i-3,1);
      s2=s2->Replace('A','1 ');
      s2=s2->Replace('0','A ');
      s2=s2->Replace('1','0 ');
      return s2;
      }

      int _tmain()
      {
      Console::WriteL ine(getMD5("Hel lo World"));
      return 0;
      }

      </code_sample>

      ...
      Cheers,
      Vadim.


      Comment

      • Pawel

        #4
        Re: MD5

        PawelR wrote:[color=blue]
        > private string getMD5(string s1)
        > {
        > string s1 = "UserName"
        > byte[] data1ToHash = (new UnicodeEncoding ()).GetBytes(s1 );
        > byte[] hashvalue1 = ((HashAlgorithm ) CryptoConfig.Cr eateFromName
        > ("MD5")).Comput eHash(data1ToHa sh);
        > string s2=BitConverter .ToString(hashv alue1);
        > for (int i=s2.Length;i>3 ;i-=3) s2=s2.Remove(i-3,1);
        > s2=s2.Replace(" A","1");
        > s2=s2.Replace(" 0","A");
        > s2=s2.Replace(" 1","0");
        > return s2;
        > }
        >
        > This generate hash string - code to application.[/color]

        Why does result of this method differ from the one from PHP md5() function?

        C#: getMD5("m") = 38ED5C320F74CE4 FB02C506C65C066 09;
        PHP: md5("m") = 6f8f57715090da2 632453988d9a150 1b;

        JavaScript implementation of MD5 I've found on Internet gives the same as
        PHP


        --
        _______________ __________

        Pawel


        Comment

        • Daniel O'Connell

          #5
          Re: MD5


          "Pawel" <pmalita@wytnij prz-rzeszow.pl> wrote in message
          news:bkutmf$i2l $1@atlantis.new s.tpi.pl...[color=blue]
          > PawelR wrote:[color=green]
          > > private string getMD5(string s1)
          > > {
          > > string s1 = "UserName"
          > > byte[] data1ToHash = (new UnicodeEncoding ()).GetBytes(s1 );
          > > byte[] hashvalue1 = ((HashAlgorithm ) CryptoConfig.Cr eateFromName
          > > ("MD5")).Comput eHash(data1ToHa sh);
          > > string s2=BitConverter .ToString(hashv alue1);
          > > for (int i=s2.Length;i>3 ;i-=3) s2=s2.Remove(i-3,1);
          > > s2=s2.Replace(" A","1");
          > > s2=s2.Replace(" 0","A");
          > > s2=s2.Replace(" 1","0");
          > > return s2;
          > > }
          > >
          > > This generate hash string - code to application.[/color]
          >
          > Why does result of this method differ from the one from PHP md5()[/color]
          function?[color=blue]
          >
          > C#: getMD5("m") = 38ED5C320F74CE4 FB02C506C65C066 09;
          > PHP: md5("m") = 6f8f57715090da2 632453988d9a150 1b;
          >[/color]
          ascii\UTF8 m = 6f8f57715090da2 632453988d9a150 1b;
          unicode m = 38ED5C32AF74CE4 FBA2C5A6C65C166 19;

          However, I am curious as to why you are modifying the result of the MD5 hash
          string, is there a reason for it?
          [color=blue]
          > JavaScript implementation of MD5 I've found on Internet gives the same as
          > PHP
          >
          >
          > --
          > _______________ __________
          >
          > Pawel
          >
          >[/color]


          Comment

          • Pawel

            #6
            Re: MD5

            Daniel O'Connell wrote:[color=blue]
            > ascii\UTF8 m = 6f8f57715090da2 632453988d9a150 1b;
            > unicode m = 38ED5C32AF74CE4 FBA2C5A6C65C166 19;
            >
            > However, I am curious as to why you are modifying the result of the
            > MD5 hash string, is there a reason for it?[/color]
            PawelR is modyfying hash string, problably besause he wants to apply this
            method to some kind of authorising. I've used it only to show differences
            between both hashing functions.

            --
            _______________ __________

            Pawe³


            Comment

            • Daniel O'Connell

              #7
              Re: MD5


              "Pawel" <pmalita@wytnij prz-rzeszow.pl> wrote in message
              news:bl15ng$e6c $1@atlantis.new s.tpi.pl...[color=blue]
              > Daniel O'Connell wrote:[color=green]
              > > ascii\UTF8 m = 6f8f57715090da2 632453988d9a150 1b;
              > > unicode m = 38ED5C32AF74CE4 FBA2C5A6C65C166 19;
              > >
              > > However, I am curious as to why you are modifying the result of the
              > > MD5 hash string, is there a reason for it?[/color]
              > PawelR is modyfying hash string, problably besause he wants to apply this
              > method to some kind of authorising. I've used it only to show differences
              > between both hashing functions.
              >[/color]
              The result difference occured in this case because his code is using UNICODE
              character set while PHP & JavaScript apparently uses ascii by default. If
              you changed the UnicodeEncoding to AsciiEncoding(o r UTF8Encoding) you would
              get the same md5 result as javascript & php returns.[color=blue]
              > --
              > _______________ __________
              >
              > Pawe³
              >
              >[/color]


              Comment

              Working...