C# Javascript中MD5.hex()的.NET等价物

C# Javascript中MD5.hex()的.NET等价物,c#,javascript,C#,Javascript,我正在尝试连接到一个用auth创建的网站,该网站使用MD5.hex(password)在将密码发送到PHP之前对其进行加密。如何在C#中实现相同的加密 编辑1: Javascript(YUI库): C#NET 实用程序: public string getMD5(string input) { // Create a new instance of the MD5CryptoServiceProvider object. MD5 md5Hasher = M

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

编辑1:

Javascript(YUI库):

C#NET

实用程序:

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;
    }
公共字符串getMD5(字符串输入)
{
//创建MD5CryptoServiceProvider对象的新实例。
MD5 md5Hasher=MD5.Create();
//将输入字符串转换为字节数组并计算哈希。
byte[]data=md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
//创建新的Stringbuilder以收集字节
//并创建一个字符串。
StringBuilder sBuilder=新StringBuilder();
//循环遍历散列数据的每个字节
//并将每个字符串格式化为十六进制字符串。
for(int i=0;i
系统.安全.加密
命名空间中使用.NET


上面的链接包含一个简短的代码示例;你可能还想看看Jeff Attwood的CodeProject文章。

@Gio:你可能知道这一点,但以防万一:C#根本不支持加密。然而,正如米奇在下面回答的那样,.NET框架确实如此。愚蠢的我,我没有仔细检查我复制粘贴的实用程序函数,并意识到MD5已经对数据进行了十六进制处理。
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;
    }