C# 到 PHP base64 编码/解码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/257462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# to PHP base64 encode/decode
提问by w-ll
So I have this c# application that needs to ping my web server thats running linux/php stack.
I am having problems with the c# way of base 64 encoding bytes.
所以我有这个 c# 应用程序需要 ping 我运行 linux/php 堆栈的 web 服务器。
我在使用 base 64 编码字节的 c# 方式时遇到问题。
my c# code is like:
我的 C# 代码是这样的:
byte[] encbuff = System.Text.Encoding.UTF8.GetBytes("the string");
String enc = Convert.ToBase64String(encbuff);
and php side:
和 php 端:
$data = $_REQUEST['in'];
$raw = base64_decode($data);
with larger strings 100+ chars it fails. I think this is due to c# adding '+'s in the encoding but not sure. any clues
使用更大的字符串 100+ 个字符它会失败。我认为这是由于 c# 在编码中添加了“+”但不确定。任何线索
采纳答案by Eoin Campbell
You should probably URL Encode your Base64 string on the C# side before you send it.
在发送之前,您可能应该在 C# 端对您的 Base64 字符串进行 URL 编码。
And URL Decode it on the php side prior to base64 decoding it.
并在base64解码之前在php端对其进行URL解码。
C# side
C#端
byte[] encbuff = System.Text.Encoding.UTF8.GetBytes("the string");
string enc = Convert.ToBase64String(encbuff);
string urlenc = Server.UrlEncode(enc);
and php side:
和 php 端:
$data = $_REQUEST['in'];
$decdata = urldecode($data);
$raw = base64_decode($decdata);
回答by Jon Skeet
Convert.ToBase64String doesn't seem to add anything extra as far as I can see. For instance:
Convert.ToBase64String 据我所知似乎没有添加任何额外的东西。例如:
byte[] bytes = new byte[1000];
Console.WriteLine(Convert.ToBase64String(bytes));
The above code prints out a load of AAAAs with == at the end, which is correct.
上面的代码打印出了一堆带有 == 的 AAAA,这是正确的。
My guess is that $data
on the PHP side doesn't contain what enc
did on the C# side - check them against each other.
我的猜测是,$data
在 PHP 端不包含enc
在 C# 端做了什么- 相互检查。
回答by Greg Hewgill
Note that +
is a valid character in base64 encoding, but when used in URLs it is often translated back to a space. This space may be confusing your PHP base64_decode
function.
请注意,这+
是 base64 编码中的有效字符,但在 URL 中使用时,它通常会转换回空格。这个空间可能会混淆你的 PHPbase64_decode
函数。
You have two approaches to solving this problem:
您有两种方法可以解决此问题:
- Use %-encoding to encode the + character before it leaves your C# application.
- In your PHP application, translate space characters back to + before passing to base64_decode.
- 使用 %-encoding 在 + 字符离开 C# 应用程序之前对其进行编码。
- 在您的 PHP 应用程序中,在传递给 base64_decode 之前将空格字符转换回 +。
The first option is probably your better choice.
第一个选项可能是您更好的选择。
回答by w-ll
in c#
在 C# 中
this is a <B>long</b>string. and lets make this a3214 ad0-3214 0czcx 909340 zxci 0324#$@#$%%13244513123
turns into
变成
dGhpcyBpcyBhIDxCPmxvbmc8L2I+c3RyaW5nLiBhbmQgbGV0cyBtYWtlIHRoaXMgYTMyMTQgYWQwLTMyMTQgMGN6Y3ggOTA5MzQwIHp4Y2kgMDMyNCMkQCMkJSUxMzI0NDUxMzEyMw==
for me. and i think that + is breaking it all.
为了我。我认为 + 正在打破这一切。
回答by Eoin Campbell
The PHP side should be: $data = $_REQUEST['in']; // $decdata = urldecode($data); $raw = base64_decode($decdata);
PHP 端应该是: $data = $_REQUEST['in']; // $decdata = urldecode($data); $raw = base64_decode($decdata);
The $_REQUEST should already be URLdecoded.
$_REQUEST 应该已经被 URL 解码了。
回答by cliff
This seems to work , replacing + with %2B...
这似乎有效,用 %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;
}