Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local fCharacterMap = {}
- local fKeyIndex = 1
- local fKey = ""
- local CHARACTERS = 26
- local function isLower( str )
- return type(str) == "string" and str == str:lower() or false
- end
- local function isAlpha( str )
- return type(str) == "string" and (str:gsub("%a", "")) == "" or false
- end
- local function vigenere( aKey )
- for row = 1, CHARACTERS do
- local lChar = 65 + row
- if lChar > 90 then
- lChar = 65
- end
- fCharacterMap[row] = {}
- for col = 1, CHARACTERS do
- fCharacterMap[row][col] = string.char( lChar )
- lChar = lChar + 1
- if lChar > 90 then
- lChar = 65
- end
- end
- end
- for i = 1, #aKey do
- local str = aKey:sub( i, i )
- if isAlpha( str ) then
- fKey = fKey..str:upper()
- end
- end
- fKeyIndex = 1
- end
- local function encode( aCharacter )
- if isAlpha( aCharacter ) then
- local islower = isLower( aCharacter )
- aCharacter = aCharacter:upper()
- local keyChar = fKey:sub( fKeyIndex, fKeyIndex )
- local lRow = keyChar:byte() - 64
- local lColumn = aCharacter:byte() - 64
- aCharacter = fCharacterMap[lRow][lColumn]
- if islower then
- aCharacter = aCharacter:lower()
- end
- fKeyIndex = fKeyIndex + 1
- if fKeyIndex == #fKey + 1 then
- fKeyIndex = 1
- end
- end
- return aCharacter
- end
- local function decode( aCharacter )
- if isAlpha( aCharacter ) then
- local islower = isLower( aCharacter )
- aCharacter = aCharacter:upper()
- local keyChar = fKey:sub( fKeyIndex, fKeyIndex )
- local lRow = keyChar:byte() - 64
- for lColumn = 1, CHARACTERS do
- if aCharacter == fCharacterMap[lRow][lColumn] then
- aCharacter = string.char( 64 + lColumn )
- break
- end
- end
- if islower then
- aCharacter = aCharacter:lower()
- end
- fKeyIndex = fKeyIndex + 1
- if fKeyIndex == fKey:len() + 1 then
- fKeyIndex = 1
- end
- end
- return aCharacter
- end
- function encodeString( aString, aKey )
- vigenere( aKey )
- result = ""
- for i = 1, #aString do
- result = result..encode( aString:sub( i, i ) )
- end
- return result
- end
- function decodeString( aString, aKey )
- vigenere( aKey )
- result = ""
- for i = 1, #aString do
- result = result..decode( aString:sub( i, i ) )
- end
- return result
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement