简体   繁体   English

解密问题时,Base-64 char数组的长度无效

[英]Invalid length for a Base-64 char array while decryption issue

I'm having a very strange issue and have no idea what can be causing it. 我有一个非常奇怪的问题,不知道是什么原因引起的。

A customer this morning emailed me about some customers getting an error when trying to view the his website, he also got the error but then when he tried again it worked. 今天早上有位客户通过电子邮件向我发送了一些客户在尝试查看其网站时遇到错误的消息,但他也收到了错误消息,但是当他再次尝试该方法时,它仍然起作用。

Looking at the logs, the error is with Chrome 65 and 67, I use 67 and do not get the error. 查看日志,错误是Chrome 65和67,我使用67,但未收到错误。

The querystring which is encrypted is below, this never worked for a customer but worked for me: 加密的查询字符串如下,它从未为客户服务,但为我服务:

AQAAANCMnd8BFdERjHoAwE%2fCl%2bsBAAAA0PyFDdX%2brkGTpXZG7C49nAQAAAACAAAAAAAQZgAAAAEAACAAAACI39m3OhUEFw3GBxXP%2bsVpw6zLJOqRkcJx1%2bFPcozLZgAAAAAOgAAAAAIAACAAAAAJpDYiaxnPjDprOQEA9u02%2bU0%2fDQDCIF7sXsjxaU3onYAAAACWWCv%2bKNSRbQjLTNeJjgE37yHviV1UmfJyoUn%2fcUg%2f0MTr23%2b16qZs9F%2fDNm4wU%2bJITVn3xXuarzcNB6ClJ2ZpwtEsMdUNBRUW0B7XA9%2bQjC69V1O2XqTp%2fgXQazHOITEBpgokD1tSbnv4pRMUfkVlogYoo0H9Lnf24FEDEnSp30AAAACwtnrmVACY71%2bcAAMANRoCuihUumid0i8P75KV0ZlUIRBXyOzASHwq9I7icvXWDbI2nNOa0mQDOgNdvZEti%2bYz AQAAANCMnd8BFdERjHoAwE%2fCl%2bsBAAAA0PyFDdX%2brkGTpXZG7C49nAQAAAACAAAAAAAQZgAAAAEAACAAAACI39m3OhUEFw3GBxXP%2bsVpw6zLJOqRkcJx1%2bFPcozLZgAAAAAOgAAAAAIAACAAAAAJpDYiaxnPjDprOQEA9u02%2bU0%2fDQDCIF7sXsjxaU3onYAAAACWWCv%2bKNSRbQjLTNeJjgE37yHviV1UmfJyoUn%2fcUg%2f0MTr23%2b16qZs9F%2fDNm4wU%2bJITVn3xXuarzcNB6ClJ2ZpwtEsMdUNBRUW0B7XA9%2bQjC69V1O2XqTp%2fgXQazHOITEBpgokD1tSbnv4pRMUfkVlogYoo0H9Lnf24FEDEnSp30AAAACwtnrmVACY71%2bcAAMANRoCuihUumid0i8P75KV0ZlUIRBXyOzASHwq9I7icvXWDbI2nNOa0mQDOgNdvZEti%2bYz

Below is my code, I'm hoping the issue is with this, but as some customers are having the issue and some not, I'm not holding my breath. 下面是我的代码,我希望问题出在这里,但是由于某些客户遇到了问题,而有些则没有,所以我没有屏息。

var encrtptUserId = EncryptionDecryption.WindowsEncrypted(encryptQueryParameters);

string urlToValidateUser = $"{baseUrl}?id={HttpUtility.UrlEncode(encrtptUserId)}";

public static string WindowsEncrypted(string text)
{
    return Convert.ToBase64String(ProtectedData.Protect(Encoding.Unicode.GetBytes(text), null, DataProtectionScope.LocalMachine));
}

public static string WindowsDecrypted(string text)
{
    return Encoding.Unicode.GetString(ProtectedData.Unprotect(Convert.FromBase64String(text), null, DataProtectionScope.LocalMachine));            
}

Its the decryption that is causing the issue, but not all the time 是导致问题的解密,但并非始终如此

Any help would be appreciated. 任何帮助,将不胜感激。

The query string includes encoded values, eg "%2b". 查询字符串包括编码值,例如“%2b”。 That's at least inconvenient. 至少这很不方便。

You could decode those values to end up with the original base64 value that you could then convert back to a byte array. 可以将这些值解码为原始的base64值,然后将其转换回字节数组。 but it would be better to use a web-safe base64 encoding to start with. 但最好使用网络安全的base64编码开始。

Convert.ToBase64String doesn't provide a URL-safe approach, but you can easily just use Replace on the result: Convert.ToBase64String不提供URL安全的方法,但是您可以轻松地对结果使用Replace

public static string WindowsEncrypted(string text)
{
    byte[] plainBinary= Encoding.Unicode.GetBytes(text);
    byte[] encrypted = ProtectedData.Protect(plainBinary, null, DataProtectionScope.LocalMachine);
    string base64 = Convert.ToBase64String(encrypted);
    // Return a url-safe string
    return base64.Replace("+", "-").Replace("/", "_").Replace("=", ".");
}

public static string WindowsDecrypted(string text)
{
    string base64 = text.Replace("-", "+").Replace("_", "/").Replace(".", "=");
    byte[] encrypted = Convert.FromBase64String(base64);
    byte[] plainBinary = ProtectedData.Unprotect(encrypted, null, DataProtectionScope.LocalMachine);
    return Encoding.Unicode.GetString(plainBinary);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM