Advertisement
Trystan_C_C

Basic Lua Encryption

Nov 26th, 2012
2,986
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.64 KB | None | 0 0
  1. --[[
  2.         Key Encryption/Decryption       PaymentOption
  3.                                         26 November 2012
  4.                                         Black Wolf Server
  5.        
  6.         This program employs a method of encrypting strings using
  7.         an algorithm that will behave differently given a specific
  8.         string, or key.
  9.        
  10.         In respect, the decryption of a string will yield different
  11.         results depending on the key given.
  12.        
  13.         Key model: 10 letter alpha-numeric key generated randomly
  14.                    by using a random number between 0 and 9 and a
  15.                    random character between a and z.
  16. ]]--
  17.  
  18. -- Generates a random 10 character alpha-numeric key using a random
  19. -- number between 0 and 9, and a random letter between a and z.
  20. -- Params : nil
  21. -- Returns: key - The key of type string that is generated.
  22. function generateKey()
  23.         -- Reseed the generator.
  24.         math.randomseed(math.random(1, 100000000))
  25.        
  26.         -- Reseed the generator with the current time so the key generation
  27.         -- should be even more difficult to recreate.
  28.         local characters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  29.                             'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  30.                             'u', 'v', 'w', 'x', 'y', 'z'
  31.                            }
  32.         local key = ""
  33.         for index = 1, 5 do
  34.                 key = key .. math.random(0, 9) .. characters[math.random(1, 26)]
  35.         end
  36.        
  37.         return key
  38. end
  39.  
  40. -- Encrypts a given string based on the key given. Remember, the encryption
  41. -- algorithm will behave differently based on what key is passed as a
  42. -- parameter.
  43. -- Params : text, key - The text to encrypt and the key to encrypt it with.
  44. -- Returns: encryptedText - The text that will have been encrypted.
  45. function encrypt(text, key)
  46.         text = tostring(text)
  47.         local encryptedText = ""
  48.         local randomSeed = 0
  49.        
  50.         for index = 1, key:len() do
  51.                 if tonumber(key:sub(index, index)) then
  52.                         randomSeed = randomSeed + tonumber(key:sub(index, index)) * index
  53.                 end
  54.         end
  55.         math.randomseed(randomSeed)
  56.        
  57.         local randomNumber = math.random(1, 20) * 5
  58.         for index = 1, text:len() do
  59.                 encryptedText = encryptedText .. string.char(math.floor(text:byte(index) + randomNumber))
  60.         end
  61.        
  62.         -- Reseed the generator.
  63.         math.randomseed(math.random(1, 100000000))
  64.         return encryptedText
  65. end
  66.  
  67. -- Decrypts a given encrypted string based on the key given. The decryption
  68. -- will vary depending on what key was passed as well, so the decryption
  69. -- will rely on the fact that you have the correct key for decryption.
  70. -- Params : text, key - The text to decrypt and the key to decrypt it with.
  71. -- Returns: decryptedText - The text that will have been decrypted.
  72. function decrypt(text, key)
  73.         local text = tostring(text)
  74.         local decryptedText = ""
  75.         local randomSeed = 0
  76.        
  77.         for index = 1, key:len() do
  78.                 if tonumber(key:sub(index, index)) then
  79.                         randomSeed = randomSeed + tonumber(key:sub(index, index)) * index
  80.                 end
  81.         end
  82.         math.randomseed(randomSeed)
  83.        
  84.         local randomNumber = math.random(1, 20) * 5
  85.         for index = 1, text:len() do
  86.                 decryptedText = decryptedText .. string.char(text:byte(index) - randomNumber)
  87.         end
  88.        
  89.         -- Reseed the generator.
  90.         math.randomseed(math.random(1, 100000000))
  91.         return decryptedText
  92. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement