Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. MD5 - different results

MD5 - different results

Scheduled Pinned Locked Moved C / C++ / MFC
helpcryptographyphpjsontutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gabbana
    wrote on last edited by
    #1

    Hy, I have my next problem. I am trying to get an md5 hash from an string (i am using "hello" in this example) The problem is, that the CryptoAPI gives me a different key than the md5 function from PHP. I think MD5 is MD5 so both should give me the same result. Here is my Code for the crypto api -> error() only shows Error Nr.: id as a small help for me. The ByteToStr function is the same ByteToStr function which is in the MSDN.

    HCRYPTPROV	hProv	= NULL;
    HCRYPTHASH	hHash	= NULL;
    BYTE	\*pbKey	=  (BYTE\*)"hello";
    DWORD	dwKeyLen	= strlen((char\*)pbKey)+1;
    DWORD	dwHashLen	= 0;
    BYTE	pbOut\[16\];
    
    if(!CryptAcquireContext(&hProv,NULL,NULL,PROV\_RSA\_FULL,0))	Error(1);
    if(!CryptCreateHash(hProv,CALG\_MD5,0,0,&hHash)) Error(2);
    if(!CryptHashData(hHash,pbKey,dwKeyLen,0)) Error(3);
    if(!CryptGetHashParam(hHash,HP\_HASHVAL,NULL,&dwHashLen,0)) Error(4);
    if(!CryptGetHashParam(hHash,HP\_HASHVAL,pbOut,&dwHashLen,0)) Error(5);
    
    
    LPCSTR lpMsg = new char\[100\];
    
    ByteToStr(dwHashLen,&pbOut,(LPSTR)lpMsg);
    
    MessageBoxA(NULL,lpMsg,"hihi",0);
    
    delete lpMsg;
    CryptDestroyHash(hHash);
    CryptReleaseContext(hProv,0);
    

    Thanks for help. bye, gabbana

    S 1 Reply Last reply
    0
    • G gabbana

      Hy, I have my next problem. I am trying to get an md5 hash from an string (i am using "hello" in this example) The problem is, that the CryptoAPI gives me a different key than the md5 function from PHP. I think MD5 is MD5 so both should give me the same result. Here is my Code for the crypto api -> error() only shows Error Nr.: id as a small help for me. The ByteToStr function is the same ByteToStr function which is in the MSDN.

      HCRYPTPROV	hProv	= NULL;
      HCRYPTHASH	hHash	= NULL;
      BYTE	\*pbKey	=  (BYTE\*)"hello";
      DWORD	dwKeyLen	= strlen((char\*)pbKey)+1;
      DWORD	dwHashLen	= 0;
      BYTE	pbOut\[16\];
      
      if(!CryptAcquireContext(&hProv,NULL,NULL,PROV\_RSA\_FULL,0))	Error(1);
      if(!CryptCreateHash(hProv,CALG\_MD5,0,0,&hHash)) Error(2);
      if(!CryptHashData(hHash,pbKey,dwKeyLen,0)) Error(3);
      if(!CryptGetHashParam(hHash,HP\_HASHVAL,NULL,&dwHashLen,0)) Error(4);
      if(!CryptGetHashParam(hHash,HP\_HASHVAL,pbOut,&dwHashLen,0)) Error(5);
      
      
      LPCSTR lpMsg = new char\[100\];
      
      ByteToStr(dwHashLen,&pbOut,(LPSTR)lpMsg);
      
      MessageBoxA(NULL,lpMsg,"hihi",0);
      
      delete lpMsg;
      CryptDestroyHash(hHash);
      CryptReleaseContext(hProv,0);
      

      Thanks for help. bye, gabbana

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      gabbana wrote:

      DWORD dwKeyLen = strlen((char*)pbKey)+1;

      Ummmm - is the PHP MD5 algorithm including a NULL terminator byte? Do PHP strings even have NULL terminators? I doubt it very much... If removing the NULL terminator doesn't fix it, one other thing to consider is whether or not PHP strings are ASCII or some form of multi-byte Unicode?

      G 1 Reply Last reply
      0
      • S Stuart Dootson

        gabbana wrote:

        DWORD dwKeyLen = strlen((char*)pbKey)+1;

        Ummmm - is the PHP MD5 algorithm including a NULL terminator byte? Do PHP strings even have NULL terminators? I doubt it very much... If removing the NULL terminator doesn't fix it, one other thing to consider is whether or not PHP strings are ASCII or some form of multi-byte Unicode?

        G Offline
        G Offline
        gabbana
        wrote on last edited by
        #3

        wow, Thank you. It was the null terminator.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups