tags:

views:

534

answers:

1

I'm trying to mimic a php script that do the following :

  1. replace a GET vaiable's every space with a + sign ($var = preg_replace("/\s/","+",$_GET['var']); )
  2. decoding to base64 : base64_decode($var);

1st i added a method perform a base64 decoding :

        public string base64Decode(string data)
    {
        try
        {
            System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();

            System.Text.Decoder utf8Decode = encoder.GetDecoder();

            byte[] todecode_byte = Convert.FromBase64String(data);
            int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
            char[] decoded_char = new char[charCount];
            utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
            string result = new String(decoded_char);
            return result;
        }
        catch (Exception e)
        {
            throw new Exception("Error in base64Decode" + e.Message);
        }
    }

but it seams that UTF-8 is not doing the job , so i tried the same method but with a UTF-7

        public string base64Decode(string data)
    {
        try
        {
            System.Text.UTF7Encoding encoder = new System.Text.UTF7Encoding();

            System.Text.Decoder utf7Decode = encoder.GetDecoder();

            byte[] todecode_byte = Convert.FromBase64String(data);
            int charCount = utf7Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
            char[] decoded_char = new char[charCount];
            utf7Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
            string result = new String(decoded_char);
            return result;
        }
        catch (Exception e)
        {
            throw new Exception("Error in base64Decode" + e.Message);
        }
    }

one last thing to say, the successful php decoding contains special signs , like the registered sign and trademark sign but the C# version doesn't !

also , does php base64_decode affected by the server language ?

+6  A: 

UTF-7 is very unlikely to be what you want. You really need to know what encoding PHP is using. It may be using the default encoding for your system. Fortunately it's a lot easier to decode than you're making it:

public static string base64Decode(string data)
{
    byte[] binary = Convert.FromBaseString(data);
    return Encoding.Default.GetString(binary);
}

There's no need to explicitly mess around with Encoder :)

Another possibility is that PHP is using ISO Latin 1, which is code page 28591:

public static string base64Decode(string data)
{
    byte[] binary = Convert.FromBaseString(data);
    return Encoding.GetEncoding(28591).GetString(binary);
}

The PHP manual unhelpfully just says: "Before PHP 6, a character is the same as a byte. That is, there are exactly 256 different characters possible." Shame it doesn't say what each byte actually means...

Jon Skeet
you saved my day ! very much appreciated !
HeoQue
Jon Skeet you always impress.
BC
This one was easy. My recent hidebysig answer took a bit more digging ;)
Jon Skeet