MD5 niezgodność skrótów

0

Witam kolegów !

Jestem początkującym programistą, mam problem z generowaniem skrótu za pomocą MD5 w C#.

Posiadam funkcję hashującą stringa:

 
private static string MD5Hash(string input)
        {
            StringBuilder hash = new StringBuilder();

            MD5 md5 = MD5.Create();
            byte[] inputbytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hashbytes = md5.ComputeHash(inputbytes);         

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

wywołując powyższą funkcję z argumentem "aaa" otrzymuję e6a97266e55eb41d155087dc21601507
Mam inną aplikację pisaną w C++ framework QT która również posiada funkcję hashującą.
wynik dla stringa "aaa" to 47bce5c74f589f4867dbd57e9ca9f808 (generatory na stronach www generują właśnie taki skrót, wnioskuję z tego że poniższy kod robi to poprawnie)

 

        QCryptographicHash hasloszyfr(QCryptographicHash::Md5);
        hasloszyfr.reset();
        hasloszyfr.addData(nowe_haslo.toLatin1());
        QString zaszyfrowanehaslo=hasloszyfr.result().toHex();

Problem polega na tym że obie aplikacje mają korzystać z tej samej bazy a generują różne hashe dla tych samych stringów.

0

Głowy za to nie dam, ale wydaje mi się że to przez zmianę na bity.

0

A nie dlatego, że w C# używasz ASCII, a w C++ Latin1(ISO 8859-1)?

0

Ja tak generuje md5, gdzie stream to chociażby stream ciągu znaków, plik itp

                    using (var md5 = MD5.Create())
                    {
                            string md5_text = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
                    }
0
static void Main(string[] args)
{
    string source = "aaa";
    using (MD5 md5Hash = MD5.Create())
    {
        string hash = GetMd5Hash(md5Hash, source);
        Console.WriteLine(hash);
        Console.WriteLine(hash.Equals("47bce5c74f589f4867dbd57e9ca9f808")); // True
    }
}

static string GetMd5Hash(MD5 md5Hash, string input)
{
    byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
    StringBuilder sb = new StringBuilder();
    foreach (byte t in data)
    {
        sb.Append(t.ToString("x2"));
    }

    return sb.ToString();
}
0

Wygląda na to że jest to problem z kodowaniem.
zrobiłem użyłem konwersji:

private static string MD5Hash(string input)
        {
            Encoding uni = Encoding.Unicode;
            byte[] uniBytes = uni.GetBytes(input);
            Encoding latin1 = Encoding.GetEncoding(28591);
            byte[] latinBytes = Encoding.Convert(uni, latin1, uniBytes);

            MD5 md5 = MD5.Create();
            StringBuilder hash = new StringBuilder();
            byte[] hashbytes = md5.ComputeHash(latinBytes);

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

mimo to problem dalej istnieje

1 użytkowników online, w tym zalogowanych: 0, gości: 1