简体   繁体   English

将PHP加密代码转换为C#

[英]Convert PHP encryption code to C#

I'm trying to convert this piece of code from PHP to C#. 我正在尝试将这段代码从PHP转换为C#。 It's part of a Captive Portal. 它是强制门户的一部分。 Could somebody explain what it does? 有人可以解释它的作用吗?

  $hexchal = pack ("H32", $challenge);
  if ($uamsecret) {
    $newchal = pack ("H*", md5($hexchal . $uamsecret));
  } else {
    $newchal = $hexchal;
  }
  $response = md5("\0" . $password . $newchal);
  $newpwd = pack("a32", $password);
  $pappassword = implode ("", unpack("H32", ($newpwd ^ $newchal)));

Eduardo, 爱德华多,

if you take a look at the pack manual, pack is used to convert a string in (hex, octal, binary )to his number representation. 如果您看一下pack手册, pack是用来将一个字符串(十六进制,八进制,二进制)转换为其数字表示形式的。

so 所以

$hexcal = pack('H32', $challenge);

would convert a string like 'cca86bc64ec5889345c4c3d8dfc7ade9' to the actual 0xcca... de9 会将“ cca86bc64ec5889345c4c3d8dfc7ade9”之类的字符串转换为实际的0xcca ... de9

if $uamsecret exist do the same things with the MD5 of hexchal concacteate with the uamsecret . 如果$ uamsecret存在,则用hexchal的MD5和uamsecret进行uamsecret if ($uamsecret) { $newchal = pack ("H*", md5($hexchal . $uamsecret)); if($ uamsecret){$ newchal = pack(“ H *”,md5($ hexchal。$ uamsecret)); } else { $newchal = $hexchal; } else {$ newchal = $ hexchal; } }

$response = md5("\0" . $password . $newchal);

MD% '\\0' + $password + $newchal MD%'\\ 0'+ $ password + $ newchal

$newpwd = pack("a32", $password);

pad password to 32 byte password为32字节

  $pappassword = implode ("", unpack("H32", ($newpwd ^ $newchal)));

do a xor newpwd and newchal and convert it to a hexadecimal string, I don't get the implode() maybe it's to convert to string to an array of character. 执行xor newpwdnewchal并将其转换为十六进制字符串,我没有得到implode()也许是将其转换为字符串转换为字符数组。

I also encountered the need of php's pack-unpack functions in c# but did not get any good resource. 我也遇到了C#中php的pack-unpack函数的需要,但没有得到任何好的资源。

So i thought to do it myself. 所以我想自己做。 I have verified the function's input with pack/unpack/md5 methods found at onlinephpfunctions.com. 我已经使用onlinephpfunctions.com上的pack / unpack / md5方法验证了该函数的输入。 Since i have done code only as per my requirements. 由于我仅根据我的要求完成了代码。 This can be extended for other formats 可以扩展为其他格式

Pack

    private static string pack(string input)
    {
        //only for H32 & H*
        return Encoding.Default.GetString(FromHex(input));
    }
    public static byte[] FromHex(string hex)
    {
        hex = hex.Replace("-", "");
        byte[] raw = new byte[hex.Length / 2];
        for (int i = 0; i < raw.Length; i++)
        {
            raw[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
        }
        return raw;
    }

MD5 MD5

    private static string md5(string input)
    {
        byte[] asciiBytes = Encoding.Default.GetBytes(input);
        byte[] hashedBytes = MD5CryptoServiceProvider.Create().ComputeHash(asciiBytes);
        string hashedString = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
        return hashedString;
    }

Unpack 解压

    private static string unpack(string p1, string input)
    {
        StringBuilder output = new StringBuilder();

        for (int i = 0; i < input.Length; i++)
        {
            string a = Convert.ToInt32(input[i]).ToString("X");
            output.Append(a);
        }

        return output.ToString();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM