恩从C#code URL,URL解密在PHP(添加额外的字符以某种方式) [英] Encode URL from C#, decrypt URL in PHP (extra characters being added somehow)
问题描述
我现在读这个主题如此多的话题,我能不明白的地方问题可能可能说谎。我从加密一个C#WinForm应用程序的URL的一部分。然后我想用PHP中的URL来读取和解密的URL(全部采用基地-64)。我有一些code到shrae:
结果
code加密URL(C#):
公共静态字符串Base64En code(字符串str)
{
字节[] = encbuff Encoding.UTF8.GetBytes(STR);
返回System.Web.HttpServerUtility.UrlTokenEn code(encbuff);
}
解密的URL的一部分:
Base64En code(CND0311J4S68CCU版本F.0BHPQOEM - F。);
返回:
Q05EMDMxMUo0UzY4Q0NVIFZlci4gRi4wQkhQUU9FTSAtIGY1
结果
code解密URL(PHP):
回声base64_de code(Q05EMDMxMUo0UzY4Q0NVIFZlci4gRi4wQkhQUU9FTSAtIGY1);
返回:
CND0311J4S68CCU版本。 F.0BHPQOEM - F5
所以,哪里是额外的5的回报从哪里来的结束?我不知道这出我的生活,很令人沮丧,因为你可以想像的。
我AP preciate任何帮助! - 以及任何建议
感谢您,
埃文
CND0311J4S68CCU版本F.0BHPQOEM - F
连接codeD为base64不是:
Q05EMDMxMUo0UzY4Q0NVIFZlci4gRi4wQkhQUU9FTSAtIGY1
也许别的东西添加 1
到了最后,因为
回声base64_de code(Q05EMDMxMUo0UzY4Q0NVIFZlci4gRi4wQkhQUU9FTSAtIGY);
给你你在找什么。这东西加入它实际上是 System.Web.HttpServerUtility.UrlTokenEn code
。问题是从MSDN <以下( / A>)
这(
System.Web.HttpServerUtility.UrlTokenEn code
)不使用标准的编码。它连接codeS与 - 和_字符,这是标准的。它也消除了=迹象。但是,而不是简单地删除它们(他们并不需要取消code中的字符串),它会替换他们以数字(0,1,2),表明已删除的的=号数。
块引用>所以要为它(演示):
&LT; PHP$ URLTOKEN =Q05EMDMxMUo0UzY4Q0NVIFZlci4gRi4wQkhQUU9FTSAtIGY1;
回声urltoken_de code($ URLTOKEN);功能urltoken_de code($令牌)
{
返回base64_de code(SUBSTR($令牌,0,-1));
}功能是pretty粗糙,并且可以改进实际处理它更具体的( DEMO2 一>)
功能urltoken_de code($令牌)
{
$ LEN = strlen的($令牌);
如果(!$ LEN)
返回$令牌; $位数= $令牌[$ LEN-1];
如果(!in_array($位数,(0,2)))
{
扔InvalidArgumentException(sprintf的(无效的结束位(%S)。',$位));
}
返回base64_de code(SUBSTR($令牌,0,-1));
}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
解决方案"CND0311J4S68CCU Ver. F.0BHPQOEM - f"
encoded as base64 is not:
Q05EMDMxMUo0UzY4Q0NVIFZlci4gRi4wQkhQUU9FTSAtIGY1
Probably something else is adding the
1
at the end, becauseecho base64_decode("Q05EMDMxMUo0UzY4Q0NVIFZlci4gRi4wQkhQUU9FTSAtIGY");
gives 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):This (
System.Web.HttpServerUtility.UrlTokenEncode
) does not use a standard encoding. It encodes with - and _ characters which is standard. It also removes the = signs. But rather than simply removing them (they're not necessary to decode the string), it replaces them with a digit (0, 1, 2) indicating the number of = signs that were removed.So go for it (Demo):
<?php $urltoken = "Q05EMDMxMUo0UzY4Q0NVIFZlci4gRi4wQkhQUU9FTSAtIGY1"; echo urltoken_decode($urltoken); function urltoken_decode($token) { return base64_decode(substr($token, 0, -1)); }
The function is pretty rough and could be improved to actually deal with it more specifically (Demo2):
function urltoken_decode($token) { $len = strlen($token); if (!$len) return $token; $digit = $token[$len-1]; if (!in_array($digit, range(0,2))) { throw InvalidArgumentException(sprintf('Invalid end digit (%s).', $digit)); } return base64_decode(substr($token, 0, -1)); }
这篇关于恩从C#code URL,URL解密在PHP(添加额外的字符以某种方式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!