PHP base64_decode C# ما يعادلها
سؤال
أحاول تقليد php النصي أن تفعل ما يلي :
- محل على vaiable كل الفضاء مع علامة + ($var = preg_replace("/\s/","+",$_GET['var']);)
- فك تشفير base64 :base64_decode($var);
1 أضفت طريقة إجراء فك 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 1 اللاتينية ، الذي هو رمز الصفحة 28591:
public static string base64Decode(string data)
{
byte[] binary = Convert.FromBaseString(data);
return Encoding.GetEncoding(28591).GetString(binary);
}
PHP دليل unhelpfully فقط يقول:"قبل PHP 6, حرف هو نفس بايت.أن هناك بالضبط 256 شخصيات مختلفة ممكن." العار ليس كل بايت في الواقع يعني...