Advertisement
Alakazard12

cyph

Apr 27th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.68 KB | None | 0 0
  1. local aes = loadfile("aes")()
  2. local aesutil = loadfile("util")()
  3. local buffer = loadfile("buffer")()
  4.  
  5. local public = {};
  6.  
  7. --
  8. -- Encrypt strings
  9. -- key - byte array with key
  10. -- string - string to encrypt
  11. -- modefunction - function for cipher mode to use
  12. --
  13. function public.encryptString(key, data, modeFunction)
  14.     local iv = iv or {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  15.     local keySched = aes.expandEncryptionKey(key);
  16.     local encryptedData = buffer.new();
  17.    
  18.     for i = 1, #data/16 do
  19.         local offset = (i-1)*16 + 1;
  20.         local byteData = {string.byte(data,offset,offset +15)};
  21.                
  22.         modeFunction(keySched, byteData, iv);
  23.  
  24.         buffer.addString(encryptedData, string.char(unpack(byteData)));    
  25.     end
  26.    
  27.     return buffer.toString(encryptedData);
  28. end
  29.  
  30. --
  31. -- the following 4 functions can be used as
  32. -- modefunction for encryptString
  33. --
  34.  
  35. -- Electronic code book mode encrypt function
  36. function public.encryptECB(keySched, byteData, iv)
  37.         aes.encrypt(keySched, byteData, 1, byteData, 1);
  38. end
  39.  
  40. -- Cipher block chaining mode encrypt function
  41. function public.encryptCBC(keySched, byteData, iv)
  42.     aesutil.xorIV(byteData, iv);
  43.  
  44.     aes.encrypt(keySched, byteData, 1, byteData, 1);    
  45.        
  46.     for j = 1,16 do
  47.         iv[j] = byteData[j];
  48.     end
  49. end
  50.  
  51. -- Output feedback mode encrypt function
  52. function public.encryptOFB(keySched, byteData, iv)
  53.     aes.encrypt(keySched, iv, 1, iv, 1);
  54.     aesutil.xorIV(byteData, iv);
  55. end
  56.  
  57. -- Cipher feedback mode encrypt function
  58. function public.encryptCFB(keySched, byteData, iv)
  59.     aes.encrypt(keySched, iv, 1, iv, 1);    
  60.     aesutil.xorIV(byteData, iv);
  61.        
  62.     for j = 1,16 do
  63.         iv[j] = byteData[j];
  64.     end        
  65. end
  66.  
  67. --
  68. -- Decrypt strings
  69. -- key - byte array with key
  70. -- string - string to decrypt
  71. -- modefunction - function for cipher mode to use
  72. --
  73. function public.decryptString(key, data, modeFunction)
  74.     local iv = iv or {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  75.    
  76.     local keySched;
  77.     if (modeFunction == public.decryptOFB or modeFunction == public.decryptCFB) then
  78.         keySched = aes.expandEncryptionKey(key);
  79.         else
  80.                 keySched = aes.expandDecryptionKey(key);
  81.     end
  82.    
  83.     local decryptedData = buffer.new();
  84.  
  85.     for i = 1, #data/16 do
  86.         local offset = (i-1)*16 + 1;
  87.         local byteData = {string.byte(data,offset,offset +15)};
  88.  
  89.                 iv = modeFunction(keySched, byteData, iv);
  90.  
  91.         buffer.addString(decryptedData, string.char(unpack(byteData)));
  92.     end
  93.  
  94.     return buffer.toString(decryptedData);    
  95. end
  96.  
  97. --
  98. -- the following 4 functions can be used as
  99. -- modefunction for decryptString
  100. --
  101.  
  102. -- Electronic code book mode decrypt function
  103. function public.decryptECB(keySched, byteData, iv)
  104.  
  105.     aes.decrypt(keySched, byteData, 1, byteData, 1);
  106.    
  107.     return iv;
  108. end
  109.  
  110. -- Cipher block chaining mode decrypt function
  111. function public.decryptCBC(keySched, byteData, iv)
  112.         local nextIV = {};
  113.     for j = 1,16 do
  114.         nextIV[j] = byteData[j];
  115.     end
  116.        
  117.     aes.decrypt(keySched, byteData, 1, byteData, 1);    
  118.     aesutil.xorIV(byteData, iv);
  119.  
  120.         return nextIV;
  121. end
  122.  
  123. -- Output feedback mode decrypt function
  124. function public.decryptOFB(keySched, byteData, iv)
  125.     aes.encrypt(keySched, iv, 1, iv, 1);
  126.     aesutil.xorIV(byteData, iv);
  127.    
  128.     return iv;
  129. end
  130.  
  131. -- Cipher feedback mode decrypt function
  132. function public.decryptCFB(keySched, byteData, iv)
  133.     local nextIV = {};
  134.     for j = 1,16 do
  135.         nextIV[j] = byteData[j];
  136.     end
  137.  
  138.     aes.encrypt(keySched, iv, 1, iv, 1);
  139.        
  140.     aesutil.xorIV(byteData, iv);
  141.    
  142.     return nextIV;
  143. end
  144.  
  145. return public;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement