Advertisement
Alakazard12

AESUtil

Apr 27th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.76 KB | None | 0 0
  1. --require("bit");
  2.  
  3. local public = {};
  4. local private = {};
  5.  
  6. aeslua.util = public;
  7.  
  8. --
  9. -- calculate the parity of one byte
  10. --
  11. function public.byteParity(byte)
  12.     byte = bit.bxor(byte, bit.brshift(byte, 4));
  13.     byte = bit.bxor(byte, bit.brshift(byte, 2));
  14.     byte = bit.bxor(byte, bit.brshift(byte, 1));
  15.     return bit.band(byte, 1);
  16. end
  17.  
  18. --
  19. -- get byte at position index
  20. --
  21. function public.getByte(number, index)
  22.     if (index == 0) then
  23.         return bit.band(number,0xff);
  24.     else
  25.         return bit.band(bit.brshift(number, index*8),0xff);
  26.     end
  27. end
  28.  
  29.  
  30. --
  31. -- put number into int at position index
  32. --
  33. function public.putByte(number, index)
  34.     if (index == 0) then
  35.         return bit.band(number,0xff);
  36.     else
  37.         return bit.blshift(bit.band(number,0xff),index*8);
  38.     end
  39. end
  40.  
  41. --
  42. -- convert byte array to int array
  43. --
  44. function public.bytesToInts(bytes, start, n)
  45.     local ints = {};
  46.     for i = 0, n - 1 do
  47.         ints[i] = public.putByte(bytes[start + (i*4)    ], 3)
  48.                 + public.putByte(bytes[start + (i*4) + 1], 2)
  49.                 + public.putByte(bytes[start + (i*4) + 2], 1)    
  50.                 + public.putByte(bytes[start + (i*4) + 3], 0);
  51.     end
  52.     return ints;
  53. end
  54.  
  55. --
  56. -- convert int array to byte array
  57. --
  58. function public.intsToBytes(ints, output, outputOffset, n)
  59.     n = n or #ints;
  60.     for i = 0, n do
  61.         for j = 0,3 do
  62.             output[outputOffset + i*4 + (3 - j)] = public.getByte(ints[i], j);
  63.         end
  64.     end
  65.     return output;
  66. end
  67.  
  68. --
  69. -- convert bytes to hexString
  70. --
  71. function private.bytesToHex(bytes)
  72.     local hexBytes = "";
  73.    
  74.     for i,byte in ipairs(bytes) do
  75.         hexBytes = hexBytes .. string.format("%02x ", byte);
  76.     end
  77.  
  78.     return hexBytes;
  79. end
  80.  
  81. --
  82. -- convert data to hex string
  83. --
  84. function public.toHexString(data)
  85.     local type = type(data);
  86.     if (type == "number") then
  87.         return string.format("%08x",data);
  88.     elseif (type == "table") then
  89.         return private.bytesToHex(data);
  90.     elseif (type == "string") then
  91.         local bytes = {string.byte(data, 1, #data)};
  92.  
  93.         return private.bytesToHex(bytes);
  94.     else
  95.         return data;
  96.     end
  97. end
  98.  
  99. function public.padByteString(data)
  100.     local dataLength = #data;
  101.    
  102.     local random1 = math.random(0,255);
  103.     local random2 = math.random(0,255);
  104.  
  105.     local prefix = string.char(random1,
  106.                                random2,
  107.                                random1,
  108.                                random2,
  109.                                public.getByte(dataLength, 3),
  110.                                public.getByte(dataLength, 2),
  111.                                public.getByte(dataLength, 1),
  112.                                public.getByte(dataLength, 0));
  113.  
  114.     data = prefix .. data;
  115.  
  116.     local paddingLength = math.ceil(#data/16)*16 - #data;
  117.     local padding = "";
  118.     for i=1,paddingLength do
  119.         padding = padding .. string.char(math.random(0,255));
  120.     end
  121.  
  122.     return data .. padding;
  123. end
  124.  
  125. function private.properlyDecrypted(data)
  126.     local random = {string.byte(data,1,4)};
  127.  
  128.     if (random[1] == random[3] and random[2] == random[4]) then
  129.         return true;
  130.     end
  131.    
  132.     return false;
  133. end
  134.  
  135. function public.unpadByteString(data)
  136.     if (not private.properlyDecrypted(data)) then
  137.         return nil;
  138.     end
  139.  
  140.     local dataLength = public.putByte(string.byte(data,5), 3)
  141.                      + public.putByte(string.byte(data,6), 2)
  142.                      + public.putByte(string.byte(data,7), 1)    
  143.                      + public.putByte(string.byte(data,8), 0);
  144.    
  145.     return string.sub(data,9,8+dataLength);
  146. end
  147.  
  148. function public.xorIV(data, iv)
  149.     for i = 1,16 do
  150.         data[i] = bit.bxor(data[i], iv[i]);
  151.     end
  152. end
  153.  
  154. return public;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement