-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.py
53 lines (42 loc) · 1.65 KB
/
translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python
# translate.py Encrypt and decrypt text with ciphers.
#
# Usage:
# $ python3 translate.py caesar --key 3 > encrypted.txt
# $ python3 translate.py caesar --decrypt --key 3 < encrypted.txt
import argparse
import logging
import sys
import time
from ciphers.utils import import_cipher_module
def parse_args(prog='translate', description='Translator.'):
parser = argparse.ArgumentParser(prog=prog, description=description)
parser.add_argument('-d', dest='debug', action='store_true')
parser.add_argument('cipher', help='cipher name')
parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
default=sys.stdin)
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
default=sys.stdout)
parser.add_argument('--key', help='Key')
parser.add_argument('--decrypt', action='store_true')
return parser.parse_args()
def main():
args = parse_args()
if args.debug:
logging.basicConfig(level=logging.DEBUG)
message = args.infile.read()
cipher = import_cipher_module(args.cipher)
if not args.key:
args.key = cipher.get_random_key(message)
print('Using random key {}'.format(args.key), file=sys.stderr)
start_time = time.time()
if not args.decrypt:
mode = 'encrypt'
print(cipher.encrypt(args.key, message), file=args.outfile)
else:
mode = 'decrypt'
print(cipher.decrypt(args.key, message), file=args.outfile)
total_time = round(time.time() - start_time, 2)
print('{}ion time: {} seconds'.format(mode, total_time), file=sys.stderr)
if __name__ == '__main__':
main()