将字符串值转换为md5

mnowg1ta  于 2021-08-09  发布在  Java
关注(0)|答案(4)|浏览(468)

我正在尝试将字符串转换为md5,我所做的是:

public static string MD5Hash(string input)
        {
            StringBuilder hash = new StringBuilder();
            MD5CryptoServiceProvider md5provider = new MD5CryptoServiceProvider();
            byte[] bytes = md5provider.ComputeHash(new UTF8Encoding().GetBytes(input));

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

当我在md5hash(“mycode”)中测试时,上面的代码返回:6403df8223bf310152ad235731e79902(32个字符)
但是我的数据库上的md5是0x6403df823bf310152ad235731e79902000000000000000000000000000000000000064字符
它缺少了begginig中的“0x”和结尾中的“0”

htrmnn0y

htrmnn0y1#

根据您的示例,在数据库中生成64个字符字符串的是:

public static string MD5Hash(string input)
{
    using (var md5 = new MD5CryptoServiceProvider())
        return string.Concat("0x",
            BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(input)))
            .Replace("-", string.Empty).PadRight(62, '0'));
}

我不会说出函数的名字 MS5Hash 在本例中,如您所见,数据库字符串是串联的字符串,md5散列只是每个字符串的中间部分。

j8yoct9x

j8yoct9x2#

md5算法的哈希大小是128位。
md5类的computehash方法将哈希作为16字节的数组返回。请注意,一些md5实现产生32个字符的十六进制格式的哈希。
所以你必须检查数据库为什么你得到额外的字符?
供您参考,正确的md5值 MyCode 将是8e995273698f53088fe2ad4a0888d213
下面是生成md5的代码。

public static string GenerateMD5(string input)
{

    using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
    {
        byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
        byte[] hashBytes = md5.ComputeHash(inputBytes);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hashBytes.Length; i++)
        {
            sb.Append(hashBytes[i].ToString("X2"));
        }
        return sb.ToString();
    }
}

参考文献:https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5?view=netcore-3.1#remarks

rjjhvcjd

rjjhvcjd3#

这对我有用-

public static string MD5Hash(string text)
    {
        MD5 md5 = new MD5CryptoServiceProvider();

        //compute hash from the bytes of text  
        md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));

        //get hash result after compute it  
        byte[] result = md5.Hash;

        StringBuilder strBuilder = new StringBuilder();
        for (int i = 0; i < result.Length; i++)
        {
            //change it into 2 hexadecimal digits  
            //for each byte  
            strBuilder.Append(result[i].ToString("x2"));
        }

        return strBuilder.ToString();
    }
idv4meu8

idv4meu84#

试试这个

public static string Md5Hash(string text)
    {
        using (var md5 = new MD5CryptoServiceProvider())
        {
            using (var tds = new TripleDESCryptoServiceProvider())
            {
                tds.Key = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(encryptionKey));
                tds.Mode = CipherMode.ECB;
                tds.Padding = PaddingMode.PKCS7;

                using (var transform = tds.CreateEncryptor())
                {
                    byte[] textBytes = UTF8Encoding.UTF8.GetBytes(text);
                    byte[] bytes = transform.TransformFinalBlock(textBytes, 0, textBytes.Length);
                    return Convert.ToBase64String(bytes, 0, bytes.Length);
                }
            }
        }
    }

相关问题