Repository navigation
Expand file tree
/
Copy pathcaesar.py
More file actions
63 lines (52 loc) · 1.37 KB
/
Copy pathcaesar.py
File metadata and controls
63 lines (52 loc) · 1.37 KB
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
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# date: 2016-11-25
# copyright © Philip R. Huffman 2016 all rights reserved.
"""
uses the caesar method to encrypt a string.
"""
from sys import argv
from helpers import rotate_character
__author__ = 'Philip R. Huffman'
def decrypt(cypher_text, shift_amount):
"""
convert cypher text to plain text
:param cypher_text: string
:param shift_amount: int
:return: string
"""
plain_text = ''
for char in cypher_text:
plain_text += rotate_character(char, 26 - shift_amount)
return plain_text
def encrypt(plain_text, shift_amount=13):
"""
convert plain text to cypher text
:param plain_text: string
:param shift_amount: integer
:return: string
"""
cypher_text = ''
for char in plain_text:
cypher_text += rotate_character(char, shift_amount)
return cypher_text
def main():
"""
for use from terminal
:return: none
"""
if user_input_is_valid(argv):
rotation_number = int(argv[1])
plain = input('Type a message: ')
print('{}'.format(encrypt(plain, rotation_number)))
else:
print('usage: python3 caesar.py n')
def user_input_is_valid(args):
"""
validates terminal input
:param args: list
:return: bool
"""
return len(args) == 2 and args[1].isdigit()
if __name__ == '__main__':
main()