Advertisement
luisnaranjo733

Caesar Cipher

Feb 15th, 2012
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from sys import argv, exit
  4. from string import lowercase, uppercase, maketrans
  5. from random import randint
  6. import check_args
  7.  
  8. def get_new_alpha(key):
  9.     temp = []
  10.     for each in range(0,key):
  11.         temp.append(lowercase[each])
  12.  
  13.     rest = ''.join(temp)
  14.     new =  lowercase[key:26] + rest
  15.     return new
  16.  
  17. def get_new_ALPHA(key):
  18.     temp = []
  19.     for each in range(0,key):
  20.         temp.append(uppercase[each])
  21.  
  22.     rest = ''.join(temp)
  23.     new =  uppercase[key:26] + rest
  24.     return new
  25.  
  26. def encrypt(source, key):
  27.     intab = lowercase[:26]
  28.     outtab = get_new_alpha(key)
  29.     trantab = maketrans(intab, outtab)
  30.  
  31.     first_pass = source.translate(trantab)
  32.     intab = uppercase[:26]
  33.     outtab = get_new_ALPHA(key)
  34.     trantab = maketrans(intab,outtab)
  35.  
  36.     return first_pass.translate(trantab)
  37.  
  38.  
  39. def decrypt(source, key):
  40.     outtab = lowercase[:26]
  41.     intab = get_new_alpha(key)
  42.     trantab = maketrans(intab, outtab)
  43.  
  44.     first_pass = source.translate(trantab)
  45.  
  46.     outtab = uppercase[:26]
  47.     intab = get_new_ALPHA(key)
  48.     trantab = maketrans(intab, outtab)
  49.  
  50.     return first_pass.translate(trantab)
  51.  
  52.  
  53. args = check_args.check_args(4)
  54.  
  55. cmd = string = key = False
  56. #--------------------------------------------------
  57. #These check for present argv's and set them.
  58. if args[1]:
  59.     cmd = argv[1]
  60.  
  61. if args[2]:
  62.     string = argv[2]
  63.  
  64. if args[3]:
  65.     key = int(argv[3])
  66. #--------------------------------------------------
  67. #If no argvs are passed, then we set them here
  68. if not cmd:
  69.     print "1:\tEncrypt\n2:\tDecrypt"
  70.     ask = raw_input("> ")
  71.     if ask == '1' or ask == 'encrypt':
  72.         cmd = 'encrypt'
  73.  
  74.     if ask == '2' or ask == 'decrypt':
  75.         cmd = 'decrypt'
  76.  
  77. if not string:
  78.  
  79.     print "Input the name of the file that you would like to %s OR" % cmd
  80.     print "Input the string you would like to %s." % cmd
  81.     string = raw_input("> ")
  82.  
  83. #This checks if the string is a file or just a string, and saves it as a raw text for later use.
  84. try:
  85.     open(string,'r')
  86.     print "WARNING:\n\tThis will encrypt the contents of this file."
  87.     safety = raw_input("Are you sure about this?:\n")
  88.     if safety == 'no':
  89.         exit(0)
  90.     file = True
  91. except IOError:
  92.     string = string
  93.     file = False
  94.  
  95. if not key:
  96.     print "What is the key?"
  97.     key = int(raw_input("> "))
  98. #--------------------------------------------------
  99. def test_input():
  100.     print "cmd = %r\nstring = %r\nkey = %r\nfile = %r" % (cmd, string, key,file)
  101.  
  102. def handle():
  103.     if file == False:
  104.         if cmd == 'encrypt':
  105.             print encrypt(string, key)
  106.  
  107.         if cmd == 'decrypt' :
  108.             print decrypt(string, key)
  109.  
  110.     if file == True:
  111.         original = open(string, 'r').read()
  112.         txt = open(string, 'w')
  113.  
  114.         if cmd == 'encrypt':
  115.             encrypted_text = encrypt(original,key)
  116.             txt.write(encrypted_text)
  117.  
  118.         if cmd == 'decrypt':
  119.             decrypted_text = decrypt(original, key)
  120.             txt.write(decrypted_text)
  121.  
  122.         txt.close
  123. if __name__ == '__main__':
  124.     handle()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement