I'm trying to mimic a php script that do the following :
- replace a GET vaiable's every space with a + sign ($var = preg_replace("/\s/","+",$_GET['var']); )
- 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 ?