Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- This is a util code for Computercraft
- download:
- pastebin get zLQD13rf jutils
- then load it with
- os.loadAPI("jutils")
- usage example:
- jutils.Decrypt("VmZ0ZlZmBGtwAQVjVmDlAvZmAQRwAQN5VmDkAPZ0ZQxwZmDkVmDkAPZ0ZwHwZmDlVmZ0Zj==","thekey")
- --]]
- Jutils = {}
- -- string stuff
- function split(text, delimiter)
- if text.find(text, delimiter) == nil then
- return { text }
- end
- local splited_texts = {}
- local last_position
- for splited_text, position in text:gmatch("(.-)"..delimiter.."()") do
- table.insert(splited_texts, splited_text)
- last_position = position
- end
- table.insert(splited_texts, string.sub(text, last_position))
- return splited_texts
- end
- function split_lines(text)
- local lines = {}
- local function splitter(line)
- table.insert(lines, line)
- return ""
- end
- text:gsub("(.-\r?\n)", splitter)
- return lines
- end
- -- Returns the ASCII bytecode of either 'a' or 'A'
- function ascii_base(s)
- return s:lower() == s and ('a'):byte() or ('A'):byte()
- end
- -- ROT13 is based on Caesar ciphering algorithm, using 13 as a key
- function caesar_cipher(str, key)
- return (str:gsub('%a', function(s)
- local base = ascii_base(s)
- return string.char(((s:byte() - base + key) % 26) + base)
- end))
- end
- -- str : a string to be ciphered
- -- returns : the ciphered string
- function rot13(str)
- return caesar_cipher(str, 13)
- end
- -- str : a string to be deciphered
- -- returns : the deciphered string
- function derot13(str)
- return caesar_cipher(str, -13)
- end
- function _keyToInt(str)
- local z=0;
- for i = 1, #str do
- z= z+string.byte(i)
- end
- return z
- end
- -- number stuff
- function round(num, n)
- local mult = 10^(n or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- -- table stuff
- function dump(table)
- for key, value in pairs(table) do
- print("["..key.."] ", value)
- end
- end
- function equal(table1, table2)
- local result = true
- for i=1, #table1 do
- if table1[i] ~= table2[i] then
- result = false
- break
- end
- end
- return result
- end
- -- other stuff
- -- bitshift functions (<<, >> equivalent)
- -- shift left
- function lsh(value,shift)
- return (value*(2^shift)) % 256
- end
- -- shift right
- function rsh(value,shift)
- return math.floor(value/2^shift) % 256
- end
- -- return single bit (for OR)
- function bit(x,b)
- return (x % 2^b - x % 2^(b-1) > 0)
- end
- -- logic OR for number values
- function lor(x,y)
- local bit = bit
- result = 0
- for p=1,8 do result = result + (((bit(x,p) or bit(y,p)) == true) and 2^(p-1) or 0) end
- return result
- end
- -- encryption table
- local base64chars = {
- [0]='A',[1]='B',[2]='C',[3]='D',[4]='E',[5]='F',[6]='G',[7]='H',[8]='I',
- [9]='J',[10]='K',[11]='L',[12]='M',[13]='N',[14]='O',[15]='P',[16]='Q',
- [17]='R',[18]='S',[19]='T',[20]='U',[21]='V',[22]='W',[23]='X',[24]='Y',
- [25]='Z',[26]='a',[27]='b',[28]='c',[29]='d',[30]='e',[31]='f',[32]='g',
- [33]='h',[34]='i',[35]='j',[36]='k',[37]='l',[38]='m',[39]='n',[40]='o',
- [41]='p',[42]='q',[43]='r',[44]='s',[45]='t',[46]='u',[47]='v',[48]='w',
- [49]='x',[50]='y',[51]='z',[52]='0',[53]='1',[54]='2',[55]='3',[56]='4',
- [57]='5',[58]='6',[59]='7',[60]='8',[61]='9',[62]='-',[63]='_'}
- -- function encode
- -- encodes input string to base64.
- function enc(data)
- local bytes = {}
- local result = ""
- local rsh = rsh
- local lsh = lsh
- local lor = lor
- local strformat = string.format
- local strchar = string.char
- local base64chars = base64chars
- for spos=0,string.len(data)-1,3 do
- for byte=1,3 do bytes[byte] = string.byte(string.sub(data,(spos+byte))) or 0 end
- result = strformat('%s%s%s%s%s',result,base64chars[rsh(bytes[1],2)],base64chars[lor(lsh((bytes[1] % 4),4), rsh(bytes[2],4))] or "=",((#data-spos) > 1) and base64chars[lor(lsh(bytes[2] % 16,2), rsh(bytes[3],6))] or "=",((#data-spos) > 2) and base64chars[(bytes[3] % 64)] or "=")
- end
- return result
- end
- -- decryption table
- local base64bytes = {['A']=0,['B']=1,['C']=2,['D']=3,['E']=4,['F']=5,['G']=6,['H']=7,['I']=8,['J']=9,['K']=10,['L']=11,['M']=12,['N']=13,['O']=14,['P']=15,['Q']=16,['R']=17,['S']=18,['T']=19,['U']=20,['V']=21,['W']=22,['X']=23,['Y']=24,['Z']=25,['a']=26,['b']=27,['c']=28,['d']=29,['e']=30,['f']=31,['g']=32,['h']=33,['i']=34,['j']=35,['k']=36,['l']=37,['m']=38,['n']=39,['o']=40,['p']=41,['q']=42,['r']=43,['s']=44,['t']=45,['u']=46,['v']=47,['w']=48,['x']=49,['y']=50,['z']=51,['0']=52,['1']=53,['2']=54,['3']=55,['4']=56,['5']=57,['6']=58,['7']=59,['8']=60,['9']=61,['-']=62,['_']=63,['=']=nil}
- -- function decode
- -- decode base64 input to string
- function dec(data)
- local chars = {}
- local result=""
- local rsh = rsh
- local lsh = lsh
- local lor = lor
- local strformat = string.format
- local strchar = string.char
- local base64chars = base64chars
- for dpos=0,string.len(data)-1,4 do
- for char=1,4 do chars[char] = base64bytes[(string.sub(data,(dpos+char),(dpos+char)) or "=")] end
- result = strformat('%s%s%s%s',result,strchar(lor(lsh(chars[1],2), rsh(chars[2],4))),(chars[3] ~= nil) and strchar(lor(lsh(chars[2],4), rsh(chars[3],2))) or "",(chars[4] ~= nil) and strchar(lor(lsh(chars[3],6) % 192, (chars[4]))) or "")
- end
- return result
- end
- -- some type of encryption here
- function Encrypt(str,encryptionKey)
- local str = textutils.serialize( str )
- local encrypted = ''
- for i = 1, #str do
- encrypted = encrypted .. '#' .. string.byte(str:sub(i, i)) + _keyToInt(encryptionKey)
- end
- return rot13( enc( derot13(encrypted)) )
- end
- function Decrypt(str,encryptionKey)
- local decrypted = ''
- local str = derot13(str)
- str = dec(str)
- str = rot13(str)
- for x in str:gmatch('#(%d+)') do
- decrypted = decrypted .. string.char(x - _keyToInt(encryptionKey))
- end
- return textutils.unserialize(decrypted)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement