質問

次のことを行うphpスクリプトを模倣しようとしています:

  1. GET変数のすべてのスペースを+記号で置き換えます($ var = preg_replace(<!> quot; / \ s / <!> quot;、<!> quot; + <!> quot;、$ _ 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はサーバー言語の影響を受けますか?

役に立ちましたか?

解決

UTF-7があなたの望むものになる可能性は非常に低いです。 PHPが使用しているエンコーディングを知る必要があります。システムのデフォルトのエンコーディングを使用している可能性があります 。幸いなことに、作成するよりも解読がはるかに簡単です:

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

Encoderを明示的にいじる必要はありません:)

別の可能性は、PHPがISO Latin 1を使用していることです。これはコードページ28591:です

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

PHPマニュアルには、次のように書かれています:<!> quot; PHP 6より前は、文字はバイトと同じです。つまり、正確に256の異なる文字が可能です。<!> quot;残念なことに、各バイトが実際に意味 ...

の意味を言っていない
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top