所以我必须这c#应用程序,需要平我的网服务器就运行linux/php堆。
我有问题的c#方式的基64编码的字节。

我的c#码是这样的:

byte[] encbuff = System.Text.Encoding.UTF8.GetBytes("the string");
String enc = Convert.ToBase64String(encbuff);

和php面:

$data = $_REQUEST['in'];
$raw = base64_decode($data);

与大串100+chars它失败。我认为这是由于c#添加'+'s的编码,但不能确定。任何线索

有帮助吗?

解决方案

您可能应该在发送之前对C#端的Base64字符串进行URL编码。

和URL在base64解码之前在php端解码它。

C#side

byte[] encbuff = System.Text.Encoding.UTF8.GetBytes("the string");
string enc = Convert.ToBase64String(encbuff);
string urlenc = Server.UrlEncode(enc);

和php方面:

$data = 

您可能应该在发送之前对C#端的Base64字符串进行URL编码。

和URL在base64解码之前在php端解码它。

C#side

byte[] encbuff = System.Text.Encoding.UTF8.GetBytes("the string");
string enc = Convert.ToBase64String(encbuff);
string urlenc = Server.UrlEncode(enc);

和php方面:

<*>REQUEST['in']; $decdata = urldecode($data); $raw = base64_decode($decdata);

其他提示

注意, + 是一个有效的角色在base64编码,但当用于网址也往往是翻译回的一个空间。这个空间可能会造成混乱PHP base64_decode 功能。

你有两种办法来解决这个问题:

  • 使用%编码来编码+字之前就离开你C#应用程序。
  • 在你的PHP应用、翻译空间符回+之前通过base64_decode.

第一种选择可能是你最好的选择。

这似乎有效,用%2B取代+ ......

private string HTTPPost(string URL, Dictionary<string, string> FormData)
{

    UTF8Encoding UTF8encoding = new UTF8Encoding();
    string postData = "";

    foreach (KeyValuePair<String, String> entry in FormData)
    {
            postData += entry.Key + "=" + entry.Value + "&";
    }

    postData = postData.Remove(postData.Length - 1);

    //urlencode replace (+) with (%2B) so it will not be changed to space ( )
    postData = postData.Replace("+", "%2B");

    byte[] data = UTF8encoding.GetBytes(postData); 

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);

    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = data.Length;

    Stream strm = request.GetRequestStream();
    // Send the data.
    strm.Write(data, 0, data.Length);
    strm.Close();

    WebResponse rsp = null;
    // Send the data to the webserver
    rsp = request.GetResponse();

    StreamReader rspStream = new StreamReader(rsp.GetResponseStream());
    string response = rspStream.ReadToEnd();

    return response;

}

Convert.ToBase64String似乎没有添加任何额外的东西,据我所见。例如:

byte[] bytes = new byte[1000];
Console.WriteLine(Convert.ToBase64String(bytes));

上面的代码在末尾打印出一堆带有==的AAAA,这是正确的。

我的猜测是PHP端的 $ data 不包含 enc 在C#端做的事情 - 互相检查。

在c#

this is a <B>long</b>string. and lets make this a3214 ad0-3214 0czcx 909340 zxci 0324#$@#$%%13244513123

变成

dGhpcyBpcyBhIDxCPmxvbmc8L2I+c3RyaW5nLiBhbmQgbGV0cyBtYWtlIHRoaXMgYTMyMTQgYWQwLTMyMTQgMGN6Y3ggOTA5MzQwIHp4Y2kgMDMyNCMkQCMkJSUxMzI0NDUxMzEyMw==
对我来说。我认为+正在打破这一切。

PHP方面应该是: $ data = $ _REQUEST ['in']; // $ decdata = urldecode($ data); $ raw = base64_decode($ decdata);

$ _REQUEST应该已经过URL解码。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top