-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvigenere.py
49 lines (42 loc) · 1.7 KB
/
vigenere.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
from helpers import alphabet_position, rotate_character
from sys import argv, exit
def encrypt(user_text, user_key):
# Create variables
encrypted_message = ''
key_index = 0
# Change user_key into list of ordinal values
ord_user_key = []
for char in user_key:
char = char.lower()
char_rotate = chr(ord(char) - ord('a'))
ord_user_key.append(ord(char_rotate))
# Loop through user_text
for char in user_text:
# Counter to loop through key index - modulo in range of key length
key_index = key_index % (len(user_key))
if char.isalpha() == True:
encrypted_message += rotate_character(char, ord_user_key[key_index])
key_index += 1
else:
encrypted_message += rotate_character(char, ord_user_key[key_index])
return encrypted_message
def main():
#Validate argv is only alphabetic characters
for char in argv[1]:
if char.isalpha() == False:
print('''usage: python vigenere.py keyword
Arguments:
-keyword : The string to be used as a "key" to encrypt your message. Should only contain alphabetic characters-- no numbers or special characters.''')
exit()
#Validate number of arguments passed into program
if len(argv) != 2:
print('''usage: python vigenere.py keyword
Arguments:
-keyword : The string to be used as a "key" to encrypt your message. Should only contain alphabetic characters-- no numbers or special characters.''')
exit()
else:
user_text = input("Enter the message you want to encrypt: ")
#user_key = input("Enter your secret cipher key: ")
print(encrypt(user_text, argv[1]))
if __name__ == "__main__":
main()