【问题标题】:Decrypt WebResource.axd URL generated on HTTPS instance解密在 HTTPS 实例上生成的 WebResource.axd URL
【发布时间】:2014-12-08 12:27:54
【问题描述】:

我有下面提到的代码:

string urlEncodedData = URL.Text;

byte[] encryptedData = HttpServerUtility.UrlTokenDecode(urlEncodedData);

Type machineKeySection = typeof(System.Web.Configuration.MachineKeySection);
Type[] paramTypes = new Type[] { typeof(bool), typeof(byte[]), typeof(byte[]), typeof(int), typeof(int) };
MethodInfo encryptOrDecryptData = machineKeySection.GetMethod("EncryptOrDecryptData", BindingFlags.Static | BindingFlags.NonPublic, null, paramTypes, null);

try
{
     byte[] decryptedData = (byte[])encryptOrDecryptData.Invoke(null, new object[] { false, encryptedData, null, 0, encryptedData.Length });
     string decrypted = Encoding.UTF8.GetString(decryptedData);

     decryptedLabel.BackColor = Color.Lime;
     decryptedLabel.Text = decrypted;
}
catch (TargetInvocationException)
{
     decryptedLabel.BackColor = Color.Red;
     decryptedLabel.Text = "Error decrypting data. Are you running your page on the same server and inside the same application as the web resource URL that was generated?";
}

它会解密并告诉我有关 webresource 的详细信息。 在本地它工作正常。

但在生产过程中,它总是给我来自 catch 块的以下消息

解密数据时出错。您是否在与生成的 Web 资源 URL 相同的服务器和相同的应用程序中运行您的页面?

我唯一的区别是使用 HTTPS 进行生产。上面的代码对 HTTPS 也有效吗,还是我必须对其进行更改?

【问题讨论】:

    标签: c# asp.net .net https webresource.axd


    【解决方案1】:

    我也使用此代码 sn-p 来解密 webresource.axd 参数,但最近它停止工作。

    也许是框架更改为 4.5,因为我在 .net 源代码中找到了这条评论 - 页面类,方法 DecryptString http://referencesource.microsoft.com/#System.Web/UI/Page.cs,18cf7b1fe99faea6

    if (AspNetCryptoServiceProvider.Instance.IsDefaultProvider) {
                // ASP.NET 4.5 Crypto DCR: Go through the new AspNetCryptoServiceProvider
                // if we're configured to do so.
                ICryptoService cryptoService = AspNetCryptoServiceProvider.Instance.GetCryptoService(purpose, CryptoServiceOptions.CacheableOutput);
                clearData = cryptoService.Unprotect(protectedData);
            }
            else {
                // If we're not configured to go through the new crypto routines,
                // fall back to the standard MachineKey crypto routines.
    #pragma warning disable 618 // calling obsolete methods
                clearData = MachineKeySection.EncryptOrDecryptData(fEncrypt: false, buf: protectedData, modifier: null, start: 0, length: protectedData.Length, useValidationSymAlgo: false, useLegacyMode: false, ivType: IVType.Hash);
    #pragma warning restore 618 // calling obsolete methods
            } 
    

    您确定唯一的区别是 http 和 https,也许还有框架版本?

    尽管如此 我使用了 DecryptString 方法而不是 EncryptOrDecryptData,下面的代码对我有用。您可以检查这是否也适合您:)

    private static string Decrypt(string webResourceParameter)
        {
            var purposeType = Type.GetType("System.Web.Security.Cryptography.Purpose, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
    
            if (purposeType == null)
                return null;
    
            try
            {
                var purpose = Activator.CreateInstance(purposeType, "AssemblyResourceLoader.WebResourceUrl");
    
                const BindingFlags decryptFlags = BindingFlags.NonPublic | BindingFlags.Static;
                var decryptString = typeof (Page).GetMethod("DecryptString", decryptFlags);
    
                var decrypt = decryptString.Invoke(null, new[] {webResourceParameter, purpose}) as string;
                return decrypt;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2020-03-07
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多