在Node.JS环境中,调用JShaman的WebAPI接口,对JS代码进行混淆加密。
效果如下:
代码:
//js代码
var js_code = `
function NewObject(prefix)
{
var count=0;
this.SayHello=function(msg)
{
count++;
alert(prefix+msg);
}
this.GetCount=function()
{
return count;
}
}
var obj=new NewObject("Message : ");
obj.SayHello("You are welcome.");
`;
//配置,配置信息请关注JShaman官网,可能会有更新
var config = {
"part_variable_identifier_obfuscate": 1,
"global_variable_identifier_obfuscate": 0,
"part_function_identifier_obfuscate":0,
"global_function_identifier_obfuscate": 0,
"member_expression_encode": 1,
"numberic_literal_encode": 1,
"binary_express_obfuscate": 1,
"boolean_encode": 1,
"json_encode":1,
"string_unicode_encode": 1,
"assignment_junk_code":1,
"zombie_code": 1,
"eval_encode": 1,
"control_flow": 1,
"comma_operator": 1,
"string_array": 1,
"string_array_encode": 1,
"vm_execute": 1,
"ast_execute": 1,
"no_beautifier": 1,
"tamper_proof": 1,
"comments": 0,
"compress": 1,
"reserved_word": ["jshaman","wlw"]
}
//同步request调用
var jshaman_url = "http://www.jshaman.com:2080/obfuscate/";
var request = require('sync-request');
(function jf(js_code, config, jshaman_url){
var res = request("POST", jshaman_url, {json:{
"js_code":js_code,
"vip_code":"free",
"config":config
}})
var json_res = JSON.parse(res.getBody('utf8'));
if(json_res.state == 0){
//输出
console.log(json_res.content);
}
}
)(js_code,config,jshaman_url);
为什么要对JS代码混淆加密?
中文的说明很多了,引用一段老外的话语,看看外国人是怎样理解JS混淆加密必要性的:
There are numerous reasons why it's a good idea to protect your code, such as:
Prevent anyone from simply copy/pasting your work. This is specially important on 100% client side projects, such as HTML5 games;
Removal of comments and whitespace that aren't needed. Making it faster to load and harder to understand;
Protection of work that hasn't been paid for yet.
You can show your work to the client knowing that they won't have the source code until the invoice has been paid.
本例中,调用的是JShaman英文版的接口。
JShaman是国内知名的JS代码保护服务提供商,JShaman英文版跟中文版功能是有差异的:
主要是功能配置的不同:
具体,请到JShaman官网了解。