WorldofASP.NET : ASP.NET Tutorial, Hosting, and Source Code
You are 1 of 65 users

WorldofASP.NET >> ASP.NET >> General ASP.NET

Encrypt Passing Parameter in Url QueryString

Creating a class to encrypt passing value from anonymous user

Published Date : 02 Jun 2008

Author : Hans Candra
Language : C#
Platform : .NET
 
Technology : ASP.NET
Views : 7020
Rating : (1 votes so far)
Email to a Friend | Print this Article | Add to Favourites | Report Error

Introduction

Once upon a time in the tech world, security is very important. This being most true in the early years of the industry, when there were gaping holes in privacy policies and confidential client information. With the new Cryptography classes in .NET, there's absolutely no excuse for not hiding even the most innocuous user data.

Main

In computer programming, a parameter is a variable which takes on the meaning of a corresponding argument passed in a call to a subroutine. A parameter represents a value that the procedure expects you to supply when you call it. The procedure's declaration defines its parameters. You can define a procedure with no parameters, one parameter, or more than one. The part of the procedure definition that specifies the parameters is called the parameter list.

If you're not going to use a session variable for storing end user information, you're most likely going to keep some sort of State by passing the information to a cookie or push it around with GET/POST parameters. If you're passing around any sort of ID or user information like their name, it's better to encrypt the information.

What I was looking for was a quick way to encrypt and decrypt parts of a QueryString - it had to be on the fly and quick.

Example: url with QueryString http://www.weburl.com/product.aspx?id=1234. In here id is a parameter and 1234 is the value of it. Now we want to encrypt 1234 so it cant be recognize by common user.

Class To Encpypt Passing Parameter Value

using System;
using System.Data;
using System.Configuration;
using System.Linq;
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.Xml.Linq;
using System.Security.Cryptography;
using System.Xml;
using System.Text;
using System.IO;

public class Encryption
{
    private static byte[] key = { };
    private static byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
    private static string EncryptionKey = "!5623a#de";
    public Encryption()
    {

    }
    public static string Decrypt(string Input)
    {
        Byte[] inputByteArray = new Byte[Input.Length];
        try
        {
            key = System.Text.Encoding.UTF8.GetBytes
(EncryptionKey.Substring(0, 8));
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            inputByteArray = Convert.FromBase64String(Input);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream
(ms, des.CreateDecryptor(key, IV), <BR>>CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            Encoding encoding = Encoding.UTF8;
            return encoding.GetString(ms.ToArray());

        }
        catch (Exception ex)
        {
            return "";
        }

    }
    public static string Encrypt(string Input)
    {
        try
        {
            key = System.Text.Encoding.UTF8.GetBytes
(EncryptionKey.Substring(0, 8));
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            Byte[] inputByteArray = Encoding.UTF8.GetBytes(Input);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream
(ms, des.CreateEncryptor(key, IV),CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Convert.ToBase64String(ms.ToArray());
        }
        catch (Exception ex)
        {
            return "";
        }


    }
}

Conclusion

When you need to pass the encryption parameter just simple write:
c# backend sytax:
Int value = 1234;
Asp.net url syntax:
http://www.weburl.com/product.aspx?id=<%=Encryption.Encrypt(value)%>

And to Decrypt it, c# backend sytax:
int id = Convert.ToInt32(Encryption.Decrypt(Request.QueryString["id"].Trim()));

References

Include all the useful links or references that can help users learn about this tutorial

  1. How to use ASP.NET to Encrypt Data
  2. ASP.NET Encryption Symplified



          Other Related and Popular Articles :

          Working with Master Pages and Basics of Master Pages
          Why do you need master pages and how it can improve the maintenance of your website
          Tips to Improve Your ASP.NET Web site performance
          The article contains guidelines for improving your ASP.NET Web applications
          Use C# and VB.NET in the same project
          Use C# and VB.NET classes in the same ASP.NET application
          Publish and create RSS Feeds easily on your ASP.NET websites
          This article explains the concept of creating RSS feeds for your ASP.NET website with auto updating contents
          Working with HttpWebRequest and HttpWebResponse in ASP.NET
          This article explain how to use WebRequest and WebResponse in ASP.NET to grab contents and screen scrape
          Sending Email in ASP.NET 1
          This article explain how to send email in ASP.NET 1 either by using SMTP authentication or not

          Author Profile : Hans Candra

          Click here to view Author Profile


          How would you rate the quality of this content?
          Poor Excellent

          Comments

          #An excellent example that actually works
          02 Jul 2008 12:59 by : leon

          Thank you so much. Other DES examples I've found online(including one from MS) omitted the cs.FlushFinalBlock() call in the Decrypt method, resulting in truncated data. I've wasted 4 hours trying to troubleshoot it until I stumbled across your post. Thank you so much!

          Leave New Comments


          Article Content copyright by Hans Candra
          Everything else Copyright © by WorldofASP.NET 2008
           
          Announcements
          Earn Cash by writing an article or review
          For more info Click here







          Legend : - Within 3 Days - Within 6 Days - Within 9 Days

          Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 Link To Us | Contact Us
          © 2002-2008 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
          Our Partners : ASP.NET Web Hosting | Windows Web Hosting | FREE ASP.NET CMS | Phone Card | PHP Directory | Bangkok Hotels |Calling Card