Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Key Encryption/Decryption PaymentOption
- 26 November 2012
- Black Wolf Server
- This program employs a method of encrypting strings using
- an algorithm that will behave differently given a specific
- string, or key.
- In respect, the decryption of a string will yield different
- results depending on the key given.
- Key model: 10 letter alpha-numeric key generated randomly
- by using a random number between 0 and 9 and a
- random character between a and z.
- ]]--
- -- Generates a random 10 character alpha-numeric key using a random
- -- number between 0 and 9, and a random letter between a and z.
- -- Params : nil
- -- Returns: key - The key of type string that is generated.
- function generateKey()
- -- Reseed the generator.
- math.randomseed(math.random(1, 100000000))
- -- Reseed the generator with the current time so the key generation
- -- should be even more difficult to recreate.
- local characters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
- 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
- 'u', 'v', 'w', 'x', 'y', 'z'
- }
- local key = ""
- for index = 1, 5 do
- key = key .. math.random(0, 9) .. characters[math.random(1, 26)]
- end
- return key
- end
- -- Encrypts a given string based on the key given. Remember, the encryption
- -- algorithm will behave differently based on what key is passed as a
- -- parameter.
- -- Params : text, key - The text to encrypt and the key to encrypt it with.
- -- Returns: encryptedText - The text that will have been encrypted.
- function encrypt(text, key)
- text = tostring(text)
- local encryptedText = ""
- local randomSeed = 0
- for index = 1, key:len() do
- if tonumber(key:sub(index, index)) then
- randomSeed = randomSeed + tonumber(key:sub(index, index)) * index
- end
- end
- math.randomseed(randomSeed)
- local randomNumber = math.random(1, 20) * 5
- for index = 1, text:len() do
- encryptedText = encryptedText .. string.char(math.floor(text:byte(index) + randomNumber))
- end
- -- Reseed the generator.
- math.randomseed(math.random(1, 100000000))
- return encryptedText
- end
- -- Decrypts a given encrypted string based on the key given. The decryption
- -- will vary depending on what key was passed as well, so the decryption
- -- will rely on the fact that you have the correct key for decryption.
- -- Params : text, key - The text to decrypt and the key to decrypt it with.
- -- Returns: decryptedText - The text that will have been decrypted.
- function decrypt(text, key)
- local text = tostring(text)
- local decryptedText = ""
- local randomSeed = 0
- for index = 1, key:len() do
- if tonumber(key:sub(index, index)) then
- randomSeed = randomSeed + tonumber(key:sub(index, index)) * index
- end
- end
- math.randomseed(randomSeed)
- local randomNumber = math.random(1, 20) * 5
- for index = 1, text:len() do
- decryptedText = decryptedText .. string.char(text:byte(index) - randomNumber)
- end
- -- Reseed the generator.
- math.randomseed(math.random(1, 100000000))
- return decryptedText
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement