MD5

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
 
V

Vadim Melnik

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.
 
P

Pawel

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
 
D

Daniel O'Connell

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?
 
P

Pawel

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.
 
D

Daniel O'Connell

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

From C# to VC.net 2
MD5 hash in C# vs. others 2
Hash MD5, Sha1 and Length 40
Gaps in ordered series 1
BitArray bug or I am confused? 2
word to excel 1
a simple http server plz help 2
a simple http server 1

Top