1+ const SimpleXorEncrypt = require ( './SimpleXorEncrypt' ) ;
2+ const ShuffleWithKey = require ( './ShuffleWithKey' ) ;
3+ const compareVersion = require ( 'compare-version' ) ;
4+ const luamin = require ( 'luamin' ) ;
5+
6+ const templates = {
7+ credit : '-- Lua simple XOR encrypt by Ganlv\n' ,
8+ main : 'local main = ' ,
9+ decoder : '((function (bytes, key)\n' +
10+ ' -- http://lua-users.org/wiki/BitUtils\n' +
11+ ' function bxor(a, b)\n' +
12+ ' local r = 0\n' +
13+ ' for i = 0, 31 do\n' +
14+ ' local x = a / 2 + b / 2\n' +
15+ ' if x ~= math.floor(x) then\n' +
16+ ' r = r + 2 ^ i\n' +
17+ ' end\n' +
18+ ' a = math.floor(a / 2)\n' +
19+ ' b = math.floor(b / 2)\n' +
20+ ' end\n' +
21+ ' return r\n' +
22+ ' end\n' +
23+ '\n' +
24+ ' local getDataBytes = function (bytes)\n' +
25+ ' local result = {}\n' +
26+ ' local i = 1\n' +
27+ ' local index = bytes[i]\n' +
28+ ' while (index >= 0) do\n' +
29+ ' result[i] = bytes[index + 1]\n' +
30+ ' i = i + 1\n' +
31+ ' index = bytes[i]\n' +
32+ ' end\n' +
33+ ' return result\n' +
34+ ' end\n' +
35+ '\n' +
36+ ' local decode = function (bytes, key)\n' +
37+ ' if #key <= 0 then\n' +
38+ ' return {}\n' +
39+ ' end\n' +
40+ ' local i = 1\n' +
41+ ' local j = 1\n' +
42+ ' for i = 1, #bytes do\n' +
43+ ' bytes[i] = bxor(bytes[i], string.byte(key, j))\n' +
44+ ' j = j + 1\n' +
45+ ' if j > #key then\n' +
46+ ' j = 1\n' +
47+ ' end\n' +
48+ ' end\n' +
49+ ' return bytes\n' +
50+ ' end\n' +
51+ '\n' +
52+ ' local bytesToString = function (bytes)\n' +
53+ ' local result = ""\n' +
54+ ' for i = 1, #bytes do\n' +
55+ ' result = result .. string.char(bytes[i])\n' +
56+ ' end\n' +
57+ ' return result\n' +
58+ ' end\n' +
59+ '\n' +
60+ ' return bytesToString(decode(getDataBytes(bytes), key))\n' +
61+ 'end)({' ,
62+ decoderEnd : '}, key))\n' +
63+ 'if main then\n' +
64+ ' main()\n' +
65+ 'else\n' +
66+ ' ' ,
67+ keyWrongAlertEnd : '\n' +
68+ 'end'
69+ } ;
70+
71+ function parseOptions ( options ) {
72+ if ( options . isGG ) {
73+ if ( ! options . luaVersion ) {
74+ options . luaVersion = '5.2' ;
75+ }
76+ if ( ! options . keyInputCode ) {
77+ options . keyInputCode = 'key = gg.prompt({"请输入密码:"}, {""}, {"text"})[0]\n' ;
78+ }
79+ if ( ! options . keyWrongAlertCode ) {
80+ options . keyWrongAlertCode = 'gg.alert("密码错误")' ;
81+ }
82+ }
83+ if ( ! options . luaVersion ) {
84+ options . luaVersion = '5.1' ;
85+ }
86+ if ( ! options . keyInputCode ) {
87+ options . keyInputCode = 'key = "把这里替换成密码"\n' ;
88+ }
89+ if ( compareVersion ( options . luaVersion , '5.2' ) < 0 ) {
90+ options . loadFunction = 'loadstring' ;
91+ } else {
92+ options . loadFunction = 'load' ;
93+ }
94+ if ( ! options . keyWrongAlertCode ) {
95+ options . keyWrongAlertCode = 'print("密码错误")' ;
96+ }
97+ return options ;
98+ }
99+
100+ function encrypt ( bytes , key , options = { } ) {
101+ options = parseOptions ( options ) ;
102+ let encryptedBytes = ShuffleWithKey . shuffle ( SimpleXorEncrypt . encrypt ( bytes , key ) , key ) ;
103+ let code = options . keyInputCode
104+ + templates . main
105+ + options . loadFunction
106+ + templates . decoder
107+ + encryptedBytes . join ( ',' )
108+ + templates . decoderEnd
109+ + options . keyWrongAlertCode
110+ + templates . keyWrongAlertEnd ;
111+ return templates . credit
112+ + luamin . minify ( code ) ;
113+ }
114+
115+ exports . encrypt = encrypt ;
0 commit comments