简体   繁体   English

Nashorn中的编码/解码Javascript

[英]Encoding/Decoding Javascript in Nashorn

I use the technique described here: http://scriptasylum.com/tutorials/encode-decode.html 我使用此处描述的技术: http : //scriptasylum.com/tutorials/encode-decode.html

In a nutshell, one has a javascript file that looks like this, where the actual javascript is encoded: 简而言之,有一个如下所示的javascript文件,其中实际的javascript编码如下:

document.write( unescape( 'escaped string' ) ); dF('encoded javascript');

I now want to run that same .js module under Nashorn, but Nashorn does not have a document object. 我现在想在Nashorn下运行相同的.js模块,但是Nashorn没有文档对象。 Therefore, I can not do document.write(). 因此,我不能执行document.write()。

Note: It is well known that this technique is easily bypassed and people modest technical ability can still look at the actual code. 注意:众所周知,此技术很容易被绕开,人们只有中等技术才能仍然可以查看实际代码。 My use case does not require strong security so that is not a concern. 我的用例不需要强大的安全性,因此不必担心。 That said, Please consider answers on why one should not do this as off topic. 就是说,请考虑有关为什么不应该将此作为主题的答案。 Thanks. 谢谢。

Basically, that code breaks down into two parts: 基本上,该代码分为两部分:

  1. Un-obfuscate the string via unescape . 通过unescape混淆字符串。

  2. Write the string out via document.write. 通过document.write.将字符串写出document.write.

It sounds like you want to use the string directly for some reason. 听起来您出于某种原因想直接使用该字符串。

You have at least two options: 您至少有两个选择:

  1. You can provide a document object to the scripting engine with a write method that accepts a string. 您可以使用接受字符串的write方法将document对象提供给脚本引擎。 Then you can do with it what you like. 然后,您可以随心所欲地使用它。 (Or substitute document.write before evaluating the string with any function you want called.) (或者在使用您要调用的任何函数评估字符串之前,用document.write代替。)

  2. Remove the document.write( and the corresponding ) at the end and have the engine evaluate the string and hand it to you directly as the result of ScriptEngine#eval . 最后删除document.write(和相应的) ,让引擎评估该字符串并将其作为ScriptEngine#eval的结果直接交给您。

Either way, you'll end up with a string that you can then do something with. 无论哪种方式,您都将得到一个字符串,然后可以使用它进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM