JavaScript Magic Tricks: try & catch encryption

javascript-magic-tricks:-try-&-catch-encryption

This article shares a unique encryption and decryption method for JavaScript code.

Technical Principles

Encrypt JavaScript code, and then “try” to decrypt it in the try-catch error handling syntax. If the decryption is successful and the code can be executed, it means the decryption is successful. If the code cannot be executed, it means the decryption has failed, and an error will be thrown which can be caught by the catch to attempt decryption again.

The following code implements JavaScript encryption.

var source_string ='alert("JShaman Javscript Obfuscator");';
var encoded_string = "";
function encode(){
  for(var i=0;i

Execute output

Image description

The following code implements decryption.

var encode_key = 0;
var encoded_string = "hel{}!+CZahdhg)Chzj{`y})Fko|zjh}f{+ 2";
var decoded_string = encoded_string;
function decode(){
  try{
    console.log("Execute:",decoded_string);
    eval(decoded_string);
  }catch(e){
    encode_key += 1;
    decoded_string ="";
    for(var i=0; i

Explanation: Assuming that the key is unknown, the code in the try section will attempt to decrypt an incorrect ciphertext, which naturally cannot be executed. The program flow will then be caught by the catch and the key will be modified to attempt decryption again. This process will continue indefinitely until the correct key is found. In this demonstration, a simple XOR encryption is used. If a more complex encryption algorithm with a less guessable key is used, the encryption effectiveness of this method will be very good.

Execute output

Image description

Precautions

This is a novel encryption method, where the encrypted code can be used independently. Without the key, if analyzed using a general reverse method, it will result in countless decryption results, which can seriously interfere with the analyst. However, there are certain limitations to using this method: because decryption relies on eval execution, the encrypted statement needs to be executable, such as wrapped in statements like alert, console.log, eval, etc.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
prerequisite-of-the-application

PREREQUISITE OF THE APPLICATION

Next Post
build-an-event-driven-uptime-monitor-in-go-

Build an Event-Driven Uptime Monitor in Go 🚀

Related Posts