3

我正在尝试模仿执行以下操作的 php 脚本:

  1. 用 + 号替换 GET 变量的每个空格 ($var = preg_replace("/\s/","+",$_GET['var']); )
  2. 解码为 base64:base64_decode($var);

第一个我添加了一个执行base64解码的方法:

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

但它似乎 UTF-8 没有完成这项工作,所以我尝试了相同的方法,但使用的是 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);
        }
    }

最后要说的是,成功的php解码包含特殊标志,如注册标志和商标标志,但C#版本没有!

另外,php base64_decode 是否受服务器语言影响?

4

1 回答 1

10

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

于 2009-03-17T23:09:19.203 回答