Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jul 09, 2012 08:58 AM by hitesh.gawhade
Member
54 Points
103 Posts
May 30, 2012 10:09 AM|LINK
<a href="mypost.aspx?email=<%=Membership.Getuser.Email%>">my post</a>
i want to encrypt the url so the end uer will not see something like
mypost.aspx?email=myname@mymail.com
but see the encrypted url
Star
8978 Points
1660 Posts
May 30, 2012 10:17 AM|LINK
use that class that encrypt querystrings URL ..
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 += new EventHandler(context_BeginRequest); } #endregion private const string PARAMETER_NAME = "enc="; private const string ENCRYPTION_KEY = "key"; 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("aspx") && context.Request.RawUrl.Contains("?")) { 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 == "GET") { // 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 + encryptedQuery, false); } } } catch (Exception ex) { // m_Logger.Error("An error occurred while parsing the query string in the URL: " + path, ex); context.Response.Redirect("~/index.aspx"); } } /// /// 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("?")); path = path.Substring(path.LastIndexOf("/") + 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("?") + 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 "?" + PARAMETER_NAME + 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 }
Also go though these Links
Good luck`
9250 Points
1578 Posts
May 30, 2012 10:21 AM|LINK
This might help
http://makhaai.blogspot.co.uk/2010/04/encrypt-and-decript-string-aspnet.html
Or if you have a google there are various simply encrypt/decrypt functions out there
http://www.joshrharrison.com/archive/2009/01/28/c-encryption.aspx
Participant
882 Points
222 Posts
May 30, 2012 10:36 AM|LINK
Hi,
please check here http://social.technet.microsoft.com/wiki/contents/articles/5264.aspx
Contributor
5171 Points
833 Posts
May 30, 2012 10:57 AM|LINK
Try this
1) Add the below code in ur code behind
const string passphrase = "password"; public static string EncryptData(string Message) { byte[] Results; System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding(); MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider(); byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(passphrase)); TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider(); TDESAlgorithm.Key = TDESKey; TDESAlgorithm.Mode = CipherMode.ECB; TDESAlgorithm.Padding = PaddingMode.PKCS7; byte[] DataToEncrypt = UTF8.GetBytes(Message); try { ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor(); Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length); } finally { TDESAlgorithm.Clear(); HashProvider.Clear(); } return Convert.ToBase64String(Results); } public static string DecryptString(string Message) { byte[] Results; System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding(); MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider(); byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(passphrase)); TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider(); TDESAlgorithm.Key = TDESKey; TDESAlgorithm.Mode = CipherMode.ECB; TDESAlgorithm.Padding = PaddingMode.PKCS7; byte[] DataToDecrypt = Convert.FromBase64String(Message); try { ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor(); Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length); } finally { TDESAlgorithm.Clear(); HashProvider.Clear(); } return UTF8.GetString(Results); }
2) Alter ur <a> as below
<a href="mypost.aspx?email=<%=EncryptData(Membership.Getuser.Email)%>">my post</a>
3) In destination page (mypost.aspx), decrypt ur query string (email) as below
protected void Page_Load(object sender, EventArgs e) { var email = WebForm1.DecryptString(Request.QueryString[0]); //Ur code }
163 Points
83 Posts
Jul 09, 2012 08:58 AM|LINK
Thnx it has worked for me. I was looking exactly for this.
misteraddi
Member
54 Points
103 Posts
encrypt url
May 30, 2012 10:09 AM|LINK
i want to encrypt the url so the end uer will not see something like
mypost.aspx?email=myname@mymail.com
but see the encrypted url
MahadTECH
Star
8978 Points
1660 Posts
Re: encrypt url
May 30, 2012 10:17 AM|LINK
use that class that encrypt querystrings URL ..
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 += new EventHandler(context_BeginRequest); } #endregion private const string PARAMETER_NAME = "enc="; private const string ENCRYPTION_KEY = "key"; 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("aspx") && context.Request.RawUrl.Contains("?")) { 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 == "GET") { // 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 + encryptedQuery, false); } } } catch (Exception ex) { // m_Logger.Error("An error occurred while parsing the query string in the URL: " + path, ex); context.Response.Redirect("~/index.aspx"); } } /// /// 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("?")); path = path.Substring(path.LastIndexOf("/") + 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("?") + 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 "?" + PARAMETER_NAME + 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 }Also go though these Links
Good luck`
Mahad Bin Mukhtar
Remember to Mark the replies as Answers
The easiest day was 'yesterday'.
MCP, MCSD
For .NET TECH Blog
AidyF
Star
9250 Points
1578 Posts
Re: encrypt url
May 30, 2012 10:21 AM|LINK
This might help
http://makhaai.blogspot.co.uk/2010/04/encrypt-and-decript-string-aspnet.html
Or if you have a google there are various simply encrypt/decrypt functions out there
http://www.joshrharrison.com/archive/2009/01/28/c-encryption.aspx
P_IT
Participant
882 Points
222 Posts
Re: encrypt url
May 30, 2012 10:36 AM|LINK
Hi,
please check here http://social.technet.microsoft.com/wiki/contents/articles/5264.aspx
P_IT
Ramesh T
Contributor
5171 Points
833 Posts
Re: encrypt url
May 30, 2012 10:57 AM|LINK
Try this
1) Add the below code in ur code behind
const string passphrase = "password"; public static string EncryptData(string Message) { byte[] Results; System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding(); MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider(); byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(passphrase)); TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider(); TDESAlgorithm.Key = TDESKey; TDESAlgorithm.Mode = CipherMode.ECB; TDESAlgorithm.Padding = PaddingMode.PKCS7; byte[] DataToEncrypt = UTF8.GetBytes(Message); try { ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor(); Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length); } finally { TDESAlgorithm.Clear(); HashProvider.Clear(); } return Convert.ToBase64String(Results); } public static string DecryptString(string Message) { byte[] Results; System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding(); MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider(); byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(passphrase)); TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider(); TDESAlgorithm.Key = TDESKey; TDESAlgorithm.Mode = CipherMode.ECB; TDESAlgorithm.Padding = PaddingMode.PKCS7; byte[] DataToDecrypt = Convert.FromBase64String(Message); try { ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor(); Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length); } finally { TDESAlgorithm.Clear(); HashProvider.Clear(); } return UTF8.GetString(Results); }2) Alter ur <a> as below
3) In destination page (mypost.aspx), decrypt ur query string (email) as below
protected void Page_Load(object sender, EventArgs e) { var email = WebForm1.DecryptString(Request.QueryString[0]); //Ur code }hitesh.gawha...
Member
163 Points
83 Posts
Re: encrypt url
Jul 09, 2012 08:58 AM|LINK
Thnx it has worked for me. I was looking exactly for this.