MetaDark

Encryption / Decryption API

Nov 4th, 2012
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.81 KB | None | 0 0
  1. --[[
  2. MDCrypt 1.0
  3.  
  4. MDCrypt is a simple and fast encryption/decryption API which allows data to be saved and transferred with an encryption key.
  5.  
  6. Copyright (C) 2012 MetaDark
  7.  
  8. This program is free software: you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation, either version 3 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20. ]]--
  21.  
  22. local charSize = 255
  23. local amplitude = charSize/math.pi
  24. local frequency = math.pi/charSize*8
  25.  
  26. -- MetaDark Offset Algorithm 1 (Not complicated but very hard to crack) --
  27. function MDOA1(x)
  28.     return math.ceil(amplitude * math.sin(frequency * x) + (amplitude*(math.cos(x) % 9)))
  29. end
  30.  
  31. -- Convert letters into numbers --
  32. function stringToInt(input)
  33.     if type(input) == "number" then
  34.         return input
  35.     elseif type(input) == "string" then
  36.         output = 0
  37.         for x = 1, #input do
  38.             output = output + input:byte(x)
  39.         end
  40.        
  41.         return output
  42.     else
  43.         return 0
  44.     end
  45. end
  46.  
  47. -- Encrypt String --
  48. function encrypt(input, key)
  49.     key = stringToInt(key)
  50.    
  51.     local output = ""
  52.     for x = 1, #input do
  53.         output = output .. string.char((input:byte(x) + MDOA1(x + key)) % charSize)
  54.     end
  55.    
  56.     return output
  57. end
  58.  
  59. -- Decrypt String --
  60. function decrypt(input, key)
  61.     key = stringToInt(key)
  62.    
  63.     local output = ""
  64.     for x = 1, #input do
  65.         output = output .. string.char((input:byte(x) - MDOA1(x + key)) % charSize)
  66.     end
  67.    
  68.     return output
  69. end
Advertisement
Add Comment
Please, Sign In to add comment