gpt4 book ai didi

c# - .NET 等效于 Javascript 中的 MD5.hex()

转载 作者:搜寻专家 更新时间:2023-11-01 05:26:53 24 4
gpt4 key购买 nike

我正在尝试连接到我使用 auth 创建的网站,该网站在将密码发送到 PHP 之前使用 MD5.hex(password) 对密码进行加密。我怎样才能在 C# 中实现相同的加密?

编辑1:

Javascript(YUI 库):

pw = MD5.hex(pw);
this.chap.value = MD5.hex(pw + this.token.value);

C#.NET

string pw = getMD5(getHex(getMD5(getHex(my_password)) + my_token));

效用:

public string getMD5(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();
}

public string getHex(string asciiString)
{
string hex = "";
foreach (char c in asciiString)
{
int tmp = c;
hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
}
return hex;
}

最佳答案

使用 .NET 的 MD5 ClassSystem.Security.Cryptography 命名空间中。

上面的链接包含一个简短的代码示例;您可能还想查看 Jeff Attwood 的 CodeProject 文章 .NET Encryption Simplified .

关于c# - .NET 等效于 Javascript 中的 MD5.hex(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2711084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com