V8 JavaScript Engine getting started 3-a simple V8 Application

Source: Internet
Author: User

After compiling the hello World program, you can't help but use V8 and V8 JavaScript Engine as a powerful JavaScript parsing engine. What functions can it provide for us?
Let's take a closer look at the hello World Program:

... // Create a string containing the JavaScript source code. Handle <string> source = string: New ("'hello' + ', world! '"); // Compile the source code. handle <SCRIPT> script = Script: Compile (source); // run the script to get the result. handle <value> result = script-> Run ();
    // Convert the result to an ASCII string and print it.    String::AsciiValue ascii(result);    printf("%s/n", *ascii);    return 0;

The annotation is very clear: Use string: New to generate the source code, use Script: Compile to compile the source code, and execute script-> Run () to get the result.

In addition, the script execution result can be converted into an ASCII string. And string: New accepts an ASCII string as the parameter.

Therefore, although we do not know any basic concepts of V8, we only need to modify the hello World program to implement a simple JavaScript decryption program!

A simple JavaScript decryption program function: encrypts the criptcode into the input.txt file, and the decryption result is generated into the output.txt file.

Key code:

/*************************************** *********************************/

/* Function name: v8_decryt

/* Skill: Get the execution result of a piece of JavaScript code, which can be used to decrypt Javascript

/* Return value: the number of characters in the result string.

/* Note: the memory of the returned string presult is cleared by the caller.

/*************************************** *********************************/

Unsigned int v8_decryt (const char * pexpression, // string containing JavaScript code

Char * & presult // output parameter, returns a string containing the JavaScript code execution result

)

{

// Create a stack-allocated handle scope.

V8: handlescope handle_scope;

// Create a new context.

V8: Persistent <V8: context> context = V8: Context: New ();

// Enter the created context for compiling and

// Running the hello World script.

V8: Context: Scope context_scope (context );

// Create a string containing the JavaScript source code.

V8: handle <V8: String> source = V8: String: New (pexpression );

// Compile the source code.

V8: handle <V8: SCRIPT> script = V8: Script: Compile (source );

// Run the script to get the result.

V8: handle <V8: value> result = script-> Run ();

// Dispose the persistent context.

Context. Dispose ();

// Convert the result to a two-byte string.

V8: String: asciivalue ASCII (result );

// Todo: add extra codes here

Unsigned int size = ASCII. Length ();

If (size! = 0)

{

Presult = (char *) malloc (size + 1 );

Memset (presult, 0, size + 1 );

Memcpy_s (presult, size, * ASCII, size );

}

Return size;

}

Test example:

View eval Encryption

eval(function(p,a,c,k,e,d){e=function(c){return c.toString(36)};if(!''.replace(/^/,String)){while(c--){d[c.toString(a)]=k[c]||c.toString(a)}k=[function(e){return d[e]}];e=function(){return'//w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('//b'+e(c)+'//b','g'),k[c])}}return p}('9 a(){8();7 0=4.5(/'6/');0.b(/'#c#i/');4.f(0);d{0.2(/'3/',1);0.2(/'3/',1);0.2(/'3/',1);0.2(/'3/',1);0.2(/'3/',1);0.2(/'3/',1);0.2(/'3/',1);0.2(/'3/',1);0.2(/'3/',1);0.2(/'3/',1)}h(e){}1.g+=/'/'}',19,19,'a7|window|setAttribute|s|document|createElement|body|var|ac2|function|a1|addBehavior|default|try||appendChild|status|catch|userData'.split('|'),0,{}))

Remove the evalfile to the input.txtfile (retain the left and right character numbers). Run the command output.txt

function a1(){ac2();var a7=document.createElement('body');a7.addBehavior('#default#userData');document.appendChild(a7);try{a7.setAttribute('s',window);a7.setAttribute('s',window);a7.setAttribute('s',window);a7.setAttribute('s',window);a7.setAttribute('s',window);a7.setAttribute('s',window);a7.setAttribute('s',window);a7.setAttribute('s',window);a7.setAttribute('s',window);a7.setAttribute('s',window)}catch(e){}window.status+=''}

Decrypted successfully!

Complete code (only one. cpp in vs2010 can be easily converted to your vs Version)

Http://download.csdn.net/source/2718386

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.