How to Encrypt/Decrypt Parameter passing with URL in javascripthttp://forums.asp.net/t/1772334.aspx/1?How+to+Encrypt+Decrypt+Parameter+passing+with+URL+in+javascriptFri, 20 Apr 2012 13:48:13 -040017723344844281http://forums.asp.net/p/1772334/4844281.aspx/1?How+to+Encrypt+Decrypt+Parameter+passing+with+URL+in+javascriptHow to Encrypt/Decrypt Parameter passing with URL in javascript <p>Hi</p> <p>I have created shtml page with login and password. when user click on the submit button I am calling javascript and passing value to the particular URL for e.g</p> <p>function Login(){<br> &nbsp;&nbsp;&nbsp; var id=document.getElementById('txtUserName').value;<br> &nbsp;&nbsp; &nbsp;var pwd=document.getElementById('txtPassword').value;<br> &nbsp;&nbsp; &nbsp;<br> &nbsp;&nbsp; &nbsp;if (id=='' || pwd=='')<br> &nbsp;&nbsp; &nbsp;<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;alert('Please Enter Valid User Name/Password')<br> &nbsp;&nbsp; &nbsp;else {<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; debugger;<br> &nbsp;&nbsp;&nbsp; document.Form1.action = &quot;https://xxx.aspx?x=&quot; &#43; id &#43; &quot;&amp;y=&quot; &#43; pwd;<br> &nbsp;&nbsp; &nbsp;document.Form1.submit();<br> &nbsp;&nbsp; &nbsp; }</p> <p></p> <p>is it possible that i can encrypt this id and pwd using jquery and on aspx decrypt it again.</p> 2012-02-22T05:07:53-05:004844288http://forums.asp.net/p/1772334/4844288.aspx/1?Re+How+to+Encrypt+Decrypt+Parameter+passing+with+URL+in+javascriptRe: How to Encrypt/Decrypt Parameter passing with URL in javascript <p>by encrypting information in javascript, u will just make it difficult for naive hacker to get this data....</p> <p>but, since anyone can see encryption logic of javascript... (as javascript runs on client side browser) it makes no good mechanism</p> <p>if u r using ssl (https) then u can rely on that the information is being sent from client to server in secure way</p> <p>hope this helps...</p> 2012-02-22T05:13:58-05:004844297http://forums.asp.net/p/1772334/4844297.aspx/1?Re+How+to+Encrypt+Decrypt+Parameter+passing+with+URL+in+javascriptRe: How to Encrypt/Decrypt Parameter passing with URL in javascript <p>You cannot encrypt data client side and decrypt server side. Instead.</p> <p>1.&nbsp;Do a postback and then process the data on the same page rather then redirecting to some other page</p> <p>2. Another thing you can do is postback to the same page. Encrypt and then redirect</p> <p><a href="http://madskristensen.net/post/HttpModule-for-query-string-encryption.aspx">http://madskristensen.net/post/HttpModule-for-query-string-encryption.aspx</a></p> <p>3. Another way which is the best is that. Remove QueryString and add name field to your username and password textbox.</p> <p>Then on the recepient page (xxx.aspx) you can get values easily using</p> <p>string username = Request.Form[&quot;Name of username textbox&quot;];</p> 2012-02-22T05:19:19-05:004844388http://forums.asp.net/p/1772334/4844388.aspx/1?Re+How+to+Encrypt+Decrypt+Parameter+passing+with+URL+in+javascriptRe: How to Encrypt/Decrypt Parameter passing with URL in javascript <p>HI Mudass,</p> <p></p> <p>Can you give me the example of the Point 3. point 1 &amp; 2 are not fit in my criteria</p> 2012-02-22T06:03:57-05:004846332http://forums.asp.net/p/1772334/4846332.aspx/1?Re+How+to+Encrypt+Decrypt+Parameter+passing+with+URL+in+javascriptRe: How to Encrypt/Decrypt Parameter passing with URL in javascript <p>Hi, for example, you want to submit the username and password to xxx.aspx page:</p> <pre class="prettyprint">&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt; &lt;head&gt; &lt;title&gt;&lt;/title&gt; &lt;script type=&quot;text/javascript&quot;&gt; function Login(){ var id=document.getElementById('txtUserName').value; var pwd=document.getElementById('txtPassword').value; if (id=='' || pwd=='') alert('Please Enter Valid User Name/Password') else { debugger; document.Form1.submit(); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&quot;Form1&quot; action=&quot; xxx.aspx&quot; method=&quot;post&quot;&gt; &lt;input id=&quot;txtUserName&quot; type=&quot;text&quot; name=&quot;username&quot; /&gt;&lt;input id=&quot;txtPassword&quot; type=&quot;text&quot; name='password' /&gt;&lt;input id=&quot;Submit1&quot; type=&quot;submit&quot; value=&quot;submit&quot; onclick=&quot;Login();&quot; /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p>With &ldquo;xxx.aspx&rdquo; page, you can get the username and password with the following methods:</p> <pre class="prettyprint">&lt;script runat="server"&gt; protected void Page_Load(object sender, EventArgs e) { string username = Request["username"]; string password = Request["password"]; } &lt;/script&gt; </pre> 2012-02-23T05:05:16-05:004847117http://forums.asp.net/p/1772334/4847117.aspx/1?Re+How+to+Encrypt+Decrypt+Parameter+passing+with+URL+in+javascriptRe: How to Encrypt/Decrypt Parameter passing with URL in javascript <p>Hey Allen..That's work..Thankyou so much for your help...awesome...:)</p> 2012-02-23T11:06:23-05:004847135http://forums.asp.net/p/1772334/4847135.aspx/1?Re+How+to+Encrypt+Decrypt+Parameter+passing+with+URL+in+javascriptRe: How to Encrypt/Decrypt Parameter passing with URL in javascript <p>use the following instead in page load</p> <p>string username <span class="pun">=</span><span class="pln"> </span><span class="typ">Request.Form</span><span class="pun">[</span><span class="str">&quot;username&quot;</span><span class="pun">];</span><span class="pln">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p> <p><span class="pln">string password </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Request.Form</span><span class="pun">[</span><span class="str">&quot;password&quot;</span><span class="pun">];</span><span class="pln"> </span></p> 2012-02-23T11:13:29-05:004943053http://forums.asp.net/p/1772334/4943053.aspx/1?Re+How+to+Encrypt+Decrypt+Parameter+passing+with+URL+in+javascriptRe: How to Encrypt/Decrypt Parameter passing with URL in javascript <p>Hi, I have found a close match to your requirement.&nbsp;</p> <p>The thing is that you need to do a client side XOR encyption, and decrypt again serverside. There are debates over the &quot;visible&quot; client side encryption, however, something is better than nothing.&nbsp;<img src="http://forums.asp.net/scripts/tiny_mce/plugins/emotions/img/smiley-smile.gif" alt="Smile" title="Smile" border="0" class="emoticon"></p> <p>js code:</p> <pre class="prettyprint">password = <span>document.getElementById('txtPassword').value;</span></pre> <pre class="prettyprint">var xorKey = 129; /// you can have other numeric values also.</pre> <pre class="prettyprint"> var result = ""; for (i = 0; i &lt; password.length; ++i) { result += String.fromCharCode(xorKey ^ password.charCodeAt(i)); }</pre> <p>send this to the server by available means (form submit / query string / handler / request.form)</p> <p></p> <p>now @ server side (I had used c# code)</p> <pre class="prettyprint">public bool Authenticate(string userName, string password) { byte result = 0; StringBuilder inSb = new StringBuilder(password); StringBuilder outSb = new StringBuilder(password.Length); char c; for (int i = 0; i &lt; password.Length; i++) { c = inSb[i]; c = (char)(c ^ 129); /// remember to use the same XORkey value you used in javascript outSb.Append(c); } password = outSb.ToString(); // your rest of code }</pre> <p>This I have tested and found to work flawlessly in my project too..</p> <p>The good thing is that the data is &quot;safe&quot; from being seen even if Fiddler is used.</p> <p>Happy coding.</p> <p>Links to refer:&nbsp;<a href="http://th.atguy.com/mycode/xor_js_encryption/">http://th.atguy.com/mycode/xor_js_encryption/</a><br> <a href="http://www.eggheadcafe.com/tutorials/csharp/8b53894c-a889-4914-8c46-122980cc44ae/simple-xor-encryption.aspx">http://www.eggheadcafe.com/tutorials/csharp/8b53894c-a889-4914-8c46-122980cc44ae/simple-xor-encryption.aspx</a></p> 2012-04-20T13:48:13-04:00