Advertisement
Guest User

ROT

a guest
Nov 1st, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import sys
  3. import getopt
  4.  
  5. class ROT(object):
  6.  
  7.     def __init__(self):
  8.         self._characterList = self._generate()
  9.  
  10.     def _generate(self):
  11.         '''
  12.         Generate a list of ASCII characters resembling a-z.
  13.         '''
  14.         start = chr(ord("a"))
  15.         end = chr(ord("z"))
  16.         chacterList = []
  17.  
  18.         current = start
  19.         while current is not end:
  20.             chacterList.append(current)
  21.             current = chr(ord(current) + 1) # Shift ASCII character one space
  22.         chacterList.append(current)
  23.        
  24.         return chacterList
  25.  
  26.     def shift(self, cypher, increment):
  27.         '''
  28.         Using the characterSet as reference, shift every ASCII character in the
  29.         cypher by the given increment.
  30.         '''
  31.         register = {}
  32.         for ascii in self._characterList:
  33.             register[ord(ascii)] = ascii
  34.        
  35.         plainText = []
  36.         for ascii in cypher:
  37.             ascii = ascii.lower() # Turn all to lowercase.
  38.  
  39.             cyph = register.get(ord(ascii) + increment, None)
  40.             if not cyph and ord(ascii) != 32: # ASCII 32 is space.
  41.                 cyph = register.get((ord(ascii) - 25) + (increment - 1)) # When end of list is reached, jump back to start.
  42.             elif ord(ascii) == 32:
  43.                 cyph = " "
  44.  
  45.             if not cyph:
  46.                 pass
  47.             else:
  48.                 plainText.append(cyph)
  49.         return plainText
  50.  
  51.     def length(self):
  52.         '''
  53.         Return the length of the characterset.
  54.         '''
  55.         return len(self._characterList)
  56.    
  57.  
  58. usage_doc = '''ROT encryption implementation.
  59. Usage:
  60.     ./rot.py -c <cypher>
  61. Note:
  62.     Current implementation does not allow for any special characters apart from spaces.
  63.     '''
  64. def usage():
  65.     print usage_doc
  66.  
  67. def main():
  68.     try:
  69.         long_options = ['help', 'cypher=']
  70.         opts, _ = getopt.getopt(sys.argv[1:], "hc:", long_options)
  71.     except getopt.GetoptError:
  72.         usage()
  73.         return -1
  74.  
  75.     cypher = ""
  76.     for o, a in opts:
  77.         if o in ('-h', '--help'):
  78.             usage()
  79.             return 0
  80.         if o in ('-c', '--cypher='):
  81.             cypher = a
  82.  
  83.     # Example: hello world (cycle 8)
  84.     if not len(cypher):
  85.         cypher = "zwddg ogjdv!"
  86.     rot = ROT()
  87.  
  88.     # Do an increment for every character in the lower-case alphabet.
  89.     increment = 0
  90.     while increment is not rot.length():
  91.         plainText = rot.shift(cypher, increment)
  92.         plainString = str(increment) + ": " + ''.join(plainText)
  93.         print plainString
  94.         increment = increment + 1
  95.  
  96.     plainText = rot.shift(cypher, increment)
  97.     plainString = str(increment) + ": " + ''.join(plainText)
  98.     print plainString
  99.  
  100. if __name__ == "__main__":
  101.     err_code = main()
  102.     sys.exit(err_code)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement