c# - .NET Equivalent of MD5.hex() in Javascript - Stack Overflow

I'm trying to connect to a website I made with auth that uses MD5.hex(password) to encrypt the pas

I'm trying to connect to a website I made with auth that uses MD5.hex(password) to encrypt the password before sending it to the PHP. How could I achieve the same encryption in C#?

EDIT1:

Javascript (YUI Library):

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));

Utility:

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 pute 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;
    }

I'm trying to connect to a website I made with auth that uses MD5.hex(password) to encrypt the password before sending it to the PHP. How could I achieve the same encryption in C#?

EDIT1:

Javascript (YUI Library):

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));

Utility:

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 pute 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;
    }
Share Improve this question edited Apr 26, 2010 at 4:37 Gio Borje asked Apr 26, 2010 at 3:57 Gio BorjeGio Borje 21.1k7 gold badges39 silver badges50 bronze badges 1
  • @Gio: you probably knew this, but just in case: C# has no encryption support at all. However, the .NET Framework does, as Mitch answered below. – John Saunders Commented Apr 26, 2010 at 4:02
Add a ment  | 

1 Answer 1

Reset to default 7

Using .NET's MD5 Class in the System.Security.Cryptography namespace.

The link above contains a short code example; you might also want to check out Jeff Attwood's CodeProject article .NET Encryption Simplified.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742417176a4439965.html

相关推荐

  • c# - .NET Equivalent of MD5.hex() in Javascript - Stack Overflow

    I'm trying to connect to a website I made with auth that uses MD5.hex(password) to encrypt the pas

    7小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信