English 中文(简体)
Convert PHP encryption code to C#
原标题:

I m trying to convert this piece of code from PHP to 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("" . $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.

so

$hexcal = pack( H32 , $challenge);

would convert a string like cca86bc64ec5889345c4c3d8dfc7ade9 to the actual 0xcca... de9

if $uamsecret exist do the same things with the MD5 of hexchal concacteate with the uamsecret. if ($uamsecret) { $newchal = pack ("H*", md5($hexchal . $uamsecret)); } else { $newchal = $hexchal; }

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

MD% + $password + $newchal

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

pad password to 32 byte

  $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.

问题回答

I also encountered the need of php s pack-unpack functions in c# but did not get any good resource.

So i thought to do it myself. I have verified the function s input with pack/unpack/md5 methods found at onlinephpfunctions.com. 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

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签