7 methods of JavaScript encryption and decryption

Source: Internet
Author: User
This article describes seven JavaScript encryption methods:

During webpage creation (in fact, webpage Trojans), the most worrying thing is the JavaScript code running on the client IE that I wrote so hard.CodeIt is often easily copied by others, which makes your mind a little uncomfortable. You need to know that you are tired of writing something... ^ * ^

However, we should also be aware that because JavaScript code is interpreted and executed in IE, it is impossible to keep it confidential. What we need to do is to increase the difficulty of copying by the publisher as much as possible, let him know how to leave (hopefully ~! ~), Next, I will discuss the encryption and decryption technology of JavaScript code on the webpage based on my practice over the past few years and my personal research experience.

The following JavaScript code is encrypted as an example:

 
<Script Language ="JavaScript">Alert ("Hacker line");</Script>

I. Simplest encryption and decryption

You must have a good understanding of the JavaScript Functions escape () and Unescape () (many web pages use them for encryption), namely encoding and decoding strings. For example, the example code uses escape () the function is encrypted in the following format:

 
Alert % 28% 22% u9ed1 % u5ba2 % u9632 % u7ebf % 22% 29% 3B

How? Do you still understand? Of course, the ASCII character "alert" is not encrypted. If you want to, you can write JavaScript code to re-encrypt it as follows:

 
% 61% 6C % 65% 72% 74% 28% u9ed1 % u5ba2 % u9632 % u7ebf % 22% 3B

Haha! How? This time it is completely encrypted!

Of course, the encrypted code cannot be run directly. Fortunately, Eval (codestring) is available. This function is used to check and execute JavaScript code, the required codestring parameter is a string value that contains valid JavaScript code, and the preceding decoding Unescape () is added. The encrypted result is as follows:

<Script Language ="JavaScript">VaRCode = Unescape ("% 61% 6C % 65% 72% 74% 28% u9ed1 % u5ba2 % u9632 % u7ebf % 22% 3B"); EVAL (CODE)</Script>

Is it easy? Don't be happy. decryption is as simple as it is. The decryption code is put to others (Unescape ())! Haha

Ii. Use the Escape Character "\"

You may not be familiar with the Escape Character "", but some special characters such as \ n (line feed), \ r (Press ENTER), and \ '(single quotes) are provided for JavaScript) should I know something about it? In fact, "\" can be followed by octal or hexadecimal numbers. For example, the character "A" can be expressed: "\ 141" or "\ x61" (note the lowercase character "X "), for double-byte characters such as Chinese characters "", it can only be expressed as "\ u9ed1" in hexadecimal notation (note that it is a lowercase character "U "), the character "U" indicates a dubyte character. According to this example, the code can be expressed:

The octal escape string is as follows:

<Script Language ="JavaScript">Eval ("\ 141 \ 154 \ 145 \ 162 \ 164 \ 50 \ 42 \ u9ed1 \ u5ba2 \ u9632 \ u7ebf \ 42 \ 51 \ 73")</Script>

The hexadecimal escape string is as follows:

 
<Script Language ="JavaScript">Eval ("X61x6cx65x72x74x28x22u9ed1u5ba2u9632u7ebfx22x29x3b")</Script>

There is no decoding function this time, because JavaScript will convert itself during execution. The decoding is also very simple as follows:

<Script Language ="JavaScript">Alert ("X61x6cx65x72x74x28x22u9ed1u5ba2u9632u7ebfx22x29x3b")</Script>

The displayed dialog box shows the decrypted result!

Iii. coding using the script encoder produced by Microsoft

The use of the tool is not described much! I used JavaScript to call the scripting. encoder code of the control! The Code is as follows:

<Script Language ="JavaScript">VaRSENC =NewActivexobject ("Scripting. encoder");VaRCode = '<script language ="JavaScript"> Rnalert ("Hacker line"); Rn</Script>'; VarEncode =SENC. Encodescriptfile (". htm", code, 0, ""); alert (encode );</Script>

The encoded result is as follows:

<Script Language ="Jscript. encode">#@~ ^Fgaaaa ===##@ & Lsdd' J black line of defense R # P @#@&Fgmaaa == ^ #~ @</Script>

Ugly enough, right? However, the corresponding decryption tools have already been released, and even the decrypted webpages are available! I will not talk about it because it decrypts too much Web Page code! The original decryption code is as follows:

 
<Script Language ="Jscript. encode">Function decode () Alert (decode. tostring ());</Script>

What? Is it simple enough? The principle is that IE will first decode the encoded code before it runs. If we put the encrypted code into a user-defined function such as decode, call the tostring () method for the decode of the custom function to obtain the decoded code!

If you think that the language attribute of the Code obtained through this encoding is JScript. encode, which is easy to recognize, there is also an almost unknown method of window object execScript (). Its prototype is:

 
Window.ExecScript (sexpression, slanguage)

Parameters:

Sexpression: required. String ). Code to be executed.

Slanguage: required. String ). Specifies the language of the code to be executed. The default value is Microsoft JScript.

In use, the previous "window" can be omitted without writing!

Using this code, we can run the encoded javascript code as follows:

<Script Language ="JavaScript">ExecScript ("#@~ ^ Fgaaaa ==##@ & lsdd' J R # P #@ & fgmaaa == #~ @","Jscript. encode")</Script>

You can use method 2 to encode the strings in "" so that "jscript. encode" and encoding signature code "#@~ ^ "No, the effect will be better!

4. Add any NUL null characters (hexadecimal 00 h)

In an accidental experiment, I found that when I add any number of "null characters" to any location on the HTML webpage, ie will normally display the content and execute the JavaScript code normally, when we use a General Editor to view the added "null character", it will display a space or Black Block, making the original code hard to understand, if you use NotePad to view the information, the "null character" will become "space". The encrypted result is as follows: (the "space" displayed indicates "null character ")

<SC ri p t l Ang u a GE ="J a v a s c r I p t">A l er T (" ");</SC r I p t>

How? Is it messy? If you do not know the method, it is difficult to remove the "null character" (00 h!

5. Useless content and line feed space tab Method

In JavaScript code, we can add a large number of useless strings or numbers, as well as useless code and comments, so that the real useful code is not buried in it, add a large number of line breaks, spaces, and tabs in the useful code, and use "" to wrap a normal string, this will make the code hard to understand! The encrypted form is as follows:

<Script Language ="JavaScript">"Xajgxsadffgds"; 1234567890625623216;VaR$ = 0; alert // @ $ % & * () (& (^ % ^// CCTV function //(// Hhsaasajx XC/* Asjgdsgu */"Hacker defense"// Ashjgfgf/* @ # % $ ^ & % $ 96667r45fggbhytjty */// Window);"# @ $ # % @ # 432hu"212351436</Script>

At least if I see such a code, I will not try to analyze it. Where are you?

Vi. Self-write decryption Function Method

This method is similar to the one in version 2. It only writes a function to decrypt the code. Many vbs viruses use this method to encrypt themselves to prevent scanning of signatures! The following is a simple encryption and decryption function I wrote. the encryption code is as follows (see file "encryption .htm" for details "):

 
 

The encrypted result is as follows:

 
O % Cd % D1 % D7 % E6 % 9cj % u9ef3 % ufa73 % uf1d4 % u14f1 % u7ee1kd

The encrypted and decrypted code is as follows:

 
 
< Script Language = " Javascript " >
Function uncompile (CODE)
{
Code=Unescape (CODE );
VaR C=String. fromcharcode (code. charcodeat (0)-Code. Length );
For(VAR I=1; I<Code. length; I++)
ReturnC;
}
Eval (uncompile ( " O % Cd % D1 % D7 % E6 % 9cj % u9ef3 % ufa73 % uf1d4 % u14f1 % u7ee1kd " ));
</ Script >

7. Misuse

Use the try {} catch (e) {} structure to test and decrypt the code. Although this idea is good (haha, boast of yourself), I only give an example because it is not practical. < Script Language = " Javascript " >
VaR A = ' Alert ("hacker line"); ' ;
VaR C = "" ;
For ( VaR I = 0 ; I < A. length; I ++ )
Alert (C );
// The above is the encryption code. Of course, if you use this method, you will not write the encryption
// Now the variable C is the encrypted code.
// The following function T () First assumes that the initial password is 0 and is decrypted and executed,
// In case of an error, add the password to 1 and decrypt it until it runs correctly.
VaR D = C; // Save the encrypted code
VaR B = 0 ; // Assume that the initial password is 0.
T ();
Function T () Catch (E) {
C="";
For(VaRI=0; I<D. length; I++)
B+ =1;
T ();
//SetTimeout ("t ()", 0 );
}
}
</ Script >  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.