导航:首页 > 文件处理 > js混淆压缩工具

js混淆压缩工具

发布时间:2022-05-01 08:33:10

① 怎么使用yuicompressor2.4.2.jar进行js,CSS进行代码混淆

目前开发Web应用javascript发挥的作用越来越大,相关的Javascript框架也比较多。但是有一个问题,我们开发过程中,所有的JS代码都添加了注释,如使用JsDoc,代码的可读性比较强,同时这样的代码也便于调试。但是在产品环境中,我们希望这些JS代码是压缩和混淆过的,这主要是让 JS代码加载的更快,这也是Google AJAX Libraries API出现的原因。YUI Compressor 是一款由 Yahoo 公司开发的、功能非常强大的 JS、CSS 代码混淆和压缩工具,采用Java开发,目前很多Javascript Framework都使用YUI Compressor进行代码分发。

二、使用简介:

命令行下执行 Java 程序,运行 yuicompressor jar 软件包,来完成任务:

//压缩JS
java -jar yuicompressor-2.4.2.jar --type js --charset utf-8 -v src.js > packed.js

//压缩CSS
java -jar yuicompressor-2.4.2.jar --type css --charset utf-8 -v src.css > packed.css

三、参考官方英文注释:

3.1 How does the YUI Compressor work?

The YUI Compressor is written in Java (requires Java >= 1.4) and relies on Rhino to tokenize the source JavaScript file. It starts by analyzing the source JavaScript file to understand how it is structured. It then prints out the token stream, omitting as many white space characters as possible, and replacing all local symbols by a 1 (or 2, or 3) letter symbol wherever such a substitution is appropriate (in the face of evil features such as eval or with, the YUI Compressor takes a defensive approach by not obfuscating any of the scopes containing the evil statement) The CSS compression algorithm uses a set of finely tuned regular expressions to compress the source CSS file. The YUI Compressor is open-source, so don't hesitate to look at the code to understand exactly how it works.

3.2 Using the YUI Compressor from the command line

java -jar yuicompressor-x.y.z.jar

Usage: java -jar yuicompressor-x.y.z.jar [options] [input file]

Global Options
-h, --help Displays this information
--type <js|css> Specifies the type of the input file
--charset <charset> Read the input file using <charset>
--line-break <column> Insert a line break after the specified column number
-v, --verbose Display informational messages and warnings
-o <file> Place the output into <file>. Defaults to stdout.

JavaScript Options
--nomunge Minify only, do not obfuscate
--preserve-semi Preserve all semicolons
--disable-optimizations Disable all micro optimizations

GLOBAL OPTIONS

-h, --help
Prints help on how to use the YUI Compressor

--line-break
Some source control tools don't like files containing lines longer than,
say 8000 characters. The linebreak option is used in that case to split
long lines after a specific column. It can also be used to make the code
more readable, easier to debug (especially with the MS Script Debugger)
Specify 0 to get a line break after each semi-colon in JavaScript, and
after each rule in CSS.

--type js|css
The type of compressor (JavaScript or CSS) is chosen based on the
extension of the input file name (.js or .css) This option is required
if no input file has been specified. Otherwise, this option is only
required if the input file extension is neither 'js' nor 'css'.

--charset character-set
If a supported character set is specified, the YUI Compressor will use it
to read the input file. Otherwise, it will assume that the platform's
default character set is being used. The output file is encoded using
the same character set. IMPORTANT: if you do not supply this argument
and the file encoding is not compatible with the system's default
encoding, the compressor will throw an error. In particular, if your
file is encoded in utf-8, you should include this parameter.

-o outfile
Place output in file outfile. If not specified, the YUI Compressor will
default to the standard output, which you can redirect to a file.

-v, --verbose
Display informational messages and warnings.

JAVASCRIPT ONLY OPTIONS

--nomunge
Minify only. Do not obfuscate local symbols.

--preserve-semi
Preserve unnecessary semicolons (such as right before a '}') This option
is useful when compressed code has to be run through JSLint (which is the
case of YUI for example)

--disable-optimizations
Disable all the built-in micro optimizations.Note: If no input file is specified, it defaults to stdin.

The following command line (x.y.z represents the version number):

java -jar yuicompressor-x.y.z.jar myfile.js -o myfile-min.jswill minify the file myfile.js and output the file myfile-min.js. For more information on how to use the YUI Compressor, please refer to the documentation included in the archive.

The charset parameter isn't always required, but the compressor may throw an error if the file's encoding is incompatible with the system's default encoding. In particular, if your file is encoded in utf-8, you should supply the parameter.

java -jar yuicompressor-x.y.z.jar myfile.js -o myfile-min.js --charset utf-8

四、YUI Compressor 压缩 JavaScript 的原理

YUI Compressor 压缩 JavaScript 的内容包括:

1.移除注释
2.移除额外的空格
3.细微优化
4.标识符替换(Identifier Replacement)

YUI Compressor 包括哪些细微优化呢?

• object["property"],如果属性名是合法的 JavaScript 标识符(注:合法的 JavaScript 标识符——由一个字母开头,其后选择性地加上一个或者多个字母、数字或下划线)且不是保留字,将优化为: object.property
• {"property":123},如果属性名是合法的 JavaScript 标识符且不是保留字,将优化为 {property:123} (注:在对象字面量中,如果属性名是一个合法的 JavaScript 标识符且不是保留字,并不强制要求用引号引住属性名)。
• 'abcd/'efgh',将优化为 "abcd'efgh"。
• "abcd" + "efgh",如果是字符串相连接,将优化成 "abcdefgh"(注:所有在使用 YUI Compressor 的前提下,对于脚本中的字符串连接,使用连接符 “+” 的效率和可维护性最高)。
对于 JavaScript 最有效的压缩优化,当属标识符替换。

比如:

(function(){function add(num1, num2) {return num1 + num2;}})();

进行属标识符替换后:

(function(){function A(C, B) {return C+ B;}})();

再移除额外的空格,最终成了:

(function(){function A(C,B){return C+B;}})();

YUI Compressor 标识符替换仅替换函数名和变量名,那哪些不能被替代呢?

1.原始值:字符串、布尔值、数字、null 和 undefined。一般来说字符串占的空间最多,而非数字字面量其次(true、false,null,underfinded)。
2.全局变量:window、document、XMLHttpRequest等等。使用最多的就是 document、window。
3.属性名,比如:foo.bar。占据的空间仅次于字符串,”.” 操作符无法被代替,且 a.b.c 更加费空间。
4.关键字。经常被过度使用的关键字有:var、return。最好的优化方法:一个函数仅出现一次 var 和 return 关键字。
对于原始值、全局变量、属性名的优化处理方式大致相同:任何字面量值、全局变量或者属性名被使用超过 2 次(包括2次),都应该用局部变量存储代替。

但有部分情况下是禁止使用标识符替换的:

1.使用 eval() 函数。解决方法:不使用或者创建一个全局函数封装 eval()。
2.使用 with 语句。解决方法:方法同上。
3.JScript 的条件注释。唯一解决的方法:不使用。
由于 YUI Compressor 是建立在 rhino interpreter 基础上的,所以上述所有的优化都是安全的。

② 求助前端JS都是用什么加密

写过js混淆器,谈一些浅显的个人看法。个人认为,js的不可读化处理分为三个方面:压缩(compression)、混淆(obfuscation)和加密(encryption)。(不可读化处理,这是我自己发明的术语,一切会增加代码不可读性的代码转换,都可以这么叫,“增加代码不可读性”可能是代码转换的结果或者目的).1.压缩这一操作的目的,是让最终代码传输量(不代表代码量,也不代表文件体积)尽可能小。压缩js的工具,常见的有:YUICompressor、UglifyJS、GoogleClosureCompiler等。通常在代码压缩的过程中,只改变代码的语法,代码的语义和控制流不会有太大改变。常见做法是把局部变量缩短化,把一些运算进行等价替换等。代码压缩对于代码保护有一些帮助,但由于语义和控制流基本没变,起不了太大作用。在压缩层面上,代码不可读只是一种附带伤害,不是最终目的。2.混淆这一操作的目的,是让代码尽可能地不可读,主要用作代码保护。让代码不可读,增加分析的难度,这是唯一目的。混淆过后文件体积变大一倍也没关系,代码量变多也没关系,运算慢50%也没关系。常见的做法有:分离常量、打乱控制流、增加无义代码、检查运行环境如果不对就罢工,等等。在混淆层面上,代码不可读是最终目的。值得一提的是,GoogleClosureCompiler的AdvanceLevelCompression会压缩类和对象的成员,其压缩结果很难分析,也可以认为是一种混淆,但兼容性不太好。广告时间:我写的js混淆器,中文名叫“看起来很厉害的JS编译器”,英文名叫做TheImpressiveJS.Segment.Compiler,看起来很厉害的JS编译器。3.加密说实话我很难对加密做一个定义,因为加密在Web界有太多歧义了。有加密就有解密,意味着加密操作可逆,密文可以明文化。就这样看来,在Web界,可以称之为加密的东西包括:HTTPS传输、JavaScript实现对称加密或者不对称加密等等。这样看来,不可逆的代码压缩和混淆就不能列入加密这个范畴了。非要找一个可以称之为加密,又经常被人误解为压缩和混淆的东西,DeanEdwards的DeanPacker/Unpacker可以拿来做个例子。比如我们把varnum=1;alert(num);输入DeanPacker,pack一下,得到这么一串东西,是不是看着非常像被压缩和混淆过的代码?把上面那串意义不明物拿来unpack一下,得到了原文。实际上DeanPacker只是对源码进行了一个字符串变换,没有深入到代码语法层面,你可以拿"Helloworld,你好师姐"来试试。用OnlineJavaScriptbeautifier能轻松把这串东西还原为“Helloworld,你好师姐”。可以看出,代码加密意味着:将代码明文进行可逆的变换(加密),生成密文;将密文进行逆变换(解密),可以还原明文;最终运行环境运行的是解密代码。结语实际上大家对压缩、混淆、加密这三个概念还是挺不清晰的,我在这里说一些个人见解,希望有帮助。在现实项目中,我是多种手段结合的:对于不需要做代码保护的项目,比如个人博客,做代码压缩,加快载入速度,这就够了。对于需要做一些代码保护,防止抄袭的项目,可以在源码中加入一些开发者的信息和防护代码,然后混淆和压缩。很不幸的是,我这方面总是做得不太好,防君子防不了小人啊哈哈。对于需要严格加密的项目,可以用混淆、压缩、加密、签名检查等多种手段,这我就不清楚了,等大婶来补充。

③ 压缩后的JS代码怎样解压

一般压缩都经过混淆,如果你看到变量名都是A,B,C,D之类的无规则的命名,那就是被混淆过的,一般来说也很难阅读,就算你 还原了格式。

如果是没有混淆的,你可以试试用js的格式化工具来重新格式化一下的,比如:
/* 美化:格式化代码,使之容易阅读 */
/* 净化:去掉代码中多余的注释、换行、空格等 */
/* 压缩:将代码压缩为更小体积,便于传输 */
/* 解压:将压缩后的代码转换为人可以阅读的格式 */
/* 混淆:将代码的中变量名简短化以减小体积,但可读性差,经混淆后的代码无法还原 */

/* 如果有用,请别忘了推荐给你的朋友: */
/* javascript在线美化、净化、压缩、解压:http://tool.lu/js */

/* 以下是演示代码 */
var Inote = {};
Inote.JSTool = function(options) {
this.options = options || {};
};
Inote.JSTool.prototype = {
_name: 'Javascript工具',
_history: {
'v1.0': ['2011-01-18', 'javascript工具上线'],
'v1.1': ['2012-03-23', '增加混淆功能'],
'v1.2': ['2012-07-21', '升级美化功能引擎'],
'v1.3': ['2014-03-01', '升级解密功能,支持eval,window.eval,window["eval"]等的解密'],
'v1.4': ['2014-08-05', '升级混淆功能引擎'],
'v1.5': ['2014-08-09', '升级js压缩引擎'],
'v1.6': ['2015-04-11', '升级js混淆引擎']
},
options: {},
getName: function() {return this._name;},
getHistory: function() {
return this._history;}
};
var jstool = new Inote.JSTool();

④ 火狐有没有JS 反混淆,反压缩的工具

尊敬的用户,您好!很高兴为您答疑。
js混淆的原理其实还是字符替换和运算符重写,而这个混淆规则是完全由编写者自己定义的,也就是说如果您没有混淆规则,基本无法将混淆后的代码恢复到易阅读状态。
希望我的回答对您有所帮助,如有疑问,欢迎继续咨询我们。

⑤ js代码混淆 以及 混淆后如何使用

js反混淆需要借助第三方工具:
1、在网络搜索js混淆还原
2、把要还原的代码放在第一个文本框,点击js解混淆就可以了。
结果在下面显示

⑥ 请问这个js是由什么方式加密混淆的如何解密

前面的\x只是16进制编解码就好
function decode(str){
return str.replace(/\\x(\w{2})/g,function(_,$1){ return String.fromCharCode(parseInt($1,16)) });
}
后面的使用压缩混淆工具,压缩了空格,简化了变量名.是可以格式化回去的,但是变量你想还原到原来的有含义的命名,是不可能的了

⑦ 给js,css代码压缩,混淆工具只有在线的吗,没有软件吗

软件应该也有,可以自己找找,只是应该还需要自己配置(大部分代码编辑器仅支持实时编译),还是需要自己手动。大部分人都选择自己动手写处理任务(例如gulp任务、grunt任务)。

⑧ vs code有没有压缩混淆js代码的扩展

有的。安装好以后,在js里面按F1,就能压缩代码,压缩好的代码就当前目录下面生成一个 xxx.min.js

网页链接

⑨ 网站发布的时候批量混淆,压缩JS代码用什么工具

此代码纯属装B用,就是js的各种类型间转换,记住js的数据类型(这里用到的):[ObjectObject]falsetrueundefined然后把他们转成string,通过数组下标取值,在字符串拼接就成了!如:!![]+[];//true+[];//0组合:(!![]+[])(+[]);//就是‘t'

⑩ UglifyJS怎么混淆js

如果你是全局安装的,可以直接在需要混淆文件的目录下,

按住shift键右击鼠标选择在此处打开命令窗口如图

新建test.js开始测试
varUglifyJS=require('uglify-js');

//代码压缩
varresult=UglifyJS.minify("varb=function(){};",{fromString:true});
console.log(" ===========================");
console.log(result);

//文件压缩
result=UglifyJS.minify(["demo.js"]);
console.log(" ===========================");
console.log(result.code);
//多文件压缩,指定sourcemap和网站来源
result=UglifyJS.minify(["main.js","demo.js"],{
outSourceMap:"out.js.map",
sourceRoot:"http://onbook.me",
mangle:true
});
console.log(" ===========================");
console.log(result.code);
console.log(result.map);

阅读全文

与js混淆压缩工具相关的资料

热点内容
卡尔曼滤波算法书籍 浏览:769
安卓手机怎么用爱思助手传文件进苹果手机上 浏览:844
安卓怎么下载60秒生存 浏览:803
外向式文件夹 浏览:240
dospdf 浏览:431
怎么修改腾讯云服务器ip 浏览:392
pdftoeps 浏览:496
为什么鸿蒙那么像安卓 浏览:736
安卓手机怎么拍自媒体视频 浏览:186
单片机各个中断的初始化 浏览:724
python怎么集合元素 浏览:481
python逐条解读 浏览:833
基于单片机的湿度控制 浏览:499
ios如何使用安卓的帐号 浏览:883
程序员公园采访 浏览:812
程序员实战教程要多长时间 浏览:979
企业数据加密技巧 浏览:135
租云服务器开发 浏览:814
程序员告白妈妈不同意 浏览:337
攻城掠地怎么查看服务器 浏览:601