문제

다음을 수행하는 PHP 스크립트를 모방하려고합니다.

  1. get vaiable의 모든 공간을 + 부호로 교체하십시오 ($ 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는 서버 언어의 영향을 받습니까?

도움이 되었습니까?

해결책

UTF-7은 당신이 원하는 것이 될 가능성이 거의 없습니다. 인코딩 PHP가 무엇을 사용하고 있는지 알아야합니다. 그것 5월 시스템에 대한 기본 인코딩을 사용해야합니다. 다행히도 당신이 만드는 것보다 해독하는 것이 훨씬 쉽습니다.

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 매뉴얼은 도움이되지 않게 말합니다. "PHP 6 이전에는 캐릭터가 바이트와 동일합니다. 즉, 정확히 256 개의 다른 문자가 있습니다." 부끄러운 것은 각 바이트가 실제로 무엇을 말하지 않습니다 수단...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top