最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to execute a code encoded in base64 in Javascript - Stack Overflow

programmeradmin4浏览0评论

I have this code in an html page (JavaScript):

<script>
    var cox,d = 0;
    Console.Log(cox,d);
</script>

Now I encrypted [var cox,d = 0; Console.Log(cox,d);] manually with base64 encode and the result is this: IHZhciBjb3gsZCA9IDA7DQogQ29uc29sZS5Mb2coY294LGQpOw==

I want that this encoded string (the code) could be executed by another JavaScript function in the same page... example:

<script>
    var encoded = "IHZhciBjb3gsZCA9IDA7DQogQ29uc29sZS5Mb2coY294LGQpOw==";
    var decodedString = atob(encoded);
    /* Problem is here */
</script>

I want to execute the JavaScript code (encoded above) in /* Problem is here */ '.

How can I do?

I have this code in an html page (JavaScript):

<script>
    var cox,d = 0;
    Console.Log(cox,d);
</script>

Now I encrypted [var cox,d = 0; Console.Log(cox,d);] manually with base64 encode and the result is this: IHZhciBjb3gsZCA9IDA7DQogQ29uc29sZS5Mb2coY294LGQpOw==

I want that this encoded string (the code) could be executed by another JavaScript function in the same page... example:

<script>
    var encoded = "IHZhciBjb3gsZCA9IDA7DQogQ29uc29sZS5Mb2coY294LGQpOw==";
    var decodedString = atob(encoded);
    /* Problem is here */
</script>

I want to execute the JavaScript code (encoded above) in /* Problem is here */ '.

How can I do?

Share Improve this question edited Feb 5, 2018 at 15:19 Rafik Tighilt 2,1011 gold badge16 silver badges27 bronze badges asked Feb 5, 2018 at 14:22 ProgrammerProgrammer 1011 silver badge9 bronze badges 3
  • 2 Just being pedantic, encoding with BASE64 and encrypting is not the same thing at all. I think you want to use eval - but that is a slippery slope :) – Morten Jensen Commented Feb 5, 2018 at 14:27
  • 2 You know that Console != console – Jonas Wilms Commented Feb 5, 2018 at 14:39
  • my pc changed it from console to Console automatically because I coded it on stack overflow not on an editor first.... – Programmer Commented Feb 5, 2018 at 19:11
Add a ment  | 

1 Answer 1

Reset to default 6

You can use the eval function (it is not remended), but it solves your problem.

/* var cox,d = 0; console.log(cox,d); */
var encoded = "dmFyIGNveCxkID0gMDsgY29uc29sZS5sb2coY294LGQpOw==";
var decodedString = atob(encoded);


eval(decodedString);

Another way of doing it, is using the New Function constructor

/* var cox,d = 0; console.log(cox,d); */
var encoded = "dmFyIGNveCxkID0gMDsgY29uc29sZS5sb2coY294LGQpOw==";
var decodedString = atob(encoded);

var func = new Function(decodedString);
func();

*When encodeding your code, Console and Log must be lowercase and not ucfirst.

I am not a security expert, and I truely advice you to check the risks of evaluating and executing this kind of functions in your apps.

Here is an interesting answer about the difference between eval and new Function

发布评论

评论列表(0)

  1. 暂无评论