Click here to Skip to main content
15,922,015 members
Articles / Web Development / ASP.NET

Simple HTML Scrambler

Rate me:
Please Sign up or sign in to vote.
4.60/5 (4 votes)
19 Dec 2007CPOL 47.4K   320   24   3
A server control for scrambling HTML code/

Screenshot -

Introduction

This server-control was developed for hiding mail-links. If you put your email-address in a mailto-link, you will receive spam-mail, but you can use simple JavaScript to avoid this. This server-control can wrap your HTML and scramble it with simple JavaScript. The control inherits from a Literal control which is used for plain-text rendering.

Using the code

HTMLScrambler is easy to use. Just put the control around your HTML, and it's ready to go.

ASP.NET
<%@ Register Assembly="Serverside.HTMLScrambler" 
    Namespace="Serverside.HTMLScrambler" TagPrefix="uc1" %>

......

<uc1:HTMLScrambler runat="server" id="HTMLScrambler1" Mode="HTML">
  this text/html will be scrambled with HTML.
</uc1:HTMLScrambler>

<hr />

<uc1:HTMLScrambler runat="server" id="HTMLScrambler2" Mode="Javascript">
  this text/html will be scrambled with Javascript.
</uc1:HTMLScrambler>

The control supports two ways to scramble: JavaScript or HTML. It's basically four small functions to get this working.

C#
// Used for javascript scramble
private string GetHex(string character)
{
    String[] hex = { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", 
         "0A", "0B", "0C", "0D", "0E", "0F", "10", "11", "12", "13", 
         "14", "15", "16", "17", "18", "19", "1A", "1B", "1C", "1D", 
         "1E", "1F", "20", "21", "22", "23", "24", "25", "26", "27", 
         "28", "29", "2A", "2B", "2C", "2D", "2E", "2F", "30", "31", 
         "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B", 
         "3C", "3D", "3E", "3F", "40", "41", "42", "43", "44", "45", 
         "46", "47", "48", "49", "4A", "4B", "4C", "4D", "4E", "4F", 
         "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", 
         "5A", "5B", "5C", "5D", "5E", "5F", "60", "61", "62", "63", 
         "64", "65", "66", "67", "68", "69", "6A", "6B", "6C", "6D", 
         "6E", "6F", "70", "71", "72", "73", "74", "75", "76", "77", 
         "78", "79", "7A", "7B", "7C", "7D", "7E", "7F", "80", "81", 
         "82", "83", "84", "85", "86", "87", "88", "89", "8A", "8B", 
         "8C", "8D", "8E", "8F", "90", "91", "92", "93", "94", "95", 
         "96", "97", "98", "99", "9A", "9B", "9C", "9D", "9E", "9F", 
         "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", 
         "AA", "AB", "AC", "AD", "AE", "AF", "B0", "B1", "B2", "B3", 
         "B4", "B5", "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", 
         "BE", "BF", "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", 
         "C8", "C9", "CA", "CB", "CC", "CD", "CE", "CF", "D0", "D1", 
         "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DA", "DB", 
         "DC", "DD", "DE", "DF", "E0", "E1", "E2", "E3", "E4", "E5", 
         "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF", 
         "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", 
         "FA", "FB", "FC", "FD", "FE", "FF" };
    char[] chrBuffer = { Convert.ToChar(character) };
    byte[] bytBuffer = Encoding.Default.GetBytes(chrBuffer);
    int chrIndex = (int)bytBuffer[0];
    return hex[chrIndex];
}


// Used for html-scramble
private string GetCharcode(string character)
{
    char[] chrBuffer = { Convert.ToChar(character) };
    byte[] bytBuffer = Encoding.Default.GetBytes(chrBuffer);
    int chrIndex = (int)bytBuffer[0];
    return "&#" + chrIndex + ";";
}

The scramble-function:

C#
private string Scramble(string HTML)
{
    try
    {
        StringBuilder js = new StringBuilder();
        if (Mode == ScrambleMode.Javascript)
        {
            js.Append("<script type=\"text/javascript\">");
            js.Append("var i,y,x=");
            js.Append("\"");
            for (int i = 0; i < HTML.Length; i++)
            {
                js.Append(this.GetHex(HTML.Substring(i, 1)));
            }
            js.Append("\";");
            js.Append("y='';");
            js.Append("for(i=0;i<x.length;i+=2)");
            js.Append("{");
            js.Append("y+=unescape('%'+x.substr(i,2));");
            js.Append("}");
            js.Append("document.write(y);");
            js.Append("</script>");
        }
        else if (Mode == ScrambleMode.HTML)
        {
            for (int i = 0; i < HTML.Length; i++)
            {
                js.Append(this.GetCharcode(HTML.Substring(i, 1)));
            }
        }
        return js.ToString();
    }
    catch
    {
        return HTML;
    }
}

The control needs a Render method which scrambles all the text in the control.

C#
protected override void Render(HtmlTextWriter writer)
{
    writer.Write(Scramble(this.Text.Trim()));
}

Enums and a rpoperty for making it easier to use in code

C#
private ScrambleMode _mode = ScrambleMode.Javascript;
// Default is javascript

public new ScrambleMode Mode
{
    get { return _mode; }
    set { _mode = value; }
}

public enum ScrambleMode
{
    HTML = 1,
    Javascript = 2
}

And, that's it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Aurum AS
Norway Norway
Microsoft Certified Solutions Developer (MCSD)

Personal website:
stian.net

My projects:
CRM1.no - A free to use norwegian crm software
Fakturax - A free to use norwegian invoice software
Timeføring.no - A free to use norwegian timereg software
MittUtlegg - A free to use norwegian software for receipts
SupportWeb - A free to use norwegian software customersupport

Comments and Discussions

 
GeneralNice control - minor improvements Pin
Michael B. Hansen19-Dec-07 22:15
Michael B. Hansen19-Dec-07 22:15 
Hi,

First of all - nice control, and thank you for releasing it under CPOL Smile | :)

Next, I have made some minor improvements to you code. First of all I have changed the way you extract the individual chars in the HTML to be encoded. There's no need for using "HTML.Substring" when you just can use the index'er on the string instead (required your GetHex() to be parsed a char instead of a string).

Next, I have encapsulated the actual JavaScript encoding in a separat function - which is just called for each string. This will minimize the HTML output of the page.

The modified code is as follows:

<br />
public sealed class HTMLScrambler : System.Web.UI.WebControls.Literal<br />
    {<br />
        private static readonly string[] HEX = { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B", "0C", "0D", "0E", "0F", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1A", "1B", "1C", "1D", "1E", "1F", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B", "3C", "3D", "3E", "3F", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4A", "4B", "4C", "4D", "4E", "4F", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "5D", "5E", "5F", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6A", "6B", "6C", "6D", "6E", "6F", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7A", "7B", "7C", "7D", "7E", "7F", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8A", "8B", "8C", "8D", "8E", "8F", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9A", "9B", "9C", "9D", "9E", "9F", "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "AA", "AB", "AC", "AD", "AE", "AF", "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF", "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB", "CC", "CD", "CE", "CF", "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF", "E0", "E1", "E2", "E3", "E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF", "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "FA", "FB", "FC", "FD", "FE", "FF" };<br />
        private string _output = null;<br />
<br />
        public enum ScrambleMode<br />
        {<br />
            HTML = 1,<br />
            Javascript = 2<br />
        }<br />
<br />
        private ScrambleMode _mode = ScrambleMode.Javascript;<br />
<br />
        public new ScrambleMode Mode<br />
        {<br />
            get { return _mode; }<br />
            set { _mode = value; }<br />
        }<br />
<br />
        protected override void OnPreRender(EventArgs e)<br />
        {<br />
            base.OnPreRender(e);<br />
<br />
            _output = Scramble(this.Text.Trim());<br />
        }<br />
<br />
        protected override void Render(HtmlTextWriter writer)<br />
        {<br />
            writer.Write(_output);<br />
        }<br />
<br />
        private string Scramble(string HTML)<br />
        {<br />
            try<br />
            {<br />
                StringBuilder js = new StringBuilder(HTML.Length*5);<br />
                if (Mode == ScrambleMode.Javascript)<br />
                {<br />
                    const string JS_Scramble = "JS_Scramble";<br />
<br />
                    if (!this.Page.ClientScript.IsClientScriptBlockRegistered(this.Page.GetType(), JS_Scramble))<br />
                    {<br />
                        const string JS_ScrambleScript =    "<script type=\"text/javascript\">"+<br />
                                                            "function JSScramble(data){"+<br />
                                                            "var i,y;"+<br />
                                                            "y='';"+<br />
                                                            "for(i=0;i<data.length;i+=2)"+<br />
                                                            "y+=unescape('%'+data.substr(i,2));"+<br />
                                                            "return y;}"+<br />
                                                            "</script>";<br />
<br />
                        this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), JS_Scramble, JS_ScrambleScript);<br />
                    }<br />
<br />
                    js.Append("<script type=\"text/javascript\">");<br />
                    js.Append("document.write(JSScramble('");<br />
                    for (int i = 0, len = HTML.Length; i != len; ++i)<br />
                    {<br />
                        js.Append(GetHex(HTML[i]));<br />
                    }<br />
                    js.Append("'));");<br />
                    js.Append("</script>");<br />
                }<br />
                else /*if (Mode == ScrambleMode.HTML)*/ //implied<br />
                {<br />
                    for (int i = 0, len=HTML.Length; i!=len; ++i)<br />
                    {<br />
                        js.Append(GetCharcode(HTML[i]));<br />
                    }<br />
                }<br />
                return js.ToString();<br />
            }<br />
            catch<br />
            {<br />
                return HTML;<br />
            }<br />
        }<br />
<br />
        private static string GetHex(char character)<br />
        {<br />
            char[] chrBuffer = { character };<br />
            byte[] bytBuffer = Encoding.Default.GetBytes(chrBuffer);<br />
            return HEX[(int)bytBuffer[0]];<br />
        }<br />
<br />
        private static string GetCharcode(char character)<br />
        {<br />
            char[] chrBuffer = { character };<br />
            byte[] bytBuffer = Encoding.Default.GetBytes(chrBuffer);<br />
            return "&#" + (int)bytBuffer[0] + ";";<br />
        }<br />
}<br />



Ones again, thank you for the code Big Grin | :-D

/Michael
AnswerRe: Nice control - minor improvements Pin
StianSandberg20-Dec-07 2:47
StianSandberg20-Dec-07 2:47 
GeneralRe: Nice control - minor improvements Pin
Andrew Rissing5-Nov-09 8:15
Andrew Rissing5-Nov-09 8:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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