pavanarya

Let us share the knowledge

Decrypting the WebResource.axd url

with 3 comments

Hi Everyone,

In my previous post(WebResource.axd) i explained  on how to embed the resources like ‘jpeg’,’js’ and other static files into a dll and how to make use of them in our project.

So, in this post we will see the techniques to decrypt the url that we get from the function GetWebResourceUrl().

string jspath= Page.ClientScript.GetWebResourceUrl(t, "ClassLibrary1.TestJavascript.js");
//The jspath looks somethings like this
///WebResource/WebResource.axd?d=iRUgqsGc-eNMUJXMKkTKJnWvTpI3RWO8FANelr8WhiOGVjLmQo5KGUjvqPgJnqcnCIr6qNyB-78stxdbAaHC5tkUgY9ZmT9RYO22V5eMD1_NrrLmu-Ae5rZKvIy2VIxVqkLYtnPLIVhaW6akVoMyqQ2&t=634556230497500000

So we are going to get an url of the above type when we make use of GetWebResourceUrl() method.

Initially the parameters that i am passing to the GetWebResourceUrl() are wrong. What i mean is instead of passing the full path of the Testjavascript.js (“ClassLibrary1.TestJavascript.js”) i am sending just “TestJavascript.js”.

As a result i am getting an url as output from the GetWebResourceUrl() method but when i am trying to make use of that in my code i am getting an exception.
I didnt understand the reason behind this. Finally i decided to decrypt the content of the query string and i found the there is an error in the way i am sending the full path of the resource file.

let me explain you about the decryption process.

string resourceurl= ClientScript.GetWebResourceUrl(typeof(ClassLibrary1.Class1), "ClassLibrary1.Testjavascript.js");
      ClientScript.RegisterClientScriptInclude("test", resourceurl);

      string[] temp=resourceurl.Split('?');
      string[] resourceurlpath = temp[1].Split('&');
      string urltodecrypt = resourceurlpath[0].Split('=')[1];
     System.Reflection.BindingFlags bf = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static;

     System.Reflection.MethodInfo DecryptString = typeof(System.Web.UI.Page).GetMethod("DecryptString", bf);

     string str2 = DecryptString.Invoke(null, new object[] { urltodecrypt }) as string;

So we will be getting the decrypted string into the variable str2.

string resourceurl= ClientScript.GetWebResourceUrl(typeof(ClassLibrary1.Class1), "ClassLibrary1.Testjavascript.js");
      ClientScript.RegisterClientScriptInclude("test", resourceurl);

this is used to get the webesource.axd url and the method RegisterClientScriptInclude() is used to register the javascript file from the server side.

After getting the url(WebResource/WebResource.axd?d=iRUgqsGc-eNMUJXMKkTKJnWvTpI3RWO8FANelr8WhiOGVjLmQo5KGUjvqPgJnqcnCIr6qNyB-78stxdbAaHC5tkUgY9ZmT9RYO22V5eMD1_NrrLmu-Ae5rZKvIy2VIxVqkLYtnPLIVhaW6akVoMyqQ2&t=634556230497500000) in the format i am trying to retrieve the d part from above url.
In the above url the query string “d” denotes the static file path that we are trying to make use(in our case javascript file).

string[] temp=resourceurl.Split('?');
      string[] resourceurlpath = temp[1].Split('&');
      string urltodecrypt = resourceurlpath[0].Split('=')[1];

the below piece of code is used to decrypt the “d” part of the webresource.axd url.

System.Reflection.BindingFlags bf = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static;

     System.Reflection.MethodInfo DecryptString = typeof(System.Web.UI.Page).GetMethod("DecryptString", bf);

     string str2 = DecryptString.Invoke(null, new object[] { urltodecrypt }) as string;
































About these ads

Written by pavanarya

November 19, 2011 at 3:43 pm

Posted in Asp.net

3 Responses

Subscribe to comments with RSS.

  1. i am getting Padding is invalid and cannot be removed. when i run this code

    Alex

    June 5, 2012 at 2:12 am

    • Hi Alex,
      I am not sure what is wrong with your code. I am able to run my code perfectly without any exception.
      One thing we can do. Can you email me your code so that i can verify it. my email id pavan.aryasomayajulu@gmail.com
      Thanks,
      Pavan

      pavanarya

      June 5, 2012 at 10:55 am

  2. I’m getting an error and I have the exact same code.

    “Parameter count mismatch.”
    at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
    at ………\WebResourceCompressionModule.cs:line 298

    Occurs only on a new server we set up.
    The .NET framework is same but some of the system.web and mscorlib versions are different b/w old and new servers.

    The DecryptString object has a private member called m_parameters.
    In the old server when debugging I see it has a single value which is a string object.
    In the new server, same code, same solution, same config, the same DecryptString object is showing 2 parameters.
    The additional parameter is a system.web.security.cryptography.purpose object.

    could this be the reason for the above mentioned error message?
    Is there an unknown bug with my current framework or certain core dlls?

    PRAMOD

    January 17, 2014 at 11:06 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 67 other followers

%d bloggers like this: