1

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("\0" . $password . $newchal);
  $newpwd = pack("a32", $password);
  $pappassword = implode ("", unpack("H32", ($newpwd ^ $newchal)));
3
  • I don't see any encryption here... there's no way to decrypt it, because of the hash step Commented Nov 13, 2009 at 2:35
  • 2
    he doesn't want to decrypt it, just port the code to c# Commented Nov 13, 2009 at 2:39
  • 2
    I know, I was just pointing out an incorrect use of the term "encryption"... Commented Nov 13, 2009 at 3:25

2 Answers 2

3

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();
    }
Sign up to request clarification or add additional context in comments.

Comments

2

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("\0" . $password . $newchal);

MD% '\0' + $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.

1 Comment

Thanks RageZ. Could you post an input / output example of those pack results?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.