.NET(C#)的Java身份验证框架

时间:2020-04-12 21:35:44

标签: javascript c# cryptography md5 hmac

我尝试使用此处所述的相同密码:

http://www.java2s.com/Code/JavaScript/Security/MD5HashinginJavaScript.htm

此代码在我尝试为其编写工具的网站上使用。

我尝试使用以下代码在Visual Studio中调试它:

var api_url

$.getJSON("https://api.ipify.org/?format=json", function(e) {
    myIP = e.ip;
    //api_url = "https://cors-anywhere-ctrack.herokuapp.com/https://tools.keycdn.com/geo.json?host=" + myIP
});

//api_url = "https://cors-anywhere-ctrack.herokuapp.com/https://tools.keycdn.com/geo.json?host=" + myIP

function geoloc() {
  api_url = "https://cors-anywhere-ctrack.herokuapp.com/https://tools.keycdn.com/geo.json?host=" + myIP
  console.log(api_url)
  const response = await fetch(api_url)
  const data = await response.json();
  console.log(data)
}

geoloc();

最后我得到了此哈希:“ e14948da52f8b93122716c2bfaee6a9d”,表示变量md5Key =“ Test1”和密码=“ Test22”

有人知道我如何在C#中获得相同的结果吗? (控制台应用程序)。也许有一个框架,或者我只使用了错误的C#函数? (我这样尝试过:Signing a string with HMAC-MD5 with C#

关于

Hdrk

1 个答案:

答案 0 :(得分:0)

警告:除非您是OP并处理这种非常特殊的情况,否则请不要使用以下代码。相反,请阅读有关secure salted password hashing的信息。即使您不关心安全性,Microsoft也已经实现了标准的PBKDF2(因此与其他语言和框架实现兼容),并且具有易于重用的sample

您尚未实现来自CalcMD5PW的JS版本的自定义密码派生

private static string ConvertedMD5(string transactionKey, string password)
{
    //The lengthening start here
    string passwrd = "";
    var count = 0;
    var space = "\0";
    while (count <= (2048 - (password.Length + 1))) 
    {
        if (count == 0)
        {
            passwrd = password;
        }
        else
        {
            passwrd += password;
        }
        passwrd += space;
        count += (password.Length + 1);
    }

    while (count < 2048)
    {
        passwrd += space;
        count += 1;
    }
    //the following are bog standard HMACMD5
    var data = Encoding.UTF8.GetBytes(passwrd);
    var key = Encoding.UTF8.GetBytes(transactionKey);
    var hmac = new HMACMD5(key);
    var hashBytes = hmac.ComputeHash(data);
    return System.BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
}