ITh*_*wer 4 c# hash md5 cryptography
我已经尝试了我可以在网上找到的每个例子,但是我无法使用我的.NET代码从我的VB6应用程序生成相同的MD5哈希结果.
VB6应用程序产生与此站点相同的结果:http: //www.functions-online.com/md5.html
但是我无法在C#中使用相同的输入获得相同的结果(使用MD5.ComputeHash方法或FormsAuthentication加密方法)
请帮忙!!!!
根据要求,这里有一些代码.这是从MSDN直接推送的:
public string hashString(string input)
{
// Create a new instance of the MD5CryptoServiceProvider object.
MD5 md5Hasher = MD5.Create();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
Run Code Online (Sandbox Code Playgroud)
我的测试字符串是:
QWERTY123TEST
此代码的结果是:
8c31a947080131edeaf847eb7c6fcad5
Run Code Online (Sandbox Code Playgroud)
测试MD5的结果是:
f6ef5dc04609664c2875895d7da34eb9
Run Code Online (Sandbox Code Playgroud)
注意:TestMD5的结果是我所期待的
注意:我真的非常非常愚蠢,抱歉 - 只是意识到我的错误输入.我一旦硬编码就行了.谢谢您的帮助
Dan*_*iel 16
这是我认识的C#MD5方法,我用它通过不同的web restful API进行身份验证
public static string GetMD5Hash(string input)
{
System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
bs = x.ComputeHash(bs);
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach (byte b in bs)
{
s.Append(b.ToString("x2").ToLower());
}
return s.ToString();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8341 次 |
| 最近记录: |