Advertisement
theoriginalbit

Lua: Vigenere Cipher

Jan 17th, 2013
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.36 KB | None | 0 0
  1. local fCharacterMap = {}
  2. local fKeyIndex = 1
  3. local fKey = ""
  4. local CHARACTERS = 26
  5.  
  6. local function isLower( str )
  7.   return type(str) == "string" and str == str:lower() or false
  8. end
  9.  
  10. local function isAlpha( str )
  11.   return type(str) == "string" and (str:gsub("%a", "")) == "" or false
  12. end
  13.  
  14. local function vigenere( aKey )
  15.   for row = 1, CHARACTERS do
  16.     local lChar = 65 + row
  17.     if lChar > 90 then
  18.         lChar = 65
  19.     end
  20.     fCharacterMap[row] = {}
  21.     for col = 1, CHARACTERS do
  22.       fCharacterMap[row][col] = string.char( lChar )
  23.       lChar = lChar + 1
  24.       if lChar > 90 then
  25.         lChar = 65
  26.       end
  27.     end
  28.   end
  29.  
  30.   for i = 1, #aKey do
  31.     local str = aKey:sub( i, i )
  32.     if isAlpha( str ) then
  33.       fKey = fKey..str:upper()
  34.     end
  35.   end
  36.  
  37.   fKeyIndex = 1
  38. end
  39.  
  40. local function encode( aCharacter )
  41.   if isAlpha( aCharacter ) then
  42.     local islower = isLower( aCharacter )
  43.     aCharacter = aCharacter:upper()
  44.    
  45.     local keyChar = fKey:sub( fKeyIndex, fKeyIndex )
  46.     local lRow = keyChar:byte() - 64
  47.     local lColumn = aCharacter:byte() - 64
  48.    
  49.     aCharacter = fCharacterMap[lRow][lColumn]
  50.    
  51.     if islower then
  52.       aCharacter = aCharacter:lower()
  53.     end
  54.    
  55.     fKeyIndex = fKeyIndex + 1
  56.     if fKeyIndex == #fKey + 1 then
  57.       fKeyIndex = 1
  58.     end
  59.   end
  60.  
  61.   return aCharacter
  62. end
  63.  
  64. local function decode( aCharacter )
  65.   if isAlpha( aCharacter ) then
  66.     local islower = isLower( aCharacter )
  67.     aCharacter = aCharacter:upper()
  68.    
  69.     local keyChar = fKey:sub( fKeyIndex, fKeyIndex )
  70.     local lRow = keyChar:byte() - 64
  71.    
  72.     for lColumn = 1, CHARACTERS do
  73.       if aCharacter == fCharacterMap[lRow][lColumn] then
  74.         aCharacter = string.char( 64 + lColumn )
  75.         break
  76.       end
  77.     end
  78.    
  79.     if islower then
  80.       aCharacter = aCharacter:lower()
  81.     end
  82.    
  83.     fKeyIndex = fKeyIndex + 1
  84.     if fKeyIndex == fKey:len() + 1 then
  85.       fKeyIndex = 1
  86.     end
  87.   end
  88.  
  89.   return aCharacter
  90. end
  91.  
  92. function encodeString( aString, aKey )
  93.   vigenere( aKey )
  94.   result = ""
  95.  
  96.   for i = 1, #aString do
  97.     result = result..encode( aString:sub( i, i ) )
  98.   end
  99.  
  100.   return result
  101. end
  102.  
  103. function decodeString( aString, aKey )
  104.   vigenere( aKey )
  105.   result = ""
  106.  
  107.   for i = 1, #aString do
  108.     result = result..decode( aString:sub( i, i ) )
  109.   end
  110.  
  111.   return result
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement