Base64 加密后运行 JavaScript 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15850727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:19:06  来源:igfitidea点击:

Run JavaScript Code after Base64 encryption

javascriptfunctionbase64

提问by user2252109

I have encrypted my JavaScript Code with the base64 method like shown here. But when I run the encode function with document.ready, the JavaScript function doesn't do what it should.

我有我的加密的JavaScript代码所示一样以base64方法在这里。但是当我使用 document.ready 运行 encode 函数时,JavaScript 函数没有做它应该做的事情。

How do I realize it, that the encrypted code will be decrypted and start to rund the function?

我如何意识到加密的代码将被解密并开始运行该功能?

Thank you in advance! :)

先感谢您!:)

回答by Guffa

Just use evalon the decoded string.

只需eval在解码后的字符串上使用。

Example using the code alert("test");:

使用代码示例alert("test");

eval(decode64('YWxlcnQlMjglMjJ0ZXN0JTIyJTI5JTNC'));

Demo: http://jsfiddle.net/uFxnz/

演示:http: //jsfiddle.net/uFxnz/

Note: Using the evalfunction is associated with unsafe and inefficient code, but in this case you have as much control over where the code is coming from as you can, so it's doing just what you want it to do.

注意:使用该eval函数与不安全和低效的代码相关联,但在这种情况下,您可以尽可能多地控制代码的来源,因此它会做您希望它做的事情。

回答by Adam

You can't run your JS code through a base64 converter and expect it to function.

你不能通过 base64 转换器运行你的 JS 代码并期望它运行。

What you are looking for is a minifier. Like uglifyjs:

您正在寻找的是一个缩小器。像 uglifyjs:

http://marijnhaverbeke.nl/uglifyjs

http://marijnhaverbeke.nl/uglifyjs

回答by Qwerty

To "protect" your code...

要“保护”您的代码...

..you can use this simple procedure.

..你可以使用这个简单的程序。

Let's say this is your code:

假设这是您的代码:

1. The Code

1. 守则

// commentary, Yaaay!
var myObj = {
    foo : function() { alert("Baz!")} ,
    html : "<div id='myDIV'></div>", // it's ["'] proof
    bar : 123 
};
myObj.foo();

2. The base64 string

2.base64字符串

1. btoa((function(){
2. // commentary, Yaaay!
3. var myObj = {
4.     foo : function() { alert("Baz!")} ,
5.     html : "<div id='myDIV'></div>", // it's ["'] proof
6.     bar : 123 
7. };
8. myObj.foo();
9. }).toString().slice(13,-2))

Notice, that I've only added lines 1and 9, the rest is your code, intact.

请注意,我只添加了行19,其余的是您的代码,完好无损。

3. Run it

3.运行它

eval(atob(base64Fun));

eval(atob("Ly8gY29tbWVudGFyeQp2YXIgbXlPYmogPSB7Cglmb28gOiBmdW5jdGlvbigpIHsgYWxlcnQoIkJheiEiKX0gLAoJaHRtbCA6ICI8ZGl2IGlkPSdteURJVic+PC9kaXY+IiwKCWJhciA6IDEyMyAKfTsKbXlPYmouZm9vKCk7"));

Compatibility

兼容性

btoa()and atob()are not supported in all browsers, but..

btoa()并且atob()并非在所有浏览器中都受支持,但是..

  1. use a polyfil and teach the browser these functions. link
  2. find another way
  1. 使用 polyfil 并教浏览器这些功能。关联
  2. 寻找另一种方式