我正在尝试连接到我使用auth制作的网站,该网站使用MD5.hex(密码)加密密码,然后再将其发送到PHP。我怎样才能在C#中实现相同的加密?
EDIT1:
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;
}
答案 0 :(得分:7)
在System.Security.Cryptography
命名空间中使用.NET MD5 Class。
上面的链接包含一个简短的代码示例;您可能还想查看Jeff Attwood的CodeProject文章.NET Encryption Simplified。