从 C# 编码 URL,用 PHP 解密 URL(以某种方式添加额外字符)
到目前为止,我已经阅读了很多关于这个主题的主题,我不明白问题可能出在哪里。我正在加密 C# winform 应用程序中的 URL 的一部分。然后我想使用 php 读取 URL 并解密 URL(全部使用 Base-64)。我确实有一些代码需要加密:
加密 URL 的代码 (C#):
public static string Base64Encode(string str)
{
byte[] encbuff = Encoding.UTF8.GetBytes(str);
return System.Web.HttpServerUtility.UrlTokenEncode(encbuff);
}
解密 URL 的一部分:
Base64Encode("CND0311J4S68CCU Ver. F.0BHPQOEM - f");
返回:
Q05EMDMxMUo0UzY4Q0NVIFZlci4gRi4wQkhQUU9FTSAtIGY1
解密 URL 的代码 (PHP):
echo base64_decode("Q05EMDMxMUo0UzY4Q0NVIFZlci4gRi4wQkhQUU9FTSAtIGY1");
返回:
CND0311J4S68CCU Ver. F.0BHPQOEM - f5
那么,返回末尾多出来的“5”是哪里来的呢?我一生都无法弄清楚这一点,正如你想象的那样,这非常令人沮丧。
我感谢任何对此的帮助 - 以及任何建议!
谢谢你,
埃文
I have read so many topics on this very subject by now, I can't understand where the issue could possibly lie. I am encrypting part of a URL from a C# winform application. I then want to read in the URL using php and decrypt the url (all using base-64). I do have some code to shrae:
Code to encrypt URL (C#):
public static string Base64Encode(string str)
{
byte[] encbuff = Encoding.UTF8.GetBytes(str);
return System.Web.HttpServerUtility.UrlTokenEncode(encbuff);
}
Decrypt a section of the URL:
Base64Encode("CND0311J4S68CCU Ver. F.0BHPQOEM - f");
Returns:
Q05EMDMxMUo0UzY4Q0NVIFZlci4gRi4wQkhQUU9FTSAtIGY1
Code to decrypt URL (PHP):
echo base64_decode("Q05EMDMxMUo0UzY4Q0NVIFZlci4gRi4wQkhQUU9FTSAtIGY1");
Returns:
CND0311J4S68CCU Ver. F.0BHPQOEM - f5
So, where is the extra "5" at the end of the return coming from? I cannot figure this out for the life of me, quite frustrating as you could imagine.
I appreciate any help with this - as well as any suggestions!
Thank you,
Evan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编码为 base64 不是:
可能还有其他东西在末尾添加
1
,因为它可以提供您要查找的内容。添加它的东西实际上是 System.Web.HttpServerUtility.UrlTokenEncode。问题如下(来自 MSDN) :
所以去吧(演示):
该功能非常粗糙,可以改进以更具体地实际处理它( 演示2):
encoded as base64 is not:
Probably something else is adding the
1
at the end, becausegives you what you're looking for. And that something adding it is in fact
System.Web.HttpServerUtility.UrlTokenEncode
. The issue is the following (from MSDN):So go for it (Demo):
The function is pretty rough and could be improved to actually deal with it more specifically (Demo2):
有趣 - 当我在 此站点 编码“CND0311J4S68CCU Ver. F.0BHPQOEM - f”时(使用 JavaScript )它编码为“Q05EMDMxMUo0UzY4Q0NVIFZlci4gRi4wQkhQUU9FTSAtIGY”。如果我解码“Q05EMDMxMUo0UzY4Q0NVIFZlci4gRi4wQkhQUU9FTSAtIGY1”,我会在最后得到额外的 5。显然问题出在编码端 - 也许尝试在将您正在编码的值传递到 Base64 编码器之前输出它,以确保它正是您认为的那样?
Interesting - when I encode "CND0311J4S68CCU Ver. F.0BHPQOEM - f" at this site (using JavaScript) it encodes as "Q05EMDMxMUo0UzY4Q0NVIFZlci4gRi4wQkhQUU9FTSAtIGY". If I decode "Q05EMDMxMUo0UzY4Q0NVIFZlci4gRi4wQkhQUU9FTSAtIGY1" I get the extra 5 on the end. Apparently the problem is on the encoding end - maybe try outputting the value you're encoding just before it's passed to the base64 encoder to make sure it's exactly what you think it is?