1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| const md5File = require('md5-file') var fs = require("fs"); var path = require("path") var pako = require("pako") var xxtea = require("xxtea-node");
var FILEPATH = path.resolve('/Users/smile/Downloads/testx'); var KEY = "18237418234-f3a3-4b" var UNZIP = true
function getFullFileNameNoSuffix(fullFileName){ var fullFileNameNoSuffix = fullFileName.substring(0,fullFileName.lastIndexOf(".")); return fullFileNameNoSuffix }
function getFileMD5(filename){ const hash = md5File.sync(filename) console.log(`The MD5 sum of ${filename} is: ${hash}`) return hash; } function xxteaDecode(filename){ var data; try{ data = fs.readFileSync(filename) }catch(error){ console.log("读取文件失败",filename); return } var res = xxtea.decrypt(data,xxtea.toBytes(KEY)) if(res == null){ console.log("解密失败") return }
if(UNZIP) { console.log("开始解压", filename) res = pako.ungzip(res) } var newName = getFullFileNameNoSuffix(filename) + ".js"
try{ fs.writeFileSync(newName,res) } catch(error){ console.log(newName,"写入出错") return } console.log("写入完毕:",newName) }
function xxteaEncode(filename){ var data; try{ data = fs.readFileSync(filename) }catch(error){ console.log("读取文件失败",filename); return } var res; if(UNZIP) { console.log("开始压缩", filename) res = pako.gzip(data,{ level:6}) }else{ res = data } res = xxtea.encrypt(res,xxtea.toBytes(KEY)) if(res == null){ console.log("加密失败") return } var newName = getFullFileNameNoSuffix(filename) + ".jsc" try{ fs.writeFileSync(newName,res) } catch(error){ console.log(newName,"写入出错") return } console.log("写入完毕:",newName) }
function fileDisplay(filePath,op){ fs.readdir(filePath,function(err,files){ if(err){ console.warn(err) }else{ files.forEach(function(filename){ var filedir = path.join(filePath, filename); fs.stat(filedir,function(eror, stats){ if(eror){ console.warn('获取文件stats失败'); }else{ var isFile = stats.isFile(); var isDir = stats.isDirectory(); if(isFile){ var suffix = path.extname(filedir); if (suffix == ".jsc" || suffix == ".js" ){ if (op=="d" && suffix == ".jsc" ){ console.log(filedir,"解密"); xxteaDecode(filedir) fs.unlinkSync(filedir) } else if(op=="e" && suffix == ".js"){ console.log(filedir,"加密"); xxteaEncode(filedir) fs.unlinkSync(filedir) } else{ console.log("invalid",filedir) } } } if(isDir){ fileDisplay(filedir,op); } } }) }); } }); }
const [node, path0, ...argv] = process.argv; var op = argv[0] fileDisplay(FILEPATH,op);
|