JaimeASV
JaimeASV

Reputation: 163

Convert string with HASH MD5 to ToBase64String

this is my problem,

i have this code that accepts clean text with passwords and returns Base64MD5 hashes

    private static string GetMd5Base64Pass(string userpwd)
    {
        MD5 md5 = new MD5CryptoServiceProvider();
        return Convert.ToBase64String(md5.ComputeHash(Encoding.ASCII.GetBytes(userpwd)));
    }

And i need to reuse it to accept MD5 string hashes and return in Base64MD5.

i tried to do this:

    private static string GetMd5Base64PassMD5(string userpwd)
    {
        MD5 md5 = new MD5CryptoServiceProvider();

        return Convert.ToBase64String(Encoding.ASCII.GetBytes(userpwd));
    }

but the returns are completely different.

already tried to convert the string to bytearray, didn't work.

I need to insert one string with 32bits MD5, and return it in Base64String.

thks

------------------------------ Edited

Example:

Password is 123123:

MD5 is: 4297f44b13955235245b2497399d7a93

Base64String of MD5 is: Qpf0SxOVUjUkWySXOZ16kw==

I need to get

this: Qpf0SxOVUjUkWySXOZ16kw==

from

this hash string 4297f44b13955235245b2497399d7a93

Upvotes: 1

Views: 6979

Answers (1)

JaimeASV
JaimeASV

Reputation: 163

    public static string ConvertHexStringToBase64(string hexString)
    {
        byte[] buffer = new byte[hexString.Length / 2];
        for (int i = 0; i < hexString.Length; i++)
        {
            buffer[i / 2] = Convert.ToByte(Convert.ToInt32(hexString.Substring(i, 2), 16));
            i += 1;
        }
        string res = Convert.ToBase64String(buffer);
        return res;
    }

this receives md5 string hashes and transforms it to Base64 Hex

Upvotes: 2

Related Questions