How to Encrypt HTML and JavaScript (and decrypt it too!)

Many programmers and developers try to protect their fruits of their hard work by encrypting the codes they write. You might do this if you are trying to prevent people from copying your JavaScript programs, or trying to obscure email links in your HTML source code. There are many ways to encode your webpages and JS files so that humans cannot easily parse the underlying HTML or JavaScript code.

Keep in mind, however, that anything that can be interpreted by a web browser can ultimately be decoded by a human. HTML and JavaScript encryption will deter an unmotivated thief or someone with limited programing knowlegde, and this is usually good enough for most website owners. But a determined person can exploit a web browser's features to read the real code in its unecrypted form.

Hexadecimal Encryption/Decryption: This is by far the most common method of encrypting JavaScript for free. The concept is simple: replace every character with its 8-bit equivalent. Since 28 = 256, and 256 = 16x16, this means you can replace every character with a two-digit number in base-16, i.e., the digits 0-9 and the letters A-F. The program below will encode and decode HTML and Javascript.


This HTML and JavaScript encryption only works if the source code is confined to standard ASCII characters. For HTML that contains more obscure Unicode characters, you need a more complex encryption system.

Other: Several free and subscription based encryption systems use keys to encrypt the source code, so every encryption will be different depending on the key that is used. These encryption programs can handle symbols outside the ASCII character set, and are complex enough that a human cannot read it. (Hexadecimal encryption, on the other hand, is so common and simple that many programmers can decode it mentally.)

No matter what method you use to obscure your source code, there is always a way to decode it using simple browser manipulations. For example, if you are viewing a web page whose source is encrypted, simply clear the address bar and enter this line of JavaScript and hit enter:

javascript:document.write('<textarea rows=25 cols=125 >'+document.documentElement.innerHTML+'<\/textarea>')

You will then see a box of plain text on the screen. It contains the web page's source code between the html tags, and several lines of decrypted code below the encrypted code. (This works best in Internet Explorer and older browsers.)

Don't pay too much for HTML/JS encryption software, as most encrypted HTML and JavaScript can be easily decrypted with a web browser. If you want to truly protect your source code, write it in a server-side language such as PHP or ASP, rather than a client-side language such as JavaScript.

© Had2Know 2010

How to Fix a Stuck Pixel

Is This MP3 Download Site Legal?

How to Rotate 3 Ad Banners with Variable Frequency

The Most Popular Web Design Programs