MD5 or SHA1 Help

Hello all,

please, I tried in everyway to make a good MD5 criptography or SHA1… I managed to do that (I’m using C#) in some ways… it works in Unity.

But when I compare with ActionScript, or PHP, or in MySQL… it returns me another data.

PHP, ActionScript and MySQL returns me the same MD5 code (or SHA1, same too). But in C#, I tried 5 diferents codes for MD5 or SHA1 and it keeps returning me a wrong code (not really wrong, just diferent from the PHP, ActionScript or MySQL)…

Anyone can help me? I even do not put the “salt” on it because just MD5 is not working… :frowning:

Someone can give me a simple MD5 that returns me the same code as MySQL?

remember there is a difference between small and big letters. Try forcing letters to be lower caps in C# (.ToLower()) before using SHA/MD5

That won’t have any effect on anything with regards to encryption. You do realize that every time that something gets encrypted like the small letter “a” or the big letter “A” it will never have the same encrypted formula applied to it right? It is based on the public/private key pairing.

Decrypt?

MD5 and SHA1 are one-way cryptographic hash functions. You use them to hash data - there’s no way to de-hash it.

MD5 functions in C#, PHP, MySQL etc will all return the same hash for the same input. If you get different hashes, then there has to be some difference in the inputs you pass to the functions.

Giving some examples that show how exactly you use the md5 functions in those languages will make helping you easier if you’re problem is not already answered.

sry Zum, but that makes absolutly no sense in respect to hashing.

hashing is not encryption…

Ok, heres is 2 examples that I used in Unity:

public static string CalculateMD5Hash(string input) {
		// Primeiro passo, calcular o MD5 hash a partir da string
		MD5 md5 = System.Security.Cryptography.MD5.Create();
		byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
		byte[] hash = md5.ComputeHash(inputBytes);
		// Segundo passo, converter o array de bytes em uma string haxadecimal
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < hash.Length; i++) {
		sb.Append(hash[i].ToString("X2"));
		}
		return sb.ToString().ToLower();
	}
	
	public static string Md5Sum(string strToEncrypt) {
		System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
		byte[] bytes = ue.GetBytes(strToEncrypt);
		// encrypt bytes
		System.Security.Cryptography.SHA1CryptoServiceProvider md5 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
		byte[] hashBytes = md5.ComputeHash(bytes);
		// Convert the encrypted bytes back to a string (base 16)
		string hashString = "";
		for (int i = 0; i < hashBytes.Length; i++) {
			hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
		}
		return hashString.PadLeft(32, '0');
	}

I cant run it now, but I can sure you it returns me a diferent data that the built-in functions of PHP and ActionScript returns me (even MySQL).
I have tried another ones, but I deleted and dont remember
:stuck_out_tongue:

EDIT: I did not decript this, I only compare the results of other languages (PHP, AS and MySQL)… sorry the wrong words

I already tried that… :smile:…same problem

Before using MD5 (that example did it after)

public static string GetMD5HashString(string unhashed)
    {
        Debug.Log("Generating MD5 hash value from: \"" + unhashed + "\"");
        byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(unhashed);
        System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
        byte[] hash = md5.ComputeHash(inputBytes);
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        for (int i = 0; i < hash.Length; i++)
        {
            sb.Append(hash[i].ToString("x2"));
        }
        return sb.ToString();
    }

this one works and computes the same as both MySQL and php5

Use
Base64String
insted of ASCII
and caps/no caps has nothing to do with either hash or encryption
and I never said that hashing was encryption at all only that case has nothing to do with encryption going
I have already given him RSA code project in C# which is encryption not hashing

The Unify wiki has an MD5 script which is used in the server-side highscore example and known to be correct.

Thanks all for the replys.

@perlohmann
I will try your code…

@zumwalt
I remember your code too… I’m trying another things, then I will try that code. THanks :smile:

@andeeee
I tried exacly that code (it was the first) and its false, the generated code its diferent than PHP, AS and MySQL generated.
The MD5 in Unifywiki is not wrong, it works… but it gives a diferent code…

EDIT: I will try diferent inputs, maybe I’m doing something wrong (but I tried several ways)

Hye perlohmann,

It worked… thanks

wait what?

I use that and the sha1 and md5 code is the same as php? at least in javascript.

Hey bloodtiger… I dont know… I used that code and it returned me a diferent code from MySQL… the code of perlohmann worked… :lol:

But maybe I did something wrong with your code…

I din’t make it but it returns the same as php md5(“string”); code and sha1(“string”); code.

here pm for an example (It is a tied to another thing which I have been working on and don’t wish to share with the forum.

I had the same issue as xandeck but perlohmann’s code works perfectly.
Here it is in js:

function GetMD5HashString(unhashed : String) : String
{
        Debug.Log("Generating MD5 hash value from: \"" + unhashed + "\"");
        var inputBytes : byte[] = System.Text.Encoding.ASCII.GetBytes(unhashed);
        var md5 : System.Security.Cryptography.MD5 = System.Security.Cryptography.MD5.Create();
        var hash : byte[] = md5.ComputeHash(inputBytes);
        var sb : System.Text.StringBuilder = new System.Text.StringBuilder();
        for (var i : int = 0; i < hash.Length; i++)
        {
            sb.Append(hash[i].ToString("x2"));
        }
        return sb.ToString();
}

php charset encoding?

Thanks to Gaiyamato. I translated it for latest version.

    string GetMD5HashString(string unhashed)
    {
        Debug.Log("Generating MD5 hash value from: " + unhashed + "");
        byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(unhashed);
        System.Security.Cryptography.MD5 MD5 = System.Security.Cryptography.MD5.Create();
        byte[] hash = MD5.ComputeHash(inputBytes);
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        for (int i = 0; i < hash.Length; i++)
        {
            sb.Append(hash[i].ToString("x2"));
        }
        return sb.ToString();
    }
1 Like