Wenxin Yiyan releases internal testing, how to use js encryption and decryption to protect it

Baidu has developed an online famous quote generator called "Wen Xin Yi Yan". To protect the security of the generated algorithm, they used JS obfuscation and encryption to hide the algorithm. In this article, we'll explore how to use JS obfuscation and encryption to protect your JavaScript code.

The basic principle of JS obfuscation is to make the code incomprehensible so that an attacker cannot easily understand and modify it. Here we use an example to illustrate how to obfuscate a simple JavaScript function:

javascriptCopy codefunction add(a, b) {
    
    
  return a + b;
}

We can use a JS obfuscator, such as UglifyJS, to obfuscate this function into the following code:

javascriptCopy code
function n(a,n){
    
    return a+n}

As you can see, the obfuscator has changed both parameter names and function names to single characters. Although the code is harder to read, the functionality of the functions is still the same.

Next, we can use encryption algorithms to further secure our code. We can encrypt our JavaScript code using Base64 encoding or AES encryption algorithm. Here, we will use the AES encryption algorithm.

First, we need a JavaScript library, such as CryptoJS, to implement the AES encryption algorithm. In our example, we will use the CryptoJS library to encrypt and decrypt the code.

javascriptCopy code
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>

Next, we'll write a simple function to encrypt and decrypt our JavaScript code:

javascriptCopy codefunction encrypt(code, key) {
    
    
  return CryptoJS.AES.encrypt(code, key).toString();
}

function decrypt(ciphertext, key) {
    
    
  return CryptoJS.AES.decrypt(ciphertext, key).toString(CryptoJS.enc.Utf8);
}

Here, we use the AES encryption and decryption functions of the CryptoJS library to encrypt and decrypt the code.

jsjiami.com

If you have different views or questions about the content of the article, please leave a message in the comment area, or send me a private message.

You can also go to the website above, there is my contact information at the bottom to discuss in detail

If your own source code is encrypted and there is no backup, you can find us to solve the problem of recovering the source code, any encryption is fine.

Guess you like

Origin blog.csdn.net/mxd01848/article/details/129719068