MD5

  • Thread starter Thread starter PawelR
  • Start date Start date
P

PawelR

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.CreateFromName
("MD5")).ComputeHash(data1ToHash);
string s2=BitConverter.ToString(hashvalue1);
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
 
Hello,
I've problem with this function

private string getMD5(string s1)

My question: How write this procedure in C++.Net?

Hope it helps somehow:

<code_sample>

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;
using namespace System::Text;
using namespace System::Security::Cryptography;

String* getMD5(String* s1)
{
Byte data1ToHash[] = (new UnicodeEncoding())->GetBytes(s1);
Byte hashvalue1[] =
static_cast<HashAlgorithm*>(CryptoConfig::CreateFromName(S"MD5"))->ComputeHa
sh(data1ToHash);
String *s2 = BitConverter::ToString(hashvalue1);
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::WriteLine(getMD5("Hello World"));
return 0;
}

</code_sample>

...
Cheers,
Vadim.
 
PawelR said:
private string getMD5(string s1)
{
string s1 = "UserName"
byte[] data1ToHash = (new UnicodeEncoding()).GetBytes(s1);
byte[] hashvalue1 = ((HashAlgorithm) CryptoConfig.CreateFromName
("MD5")).ComputeHash(data1ToHash);
string s2=BitConverter.ToString(hashvalue1);
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.

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

C#: getMD5("m") = 38ED5C320F74CE4FB02C506C65C06609;
PHP: md5("m") = 6f8f57715090da2632453988d9a1501b;

JavaScript implementation of MD5 I've found on Internet gives the same as
PHP
 
Pawel said:
PawelR said:
private string getMD5(string s1)
{
string s1 = "UserName"
byte[] data1ToHash = (new UnicodeEncoding()).GetBytes(s1);
byte[] hashvalue1 = ((HashAlgorithm) CryptoConfig.CreateFromName
("MD5")).ComputeHash(data1ToHash);
string s2=BitConverter.ToString(hashvalue1);
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.

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

C#: getMD5("m") = 38ED5C320F74CE4FB02C506C65C06609;
PHP: md5("m") = 6f8f57715090da2632453988d9a1501b;
ascii\UTF8 m = 6f8f57715090da2632453988d9a1501b;
unicode m = 38ED5C32AF74CE4FBA2C5A6C65C16619;

However, I am curious as to why you are modifying the result of the MD5 hash
string, is there a reason for it?
 
Daniel said:
ascii\UTF8 m = 6f8f57715090da2632453988d9a1501b;
unicode m = 38ED5C32AF74CE4FBA2C5A6C65C16619;

However, I am curious as to why you are modifying the result of the
MD5 hash string, is there a reason for it?
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.
 
Pawel said:
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.
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(or UTF8Encoding) you would
get the same md5 result as javascript & php returns.
 
Back
Top