Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Android和C之间的Base64 url安全编码和解码#_C#_Android_Base64_Decode_Encode - Fatal编程技术网

C# Android和C之间的Base64 url安全编码和解码#

C# Android和C之间的Base64 url安全编码和解码#,c#,android,base64,decode,encode,C#,Android,Base64,Decode,Encode,在我的android应用程序中,我使用java.util.Base64编码和解码,并使用标志URL\u SAFE和NO\u WRAP 但是,当我尝试在我的C#应用程序中使用HttpServerUtility.urltokencode对其进行解码时,我得到的是null。在这种状态下,我的编码字符串也无法在Android应用程序上解码 我错过了什么?URL\u SAFE标志是否确保Base64字符串没有+,/和任何额外的填充?为什么urltokenecode不接受Base64值 urltokenco

在我的android应用程序中,我使用
java.util.Base64
编码和解码,并使用标志
URL\u SAFE
NO\u WRAP

但是,当我尝试在我的C#应用程序中使用
HttpServerUtility.urltokencode
对其进行解码时,我得到的是
null
。在这种状态下,我的编码字符串也无法在Android应用程序上解码

我错过了什么?
URL\u SAFE
标志是否确保Base64字符串没有
+
/
和任何额外的填充?为什么
urltokenecode
不接受Base64值


urltokencode
返回
null
,因为我传递的是
字符串而不是
UrlToken

在Android中,我坚持编码/解码的
URL\u-SAFE
NO\u-WRAP
Base64标志,我成功地将我的C应用程序更改为以URL\u安全的方式解码/编码

    public string UrlEncode(string str)
    {
        if (str == null || str == "")
        {
            return null;
        }

        byte[] bytesToEncode = System.Text.UTF8Encoding.UTF8.GetBytes(str);
        String returnVal = System.Convert.ToBase64String(bytesToEncode);

        return returnVal.TrimEnd('=').Replace('+', '-').Replace('/', '_');
    }

    public string UrlDecode(string str)
    {
        if (str == null || str == "")
        {
            return null;
        }

        str.Replace('-', '+');
        str.Replace('_', '/');

        int paddings = str.Length % 4;
        if (paddings > 0)
        {
            str += new string('=', 4 - paddings);
        }

        byte[] encodedDataAsBytes = System.Convert.FromBase64String(str);
        string returnVal = System.Text.UTF8Encoding.UTF8.GetString(encodedDataAsBytes);
        return returnVal;
    }

有用的,谢谢。但是,是否有一种url安全编码的C版本不需要我们手动替换所有这些值(=,+,/)?请注意,将“str.replace”(“-”、“+”)和“str.replace”(“,”/”)替换为“str=str.replace”(“-”、“+”)和“str=str.replace”(“,”/”)。