Advertisement
awsumben13

Encryption

Aug 13th, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.92 KB | None | 0 0
  1.  
  2. -- need to optimise all this
  3.  
  4. local blocksize = 5
  5. local bit32 = 2 ^ 32
  6. local prime1 = 8747
  7. local prime2 = 2147483647
  8. local bit32half = bit32/2
  9.  
  10. local floor = math.floor
  11.  
  12. local function keygen( seed, length )
  13.     local count = 1
  14.     local keys, garbage = {}, {}
  15.  
  16.     for i = 1, length, blocksize do
  17.         seed = ( seed * ( seed > bit32half and prime2 or prime1 ) + 1 ) % bit32 + count
  18.         count = count + 1
  19.  
  20.         garbage[i] = math.max( 0, ( seed - 1 ) % 10 - 4 )
  21.  
  22.         keys[i] = seed % 256
  23.     end
  24.     return keys, garbage
  25. end
  26.  
  27. local h = {}
  28. for i = 0, 15 do
  29.     h[i] = ("%X"):format( i )
  30. end
  31. local hl = {}
  32. for i = 0, 15 do
  33.     hl[("%X"):format( i )] = i
  34. end
  35.  
  36. local function tohex2( n )
  37.     return h[math.floor( n / 16 )] .. h[n % 16]
  38. end
  39. local function fromhex2( h )
  40.     return hl[h:sub( 1, 1 )] * 16 + hl[h:sub( 2, 2 )]
  41. end
  42.  
  43. local function numbertochars( n )
  44.     local s = tohex2( n % 256 )
  45.     for i = 1, 3 do
  46.         n = math.floor( n / 256 )
  47.         s = tohex2( n % 256 ) .. s
  48.     end
  49.     return s
  50. end
  51. local function charstonumber( c )
  52.     local n = 0
  53.     for i = 1, 4 do
  54.         n = n * 256 + fromhex2( c:sub( i * 2 - 1 ) )
  55.     end
  56.     return n
  57. end
  58.  
  59. local function newgarbage( length )
  60.     if length == 0 then return "" end
  61.     return tohex2( math.random( 0, 255 ) ) .. newgarbage( length - 1 )
  62. end
  63.  
  64. local function stringkey( str )
  65.     local key = 0
  66.     for i = 1, #str do
  67.         key = key * 256 + str:byte( i )
  68.     end
  69.     return key
  70. end
  71.  
  72. local encrypt = {}
  73.  
  74. function encrypt.encrypt( text, key )
  75.     key = type( key ) == "string" and stringkey( key ) or key
  76.     text = textutils.serialize( text )
  77.     if type( key ) ~= "number" then
  78.         return error( "expected number/string key, got " .. type( key ) )
  79.     end
  80.  
  81.     local keys, garbage = keygen( key, #text )
  82.     local cipher = { numbertochars( #text ) }
  83.     math.randomseed( os.clock() )
  84.  
  85.     for i = 1, #text, blocksize do
  86.         for n = 0, blocksize - 1 do
  87.             if i + n <= #text then
  88.                 cipher[#cipher + 1] = tohex2( bit.bxor( text:byte( i + n ), keys[i] ) )
  89.             else
  90.                 break
  91.             end
  92.         end
  93.         cipher[#cipher + 1] = newgarbage( garbage[i] )
  94.     end
  95.  
  96.     return table.concat( cipher )
  97. end
  98.  
  99. function encrypt.decrypt( cipher, key )
  100.     key = type( key ) == "string" and stringkey( key ) or key
  101.     if type( cipher ) ~= "string" then
  102.         return error( "expected string cipher, got " .. type( cipher ) )
  103.     end
  104.     if type( key ) ~= "number" then
  105.         return error( "expected number/string key, got " .. type( key ) )
  106.     end
  107.     local length = charstonumber( cipher:sub( 1, 8 ) )
  108.     cipher = cipher:sub( 9 )
  109.  
  110.     local keys, garbage = keygen( key, length )
  111.     local text = {}
  112.  
  113.     local i = 1
  114.     while #cipher > 0 do
  115.         local block = cipher:sub( 1, math.min( #cipher - garbage[i] * 2, blocksize * 2 ) )
  116.  
  117.         for n = 1, #block, 2 do
  118.             text[#text + 1] = string.char( bit.bxor( fromhex2( block:sub( n ) ), keys[i] ) )
  119.         end
  120.  
  121.         cipher = cipher:sub( blocksize * 2 + garbage[i] * 2 + 1 )
  122.         i = i + blocksize
  123.     end
  124.  
  125.     return textutils.unserialize( table.concat( text ) )
  126. end
  127.  
  128. return encrypt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement