Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from sys import argv, exit
- from string import lowercase, uppercase, maketrans
- from random import randint
- import check_args
- def get_new_alpha(key):
- temp = []
- for each in range(0,key):
- temp.append(lowercase[each])
- rest = ''.join(temp)
- new = lowercase[key:26] + rest
- return new
- def get_new_ALPHA(key):
- temp = []
- for each in range(0,key):
- temp.append(uppercase[each])
- rest = ''.join(temp)
- new = uppercase[key:26] + rest
- return new
- def encrypt(source, key):
- intab = lowercase[:26]
- outtab = get_new_alpha(key)
- trantab = maketrans(intab, outtab)
- first_pass = source.translate(trantab)
- intab = uppercase[:26]
- outtab = get_new_ALPHA(key)
- trantab = maketrans(intab,outtab)
- return first_pass.translate(trantab)
- def decrypt(source, key):
- outtab = lowercase[:26]
- intab = get_new_alpha(key)
- trantab = maketrans(intab, outtab)
- first_pass = source.translate(trantab)
- outtab = uppercase[:26]
- intab = get_new_ALPHA(key)
- trantab = maketrans(intab, outtab)
- return first_pass.translate(trantab)
- args = check_args.check_args(4)
- cmd = string = key = False
- #--------------------------------------------------
- #These check for present argv's and set them.
- if args[1]:
- cmd = argv[1]
- if args[2]:
- string = argv[2]
- if args[3]:
- key = int(argv[3])
- #--------------------------------------------------
- #If no argvs are passed, then we set them here
- if not cmd:
- print "1:\tEncrypt\n2:\tDecrypt"
- ask = raw_input("> ")
- if ask == '1' or ask == 'encrypt':
- cmd = 'encrypt'
- if ask == '2' or ask == 'decrypt':
- cmd = 'decrypt'
- if not string:
- print "Input the name of the file that you would like to %s OR" % cmd
- print "Input the string you would like to %s." % cmd
- string = raw_input("> ")
- #This checks if the string is a file or just a string, and saves it as a raw text for later use.
- try:
- open(string,'r')
- print "WARNING:\n\tThis will encrypt the contents of this file."
- safety = raw_input("Are you sure about this?:\n")
- if safety == 'no':
- exit(0)
- file = True
- except IOError:
- string = string
- file = False
- if not key:
- print "What is the key?"
- key = int(raw_input("> "))
- #--------------------------------------------------
- def test_input():
- print "cmd = %r\nstring = %r\nkey = %r\nfile = %r" % (cmd, string, key,file)
- def handle():
- if file == False:
- if cmd == 'encrypt':
- print encrypt(string, key)
- if cmd == 'decrypt' :
- print decrypt(string, key)
- if file == True:
- original = open(string, 'r').read()
- txt = open(string, 'w')
- if cmd == 'encrypt':
- encrypted_text = encrypt(original,key)
- txt.write(encrypted_text)
- if cmd == 'decrypt':
- decrypted_text = decrypt(original, key)
- txt.write(decrypted_text)
- txt.close
- if __name__ == '__main__':
- handle()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement