encrypt URL datahttp://forums.asp.net/t/1798496.aspx/1?encrypt+URL+dataMon, 30 Apr 2012 20:50:57 -040017984964958351http://forums.asp.net/p/1798496/4958351.aspx/1?encrypt+URL+dataencrypt URL data <p>Hi,</p> <p>I have seen some site hide URL data&nbsp; so instead of showing .aspx?id=5. it shows ?fedjhfuwicisdhnfhtufgojdfkdksudj ( I made it up ) but you got my idea. ...I want to know the significant of this and how to creat it in my website</p> <p><a href="http://localhost:51756/demo/DataListNavMenu.aspx?id=5">http://localhost:51756/demo/DataListNavMenu.aspx?id=5</a></p> <p>&nbsp;</p> <p>Bob</p> 2012-04-30T14:01:00-04:004958374http://forums.asp.net/p/1798496/4958374.aspx/1?Re+encrypt+URL+dataRe: encrypt URL data <p>use that class that encrypt querystrings URL ..</p> <pre class="prettyprint">using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Collections.Generic; using System.IO; using System.Security.Cryptography; using System.Text; /// Summary description for QueryStringModule /// public class QueryStringModule : IHttpModule { // private ILog m_Logger = LogManager.GetLogger(typeof(QueryStringModule)); #region IHttpModule Members public void Dispose() { // Nothing to dispose } public void Init(HttpApplication context) { context.BeginRequest &#43;= new EventHandler(context_BeginRequest); } #endregion private const string PARAMETER_NAME = &quot;enc=&quot;; private const string ENCRYPTION_KEY = &quot;key&quot;; void context_BeginRequest(object sender, EventArgs e) { HttpContext context = HttpContext.Current; string query = string.Empty; string path = string.Empty; try { if (context.Request.Url.OriginalString.Contains(&quot;aspx&quot;) &amp;&amp; context.Request.RawUrl.Contains(&quot;?&quot;)) { query = ExtractQuery(context.Request.RawUrl); path = GetVirtualPath(); if (query.StartsWith(PARAMETER_NAME, StringComparison.OrdinalIgnoreCase)) { // Decrypts the query string and rewrites the path. string rawQuery = query.Replace(PARAMETER_NAME, string.Empty); string decryptedQuery = Decrypt(rawQuery); context.RewritePath(path, string.Empty, decryptedQuery); } else if (context.Request.HttpMethod == &quot;GET&quot;) { // Encrypt the query string and redirects to the encrypted URL. // Remove if you don't want all query strings to be encrypted automatically. string encryptedQuery = Encrypt(query); context.Response.Redirect(path &#43; encryptedQuery, false); } } } catch (Exception ex) { // m_Logger.Error(&quot;An error occurred while parsing the query string in the URL: &quot; &#43; path, ex); context.Response.Redirect(&quot;~/index.aspx&quot;); } } /// /// Parses the current URL and extracts the virtual path without query string. /// /// The virtual path of the current URL. private static string GetVirtualPath() { string path = HttpContext.Current.Request.RawUrl; path = path.Substring(0, path.IndexOf(&quot;?&quot;)); path = path.Substring(path.LastIndexOf(&quot;/&quot;) &#43; 1); return path; } /// /// Parses a URL and returns the query string. /// /// The URL to parse. /// The query string without the question mark. private static string ExtractQuery(string url) { int index = url.IndexOf(&quot;?&quot;) &#43; 1; return url.Substring(index); } #region Encryption/decryption /// /// The salt value used to strengthen the encryption. /// private readonly static byte[] SALT = Encoding.ASCII.GetBytes(ENCRYPTION_KEY.Length.ToString()); /// /// Encrypts any string using the Rijndael algorithm. /// /// The string to encrypt. /// A Base64 encrypted string. private static string Encrypt(string inputText) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); byte[] plainText = Encoding.Unicode.GetBytes(inputText); PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT); using (ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16))) { using (MemoryStream memoryStream = new MemoryStream()) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { cryptoStream.Write(plainText, 0, plainText.Length); cryptoStream.FlushFinalBlock(); return &quot;?&quot; &#43; PARAMETER_NAME &#43; Convert.ToBase64String(memoryStream.ToArray()); } } } } /// /// Decrypts a previously encrypted string. /// /// The encrypted string to decrypt. /// A decrypted string. private static string Decrypt(string inputText) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); byte[] encryptedData = Convert.FromBase64String(inputText); PasswordDeriveBytes secretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT); using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16))) { using (MemoryStream memoryStream = new MemoryStream(encryptedData)) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) { byte[] plainText = new byte[encryptedData.Length]; int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length); return Encoding.Unicode.GetString(plainText, 0, decryptedCount); } } } } #endregion }</pre> <pre class="prettyprint"><span class="pln"><br /><br /></span></pre> 2012-04-30T14:18:49-04:004958915http://forums.asp.net/p/1798496/4958915.aspx/1?Re+encrypt+URL+dataRe: encrypt URL data <p>Thanks Mahmoud, I apprciate your help.&nbsp; can you send me a sample code to use the class.</p> <p>bob</p> 2012-04-30T20:09:19-04:004958922http://forums.asp.net/p/1798496/4958922.aspx/1?Re+encrypt+URL+dataRe: encrypt URL data <p>u welcome Friend&nbsp;<img src="http://forums.asp.net/scripts/tiny_mce/plugins/emotions/img/smiley-smile.gif" alt="Smile" title="Smile" border="0" class="emoticon">&nbsp;.. oh i sent it above ... copy that code at CS File and put it inside ur AppCode Folder .. and then Call the this Class at any Page CS ..&nbsp;</p> 2012-04-30T20:12:15-04:004958943http://forums.asp.net/p/1798496/4958943.aspx/1?Re+encrypt+URL+dataRe: encrypt URL data <p>and add these lines to your web config file&nbsp;</p> <pre class="prettyprint">&lt;system.web&gt; &lt;httpModules&gt; &lt; add type = &quot; QueryStringModule &quot; name = &quot; QueryStringModule &quot; /&gt; &lt;/httpModules&gt; &lt;/system.web&gt;</pre> <p></p> <p></p> 2012-04-30T20:27:51-04:004958962http://forums.asp.net/p/1798496/4958962.aspx/1?Re+encrypt+URL+dataRe: encrypt URL data <p>you can encode data for use in a querystring like this:</p> <pre class="prettyprint">Dim qs As String = &quot;this is a querystring&quot; Dim encodedQs As String = HttpUtility.UrlEncode(qs)</pre> <pre class="prettyprint"><span class="pln"><br /><br /><br /></span></pre> <p><span>&nbsp;&nbsp;</span></p> <p>Its important to be aware though that urlencoding your data does not in any way&nbsp;prevent that data from being tampered with /&nbsp;changed by the user.</p> <p>if you need to use the querystring and want to make sure the data is not tampered with, you might consider the technique in this article:&nbsp;<a href="http://aspnet.4guysfromrolla.com/articles/083105-1.aspx">http://aspnet.4guysfromrolla.com/articles/083105-1.aspx</a></p> <p><a href="http://www.4guysfromrolla.com/webtech/012000-1.shtml">http://www.4guysfromrolla.com/webtech/012000-1.shtml</a></p> <p>The process of encrypting the data will also render the data illegible for your users - effectively hiding its value</p> 2012-04-30T20:50:57-04:00