私は次のことを行うphpスクリプトを模倣しようとしています:
- GET変数のすべてのスペースを+記号に置き換えます($ var = preg_replace( "/ \ s /"、 "+"、$ _ GET ['var']);)
- 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);
}
}
最後にもう1つ言いますが、成功したphpデコードには、登録記号や商標記号などの特別な記号が含まれていますが、C#バージョンには含まれていません。
また、php base64_decodeはサーバー言語の影響を受けますか?