[ create a new paste ] login | about

Link: http://codepad.org/hWjg0RcD    [ raw code | fork ]

Lua, pasted on Oct 10:
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
--[[
secboard
Copyright (C) 2017 IGuess933, hdf73b@protonmail.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
--]]

local loadOnce = 0
local cryptMode = -1
local strClip = ""

-- prior-to-use is defined as: after you download this script and before you use the script
-- therefore, preferably change these to new strings prior-to-use
-- also pick a different saltSeed and saltExOffset

-- use only letters for these two
local strPass = "qitAFLWTunoElaNRWkVhZdMEcyAdBS"
local strSeed = "SySkqAXLIGXacBuysnJLDPmPeV4NZk"

-- only numbers for these two
local saltSeed = 54
local saltExOffset = 3872

-- for preservation of past data:
-- you must copy over the mod storage to any new..
-- ..installation of your OS to the appropriate folder.
-- you must also copy this script as it is

local mod_storage = minetest.get_mod_storage()


local function conf_board()
        cryptMode = -1

	    local formspec = "size[10,10]" .. 
       "bgcolor[#080808BB;true]" ..
       "background[0,0;10,10;gui_formbg.png;true]" ..
       "textarea[1,1;9,9;text;Password Text;" ..
       strPass ..
       "]" ..
        "button_exit[5,8;3,3;save;Save]"

		minetest.show_formspec("confsec", formspec)
end

local function show_board()
        if cryptMode == -1 then
            cryptMode = 0
        end

	    local formspec = "size[10,10]" .. 
       "bgcolor[#080808BB;true]" ..
       "background[0,0;10,10;gui_formbg.png;true]" ..
       "textarea[1,1;9,9;text;Secret Text;" ..
       strClip ..
       "]" ..
        "button_exit[5,8;3,3;save;Save]"

		minetest.show_formspec("secboard", formspec)

end

minetest.register_chatcommand("confsec", {
	func = function(name, param)
        conf_board()
    end
})

minetest.register_chatcommand("secboard", {
	func = function(name, param)
        show_board()
    end
})

-- https://gist.github.com/obikag/7590516

function xor(a,b)
  local r = 0
  local f = math.floor
  for i = 0, 31 do
    local x = a / 2 + b / 2
    if x ~= f(x) then
      r = r + 2^i
    end
    a = f(a / 2)
    b = f(b / 2)
  end
  return r
end

function toBits(num)
    local t={}
    while num>0 do
        rest=math.fmod(num,2)
        t[#t+1]=rest
        num=(num-rest)/2
    end
	--[[ t gives the binary number in reverse. To fix this
		the bits table will give the correct value
		by reversing the values in t.
		The result will be left paddied with zeros to eight digits
	]]
	local bits = {}
	local lpad = 8 - #t
	if lpad > 0 then
		for c = 1,lpad do table.insert(bits,0) end
	end
	-- Reverse the values in t
	for i = #t,1,-1 do table.insert(bits,t[i]) end

    return table.concat(bits)
end

--Changes eight digit binary to decimal
function toDec(bits)
	local bmap = {128,64,32,16,8,4,2,1} --binary map

	local bitt = {}
	for c in bits:gmatch(".") do table.insert(bitt,c) end

	local result = 0

	for i = 1,#bitt do
		if bitt[i] == "1" then result = result + bmap[i] end
	end

	return result
end

function xorChars(chX,chY)
 local bitsA = toBits(string.byte(chX))
 local bitsB = toBits(string.byte(chY))

 local bitt = {}
 local bitu = {}

 for c in bitsA:gmatch(".") do table.insert(bitt,c) end
 for c in bitsB:gmatch(".") do table.insert(bitu,c) end

 local result = {}
 for i = 1,8,1 do
    table.insert(result,xor(bitu[i],bitt[i]))
 end

 return string.char(toDec(table.concat(result)))

end

function shuffleStr(strToShuffle, strSeed)
    local strA = tostring("")
    local strB = tostring("")

    local seedBlock = {}
    for ch in strSeed:gmatch(".") do
        local c = toBits(string.byte(ch))
        table.insert(seedBlock,c)
    end

    local nStrToShuffle = string.len(strToShuffle)
    local nCounter = 1

    local strSaltSeed = tostring(saltSeed)

    local nStrSaltSeedLen = string.len(strSaltSeed)

    local nCounterB = 1

    for i = 1,#seedBlock do

        local bit = seedBlock[i]

        local cSalt = string.sub(strSaltSeed, nCounterB, nCounterB+1)

        local cShuff = string.sub(strToShuffle, nCounter, nCounter+1)

        if bit <= "z" and cSalt > "5" then
            strA = strA .. cShuff              
        elseif bit <= "z" and cSalt < "5" then
            strB = strB .. cShuff
        elseif bit <= "Z" and cSalt < "5" then
            strA = strA .. cShuff          
        elseif bit <= "Z" and cSalt > "5" then
            strB = strB .. cShuff
        elseif bit <= "z" and cSalt == "5" then
            strA = strA .. cShuff              
        elseif bit <= "Z" and cSalt == "5" then
            strB = strB .. cShuff
        end

        nCounter = nCounter + 1
        nCounterB = nCounterB + 1

        if nCounter > nStrToShuffle then
            nCounter = 1
            nCounterB = 1
            break
        end

        if nCounterB > nStrSaltSeedLen then
            nCounterB = 1
        end


        if nCounter > nStrToShuffle then
            break
        end
    end

    local result = tostring("")
    result = result .. strA
    result = result .. strB
    return result

end

--Encryption and Decryption Algorithm for XOR Block cipher
function E(str)

    
    local saltSeedBackup = saltSeed

    local seedToUse = tostring("")
    seedToUse = seedToUse .. strSeed

    local keyToUse = tostring("")
    keyToUse = keyToUse .. strPass

    local passBlock = {}
    for ch in keyToUse:gmatch(".") do
        local c = ch
        table.insert(passBlock,c)
    end


    local strSaltSeed = tostring("")

	--split string into a table containing only binary numbers of each character
	local block = {}

    if cryptMode == 1 then
        str = minetest.compress(str, "deflate", "5")
        strSaltSeed = tostring(saltSeed)
        for ch in strSaltSeed:gmatch(".") do
            local c = ch
            table.insert(block,c)
        end
    elseif cryptMode == 2 then
        for ch in str:gmatch(".") do
            if ch >= "0" and ch <= "9" then
                strSaltSeed = strSaltSeed .. ch
            else
                break
            end
        end
        saltSeed = tonumber(strSaltSeed)
    end


	for ch in str:gmatch(".") do
		local c = ch
		table.insert(block,c)
	end

	--for each binary number perform xor transformation
    local nPassBlock = string.len(keyToUse)
    local nPassBlockCounter = 1
    local nSeedRegenCounter = 1

    local nBlockStart = 1

    nBlockStart = string.len(strSaltSeed)+1

	local blockFinal = {}
	for i = nBlockStart,#block do

        local chNew = xorChars(passBlock[nPassBlockCounter],block[i])

        nPassBlockCounter = nPassBlockCounter + 1
        if nPassBlockCounter > nPassBlock then
            nSeedRegenCounter = nSeedRegenCounter + 1
            nPassBlockCounter = 1

            keyToUse = shuffleStr(keyToUse,seedToUse)

            if nSeedRegenCounter > 15 then
                seedToUse = shuffleStr(seedToUse,keyToUse)
                nSeedRegenCounter = 1
            end

            passBlock = {}
            for ch in keyToUse:gmatch(".") do
                local c = ch
                table.insert(passBlock,c)
            end
        end

		block[i] = chNew
        if cryptMode == 2 then
            table.insert(blockFinal, block[i])
        end

	end

    saltSeed = saltSeedBackup

    local strToReturn = tostring("")
    if cryptMode == 1 then
        strToReturn = table.concat(block)
	    return strToReturn
    elseif cryptMode == 2 then
        strToReturn = table.concat(blockFinal)
        strToReturn = minetest.decompress(strToReturn, "deflate", "5")
	    return strToReturn
    end

	return table.concat(blockFinal)
end

function scrambleStr(str)
    local r = tostring("")
    r = E(str)
    return r
end

function descrambleStr(str)
    local r = tostring("")
    r = E(str)
    return r
end

-- Show form when the /formspec command is used.
minetest.register_chatcommand("encGo", {    
	func = function(name, param)

    if loadOnce == 0 then
        loadOnce = 1
        saltSeed = tonumber(mod_storage:get_string("salt"))
        saltSeed = saltSeed + 3
    
    end

    saltSeed = saltSeed + saltExOffset

    if cryptMode == -1 then
        return
    end

    cryptMode = 1

    strClip = scrambleStr(strClip)
    strClip = minetest.encode_base64(strClip)
    

    mod_storage:set_string("salt", tostring(saltSeed))

	end
})

-- Show form when the /formspec command is used.
minetest.register_chatcommand("decGo", {
	func = function(name, param)

    if loadOnce == 0 then
        loadOnce = 1
        saltSeed = tonumber(mod_storage:get_string("salt"))
        saltSeed = saltSeed + 3    
    end

    if cryptMode == -1 then
        return
    end

    cryptMode = 2

    strClip = minetest.decode_base64(strClip)
    saltSeed = tonumber(mod_storage:get_string("salt"))
    strClip = descrambleStr(strClip)

 
	end
})


minetest.register_on_formspec_input(function(formname, fields)
    if formname == "confsec" then

        mod_storage:set_string("salt", tostring(saltSeed))
        if fields.text == nil or not fields.save then
            return true
        else
            strPass = fields.text
            fields.text = "."
            return true
        end
    end

	if formname ~= "secboard" then
		return false
    else

        if fields.text == nil or not fields.save then
            return true
        end

	    strClip = fields.text
        fields.text = "."
        return true
	end

    return true

end)


Create a new paste based on this one


Comments: