-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypt.lua
More file actions
378 lines (326 loc) · 12 KB
/
crypt.lua
File metadata and controls
378 lines (326 loc) · 12 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
require "iutf8"
require "utils"
require "menu"
require "windows"
local MessageDigest = require "java.security.MessageDigest"
local Base64 = require "android.util.Base64"
local UUID = require "java.util.UUID"
-- XOR helper (pure Lua, no bit32 needed)
local function bxor(a, b)
local r, p = 0, 1
for _ = 1, 8 do
local a1, b1 = a % 2, b % 2
if a1 ~= b1 then r = r + p end
a = (a - a1) / 2
b = (b - b1) / 2
p = p * 2
end
return r
end
-- XOR cipher with key, returns hex string
local function xor_encrypt(text, key)
local result = {}
for i = 1, #text do
local tb = string.byte(text, i)
local kb = string.byte(key, (i - 1) % #key + 1)
result[#result + 1] = string.format("%02x", bxor(tb, kb))
end
return table.concat(result)
end
-- Hex string → XOR decrypt with key
local function xor_decrypt(hex, key)
local result = {}
for i = 1, #hex, 2 do
local byte = tonumber(hex:sub(i, i + 1), 16)
if not byte then return nil end
local kb = string.byte(key, ((i - 1) / 2) % #key + 1)
result[#result + 1] = string.char(bxor(byte, kb))
end
return table.concat(result)
end
-- Build shift map for Latin (26) + Cyrillic А-Я (32)
local function make_shift_map(shift)
local map = {}
for i = 0, 25 do
map[string.char(65 + i)] = string.char(65 + (i + shift) % 26)
map[string.char(97 + i)] = string.char(97 + (i + shift) % 26)
end
for i = 0, 31 do
map[utf8.char(0x410 + i)] = utf8.char(0x410 + (i + shift) % 32)
map[utf8.char(0x430 + i)] = utf8.char(0x430 + (i + shift) % 32)
end
return map
end
-- ROT13 (Latin) + ROT16 (Cyrillic, half of 32)
local rot_map = make_shift_map(13)
for i = 0, 31 do
rot_map[utf8.char(0x410 + i)] = utf8.char(0x410 + (i + 16) % 32)
rot_map[utf8.char(0x430 + i)] = utf8.char(0x430 + (i + 16) % 32)
end
local function rot13(text)
return text:gsub(utf8.charpattern, function(c)
return rot_map[c] or c
end)
end
-- Caesar cipher (same shift for both alphabets)
local function caesar(text, shift)
local map = make_shift_map(shift)
return text:gsub(utf8.charpattern, function(c)
return map[c] or c
end)
end
-- Morse code tables (Latin + Cyrillic)
local to_morse = {
A = ".-", B = "-...", C = "-.-.", D = "-..", E = ".", F = "..-.",
G = "--.", H = "....", I = "..", J = ".---", K = "-.-", L = ".-..",
M = "--", N = "-.", O = "---", P = ".--.", Q = "--.-", R = ".-.",
S = "...", T = "-", U = "..-", V = "...-", W = ".--", X = "-..-",
Y = "-.--", Z = "--..",
["0"] = "-----", ["1"] = ".----", ["2"] = "..---", ["3"] = "...--",
["4"] = "....-", ["5"] = ".....", ["6"] = "-....", ["7"] = "--...",
["8"] = "---..", ["9"] = "----.",
[" "] = "/", ["."] = ".-.-.-", [","] = "--..--", ["?"] = "..--..",
["!"] = "-.-.--", ["'"] = ".----.", ['"'] = ".-..-.",
[":"] = "---...", [";"] = "-.-.-.", ["="] = "-...-",
["+"] = ".-.-.", ["-"] = "-....-", ["/"] = "-..-.",
["("] = "-.--.", [")"] = "-.--.-", ["@"] = ".--.-.",
}
-- Russian Morse code
local ru_morse = {
[0x410] = ".-", [0x411] = "-...", [0x412] = ".--", [0x413] = "--.",
[0x414] = "-..", [0x415] = ".", [0x416] = "...-", [0x417] = "--..",
[0x418] = "..", [0x419] = ".---", [0x41A] = "-.-", [0x41B] = ".-..",
[0x41C] = "--", [0x41D] = "-.", [0x41E] = "---", [0x41F] = ".--.",
[0x420] = ".-.", [0x421] = "...", [0x422] = "-", [0x423] = "..-",
[0x424] = "..-.", [0x425] = "....", [0x426] = "-.-.", [0x427] = "---.",
[0x428] = "----", [0x429] = "--.-", [0x42A] = "--.--", [0x42B] = "-.-",
[0x42C] = "-..-", [0x42D] = "..-..", [0x42E] = "..--", [0x42F] = ".-.-",
[0x401] = ".",
}
for cp, morse in pairs(ru_morse) do
to_morse[utf8.char(cp)] = morse
to_morse[utf8.char(cp + 0x20)] = morse -- lowercase
end
local from_morse = {}
for k, v in pairs(to_morse) do
if not from_morse[v] then
from_morse[v] = k
end
end
local function morse_encode(text)
local result = {}
text:gsub(utf8.charpattern, function(c)
local upper = string.upper(c)
result[#result + 1] = to_morse[upper] or to_morse[c] or c
end)
return table.concat(result, " ")
end
local function morse_decode(text)
local result = {}
for word in text:gmatch("[^ ]+") do
result[#result + 1] = from_morse[word] or word
end
return table.concat(result)
end
-- Base64 via Java
local function base64_encode(text)
local bytes = luajava.newInstance("java.lang.String", text):getBytes("UTF-8")
return Base64:encodeToString(bytes, Base64.NO_WRAP)
end
local function base64_decode(text)
local bytes = Base64:decode(text, Base64.NO_WRAP)
return luajava.newInstance("java.lang.String", bytes, "UTF-8"):toString()
end
-- SHA-256 via Java
local function sha256(text)
local md = MessageDigest:getInstance("SHA-256")
local bytes = md:digest(luajava.newInstance("java.lang.String", text):getBytes("UTF-8"))
local hex = {}
for i = 1, #bytes do
local b = bytes[i]
if b < 0 then b = b + 256 end
hex[#hex + 1] = string.format("%02x", b)
end
return table.concat(hex)
end
-- Password generator
local chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*"
local function genpass(length)
local result = {}
for _ = 1, length do
local idx = math.random(1, #chars)
result[#result + 1] = chars:sub(idx, idx)
end
return table.concat(result)
end
-- Helper: get text from args or field
local function get_text(query)
local args = query:getArgs()
if args ~= "" then return args, true end
return query:replaceExpression(""), false
end
local function output(input, query, result, from_args)
if from_args then
query:answer(result)
else
inline:setText(input, result)
end
end
-- Commands
local function encrypt_cmd(input, query)
local parts = utils.split(query:getArgs(), " ", 2)
local key = parts[1]
if not key or key == "" then
return query:answer "Usage: encrypt <key> [text]"
end
local text = parts[2]
if text and text ~= "" then
query:answer(xor_encrypt(text, key))
else
inline:setText(input, xor_encrypt(query:replaceExpression(""), key))
end
end
local function decrypt_cmd(input, query)
local parts = utils.split(query:getArgs(), " ", 2)
local key = parts[1]
if not key or key == "" then
return query:answer "Usage: decrypt <key> [hex]"
end
local hex = parts[2]
if hex and hex ~= "" then
local result = xor_decrypt(hex, key)
query:answer(result or "Invalid hex input")
else
local result = xor_decrypt(query:replaceExpression(""):gsub("%s+", ""), key)
inline:setText(input, result or "Invalid hex input")
end
end
local function rot13_cmd(input, query)
local text, from_args = get_text(query)
output(input, query, rot13(text), from_args)
end
local function caesar_cmd(input, query)
local parts = utils.split(query:getArgs(), " ", 2)
local shift = tonumber(parts[1])
if not shift then
return query:answer "Usage: caesar <shift> [text]"
end
local text = parts[2]
if text and text ~= "" then
query:answer(caesar(text, shift))
else
inline:setText(input, caesar(query:replaceExpression(""), shift))
end
end
local function morse_cmd(input, query)
local text, from_args = get_text(query)
output(input, query, morse_encode(text), from_args)
end
local function demorse_cmd(input, query)
local text, from_args = get_text(query)
output(input, query, morse_decode(text), from_args)
end
local function base64e_cmd(input, query)
local text, from_args = get_text(query)
local ok, result = pcall(base64_encode, text)
output(input, query, ok and result or "Error: " .. result, from_args)
end
local function base64d_cmd(input, query)
local text, from_args = get_text(query)
local ok, result = pcall(base64_decode, text:gsub("%s+", ""))
output(input, query, ok and result or "Error: " .. result, from_args)
end
local function sha256_cmd(input, query)
local text, from_args = get_text(query)
local ok, result = pcall(sha256, text)
output(input, query, ok and result or "Error: " .. result, from_args)
end
local function uuid_cmd(_, query)
query:answer(UUID:randomUUID():toString())
end
local function genpass_cmd(_, query)
local len = tonumber(query:getArgs())
if not len or len < 1 then len = 16 end
if len > 128 then len = 128 end
query:answer(genpass(len))
end
-- Floating encrypt/decrypt window
local function fcrypt(input, query)
windows.createAligned(input, { noLimits = true }, function(ui)
local textInput = ui.textInput("Text", "Message")
local keyInput = ui.textInput("Key", "Secret key")
local result = ui.text ""
textInput:getEditText():setMaxLines(5)
result:setMaxLines(10)
textInput:setText(query:getArgs())
local paste = ui.smallButton("Paste", function()
if not windows.insertText(result:getText()) then
return inline:toast "Please focus on the desired input"
end
end)
paste:setEnabled(windows.isInsertAvailable())
ui.onFocusChanged = function(isFocused)
paste:setEnabled(not isFocused and windows.isInsertAvailable())
end
return {
textInput,
ui.spacer(8),
keyInput,
ui.spacer(8),
{
ui.smallButton("Encrypt", function()
local key = keyInput:getText()
if key == "" then return inline:toast "Enter a key" end
result:setText(xor_encrypt(textInput:getText(), key))
end),
ui.spacer(8),
ui.smallButton("Decrypt", function()
local key = keyInput:getText()
if key == "" then return inline:toast "Enter a key" end
local r = xor_decrypt(textInput:getText():gsub("%s+", ""), key)
result:setText(r or "Invalid hex input")
end),
ui.spacer(8),
ui.smallButton("B64 Enc", function()
local ok, r = pcall(base64_encode, textInput:getText())
result:setText(ok and r or "Error")
end),
ui.spacer(8),
ui.smallButton("B64 Dec", function()
local ok, r = pcall(base64_decode, textInput:getText():gsub("%s+", ""))
result:setText(ok and r or "Error")
end),
},
ui.spacer(8),
result,
ui.spacer(8),
{
ui.smallButton("Close", function()
ui:close()
end),
ui.spacer(8),
paste,
}
}
end)
query:answer()
end
return function(module)
module:setCategory "Crypt"
module:registerCommand("encrypt", encrypt_cmd, "XOR encrypt: encrypt <key> [text]")
module:registerCommand("decrypt", decrypt_cmd, "XOR decrypt: decrypt <key> [hex]")
module:registerCommand("rot13", rot13_cmd, "ROT13 cipher")
module:registerCommand("caesar", caesar_cmd, "Caesar cipher: caesar <shift> [text]")
module:registerCommand("morse", morse_cmd, "Text to Morse code")
module:registerCommand("demorse", demorse_cmd, "Morse code to text")
module:registerCommand("base64e", base64e_cmd, "Base64 encode")
module:registerCommand("base64d", base64d_cmd, "Base64 decode")
module:registerCommand("sha256", sha256_cmd, "SHA-256 hash")
module:registerCommand("uuid", uuid_cmd, "Generate random UUID")
module:registerCommand("genpass", genpass_cmd, "Generate password: genpass [length]")
if windows.isSupported() then
module:registerCommand("fcrypt", fcrypt, "Floating encrypt/decrypt toolbox")
windows.supportInsert()
end
module:saveLazyLoad()
end