Convert string with HASH MD5 to ToBase64String

可紊 提交于 2019-12-11 15:40:29

问题


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


回答1:


    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



来源:https://stackoverflow.com/questions/20374524/convert-string-with-hash-md5-to-tobase64string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!