Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import sys
- import getopt
- class ROT(object):
- def __init__(self):
- self._characterList = self._generate()
- def _generate(self):
- '''
- Generate a list of ASCII characters resembling a-z.
- '''
- start = chr(ord("a"))
- end = chr(ord("z"))
- chacterList = []
- current = start
- while current is not end:
- chacterList.append(current)
- current = chr(ord(current) + 1) # Shift ASCII character one space
- chacterList.append(current)
- return chacterList
- def shift(self, cypher, increment):
- '''
- Using the characterSet as reference, shift every ASCII character in the
- cypher by the given increment.
- '''
- register = {}
- for ascii in self._characterList:
- register[ord(ascii)] = ascii
- plainText = []
- for ascii in cypher:
- ascii = ascii.lower() # Turn all to lowercase.
- cyph = register.get(ord(ascii) + increment, None)
- if not cyph and ord(ascii) != 32: # ASCII 32 is space.
- cyph = register.get((ord(ascii) - 25) + (increment - 1)) # When end of list is reached, jump back to start.
- elif ord(ascii) == 32:
- cyph = " "
- if not cyph:
- pass
- else:
- plainText.append(cyph)
- return plainText
- def length(self):
- '''
- Return the length of the characterset.
- '''
- return len(self._characterList)
- usage_doc = '''ROT encryption implementation.
- Usage:
- ./rot.py -c <cypher>
- Note:
- Current implementation does not allow for any special characters apart from spaces.
- '''
- def usage():
- print usage_doc
- def main():
- try:
- long_options = ['help', 'cypher=']
- opts, _ = getopt.getopt(sys.argv[1:], "hc:", long_options)
- except getopt.GetoptError:
- usage()
- return -1
- cypher = ""
- for o, a in opts:
- if o in ('-h', '--help'):
- usage()
- return 0
- if o in ('-c', '--cypher='):
- cypher = a
- # Example: hello world (cycle 8)
- if not len(cypher):
- cypher = "zwddg ogjdv!"
- rot = ROT()
- # Do an increment for every character in the lower-case alphabet.
- increment = 0
- while increment is not rot.length():
- plainText = rot.shift(cypher, increment)
- plainString = str(increment) + ": " + ''.join(plainText)
- print plainString
- increment = increment + 1
- plainText = rot.shift(cypher, increment)
- plainString = str(increment) + ": " + ''.join(plainText)
- print plainString
- if __name__ == "__main__":
- err_code = main()
- sys.exit(err_code)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement