Click here to Skip to main content
6,930,524 members and growing! (27,538 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Simple web based obfuscation

By Sacha Barber

Protect sesitive data from nasty web bots using server / client obfuscation methods
C#2.0, Windows, .NET, ASP.NET, Visual-Studio, Dev
Posted:30 Jan 2007
Views:27,877
Bookmarked:35 times
Unedited contribution
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
20 votes for this article.
Popularity: 5.03 Rating: 3.87 out of 5
1 vote, 5.0%
1
1 vote, 5.0%
2
3 votes, 15.0%
3
4 votes, 20.0%
4
11 votes, 55.0%
5

 

Introduction

A while ago I had to make a web page with lots of sensitive customer details in a GridView on an ASP .NET page. I though this is really bad, what happens if this data get screen scraped by some nasty web bot, that happens to match on email regular expressions. So I had a think and an investigation into MSDN. And have come up with what I think is a nice solution.

The Basic Idea

The System.Text namespace has an ASCII encoding and there is also a System.BitConvertor so I thought about it. And thought actually I could encode the sensitive data using this method and store the ASCII / BitConverted data in the server requested HTML. Then use javascript to reverse this process when the page is 1st loaded. That is what is presented in this article.

Probably the easiest way to get this, is to look at the code.

Code

Its all in the one web form, Default.aspx. Nice and easy

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Simple web encryption</title>
    
<style type="text/css">
body 
{
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
	text-decoration: none;
	position: relative;
	color: #000000;
	margin : 0px;
	overflow-x : hidden;
	width: 100%;


}  
</style>   
<script language="javascript">
		/*
		====================================================================
		This function helps protect the email address from the evil spam-bots
		that scan web pages for useful data such as (email addresses). 
		Instead of using the data directly, the encoded value is stored in the
		html and decoded when required.
		====================================================================
		*/
		function decode(ServerEncoded)
		{
		// The ServerEncoded parameter is a string that contains the encoded data.

		// Each character in the ServerEncoded parameter has been converted into 

		// a two digit number (hex / base16). This function converts the

		// series of numbers back into the normal form and returns the 

		// decoded string to the client


		// holds the decoded string

		var res = "";

		// go through and decode the full input server encoded string

		for (i=0; i < ServerEncoded.length;)
		{
			// holds each letter (2 digits)

			var letter = "";
			letter = ServerEncoded.charAt(i) + ServerEncoded.charAt(i+1)

			// build the real decoded value

			res += String.fromCharCode(parseInt(letter,16));
			i += 2;
		}
		//return the new decoded string to allow it to be rendered to screen

		return res;
		}


        /*
		====================================================================
		This function gets a reference to the server encrypted string and
		then decrypts this using the decode() function and sets the
		txtDecrypted value to the value return by the decode() function
		====================================================================
		*/
		function GetEmailAndDecode() {
		
			//get the table element

			var txtSvrEncr = document.getElementById('txtServerEncrypted');
			var txtJSDecr = document.getElementById('txtDecrypted');
			txtJSDecr.value = decode(txtSvrEncr.value);
			
			var txtAllTog = document.getElementById('txtAllTogether');
			txtAllTog.value = decode(txtAllTog.value);
		}
</script>
    
    
</head>
<body onload="GetEmailAndDecode();">
    <form id="form1" runat="server">
    <div>
        
        <h1>Simple ASP .NET data protection</h1>
        <p>
        This simple web page contains a method for keeping sensitive data (such as emails) safe, by
        employing server side encryption and client side decryption. It could be used for any data
        I am using emails, but it could be applied to anything. You choose.
        <br/>
        <br/>
        We all have emails these days, and this is somepeople main contact detail. As such dont
        we all deserve to have this one detail kept secret from web bots that screen scrape web sites
        looking for emails. This is what is done, its easy just match an email regular expression and
        away you go. They would easily be able to pull out something like myname@hotmail.com
        <br/> 
        <br/> 
        What this piece of codebehind stuff and javascript (in this page) will show, is how to encrypt
        a users email before sending the page to the client. And then use javascript to decrypt it
        back to an email again. The great part is that the actual HTML code doesnt contain the email
        at all, so the nasty web bot cant get the users personal information out of the page. Ha Ha.
        <br/> 
        <br/>      
        <br/> 
        <br/>                    
        <b><asp:Label ID="Label1" runat="server" Text="Normal email (BAD, web bot could grab this)"></asp:Label></b>
        <br/> 
        <br/>              
        <asp:TextBox ID="txtRawEmail" runat="server" Width="357px">myname@hotmail.com</asp:TextBox>
        <br/> 
        <br/> 
        <br/> 
        <br/>             
        <b><asp:Label ID="Label2" runat="server" Text="Server side encrypted email / No javascript decryption...yet"></asp:Label></b>
        <br/> 
        <br/>             
        <asp:TextBox ID="txtServerEncrypted" runat="server" Width="357px"></asp:TextBox>
        <br/> 
        <br/> 
        <br/> 
        <br/>             
        <b><asp:Label ID="Label3" runat="server" Text="Javascript decryption, from Server side encrypted email"></asp:Label></b>
        <br/> 
        <br/>              
        <asp:TextBox ID="txtDecrypted" runat="server" Width="357px"></asp:TextBox>                                    
        <br/> 
        <br/> 
        <br/> 
        <br/>             
        <b><asp:Label ID="Label4" runat="server" Text="Putting it all together. Server side encrypted email / Javascript decryption. Ha Ha web bot"></asp:Label></b>
        <br/> 
        <br/>              
        <asp:TextBox ID="txtAllTogether" runat="server" Width="357px"></asp:TextBox>             
        </p>                  
    </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


/// <summary>

/// This page is a throw away, it is simply used to demonstrate the

/// use of the 2 tools : server side encryption / client side decryption

/// for preserving sensitive data from web bots

/// </summary>

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        #region Email Encryption 
        //if javascript is enabled do the encoding

        if (Request.Browser.JavaScript)
        {
            //do the encryption using the raw email

            txtServerEncrypted.Text = System.BitConverter.ToString(
                            System.Text.ASCIIEncoding.ASCII.GetBytes(
                                    (txtRawEmail.Text))).Replace("-", "");

            //do the encryption using the raw email

            txtAllTogether.Text = System.BitConverter.ToString(
                            System.Text.ASCIIEncoding.ASCII.GetBytes(
                                    (txtRawEmail.Text))).Replace("-", "");
        }
        else
        {
            //couldnt find javascript so just use normal email

            txtServerEncrypted.Text = txtRawEmail.Text;
            txtAllTogether.Text = txtRawEmail.Text;
        }
        #endregion
    }
}

So how does it work

Well what actually happens is very simple. Firstly the server obfuscates the data, then the client side javascript de-obfuscates it. But this means that the sensitve data is never actually within the source document in a format that a web bot can grab.

Lets have a look at the source file.

The value of the field txtAllTogether is just ASCII text. However if we then look at the rendered output for this page. we can see that the data has been de-obfuscated using client side javascript. This will work with any data at all. I just chose email, as its the most obvious form of data that should be kept private.

The attached project contains a single page, which has 4 text fields on it.

  • The raw data
  • The server side ASCII / BitConverted data
  • The Server side ASCII / BitConverted data through java script
  • Putting it all together in one textbox

 

What Do You Think ?

Thats it, I would just like to ask, if you liked the article please vote for it.

 

Conclusion

I have quite enjoyed constructing this article. I hope it helps someone the way it has helped me.

 

History

v1.1 30/01/07 : Changed wording from encryption to obfuscation, Thanks to Jan Seda. He is actually correct. Well done Jan. Thanks

v1.0 30/01/07 : Initial Issue

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Sacha Barber


Member
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Occupation: Software Developer (Senior)
Location: United Kingdom United Kingdom

Other popular ASP.NET articles:

 
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinmemberGUI Developer7:47 21 Dec '08  
QuestionObfuscation of email addresses in normal HTML pages Pinmemberpcug23:54 5 Nov '08  
Hello Sacha,

Although I have ~30 years in this business, all of my experience has been on the administrative and integration areas. I am now about to embark on an intensive self-training project in .ASP, .NET, PHP, C# (or G#, since it is open-source). Self-training, because I have elected to become care-giver to my mom.

My question concerns obfuscating sections of normal HTML contact information listed on any website. No one wants to deal with a site which has no contact info, so is there a way to obfuscate the email address, phone numbers, etc. with your method, or is there a simpler way to hide this info from the robots, but still display it to your visitor?

Thanks for your articles and your input.

Regards from across the pond,
pcug (James Simmons)

P.S. It would be nice to be able to do that for message boards too!
Wonder how much spam ended up in Jan Seda's mailbox.
AnswerRe: Obfuscation of email addresses in normal HTML pages PinmvpSacha Barber2:25 6 Nov '08  
GeneralThank you PinmemberJaECH6:08 11 Jun '08  
GeneralRe: Thank you PinmvpSacha Barber7:15 11 Jun '08  
Questionwhy response raw email when client doesn't support Javascript? Pinmember17:00 7 Feb '07  
AnswerRe: why response raw email when client doesn't support Javascript? PinmemberSacha Barber22:47 7 Feb '07  
Generalhow to open this web based system? Pinmemberzixlea8:06 5 Feb '07  
GeneralRe: how to open this web based system? PinmemberSacha Barber23:57 5 Feb '07  
GeneralNice PinmemberThe .NET Junkie12:01 30 Jan '07  
GeneralRe: Nice PinmemberSacha Barber23:05 30 Jan '07  
GeneralObfuscation PinmemberJan Seda4:52 30 Jan '07  
GeneralRe: Obfuscation PinmemberSacha Barber7:47 30 Jan '07  
GeneralRe: Obfuscation PinmemberJan Seda7:49 30 Jan '07  
GeneralRe: Obfuscation PinmemberSacha Barber8:56 30 Jan '07  
GeneralRe: Obfuscation PinmemberSacha Barber8:48 1 Feb '07  
GeneralRe: Obfuscation PinmemberJan Seda2:36 2 Feb '07  
GeneralRe: Obfuscation PinmemberSacha Barber23:22 2 Feb '07  
GeneralRe: Obfuscation PinmemberSacha Barber22:57 12 Feb '07  
GeneralRe: Obfuscation PinmemberJan Seda6:53 15 Feb '07  
GeneralRe: Obfuscation PinmemberSacha Barber8:02 15 Feb '07  
GeneralRe: Obfuscation PinmemberSacha Barber0:21 1 Mar '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 30 Jan 2007
Editor:
Copyright 2007 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project