-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
61 lines (44 loc) · 1.49 KB
/
main.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
54
55
56
def check_key(input_message, key):
if len(input_message) == len(key):
return key
for i in range(len(input_message) - len(key)):
key += key[i % len(key)]
return key
# encryption vvv
def encrypt_vigenere(input_message, key):
if not check_case(input_message, key):
return 'Enter the message and the key using only upper case'
key = check_key(input_message, key)
output = ''
for i in range(len(input_message)):
if input_message[i] == ' ':
output += ' '
continue
temp = (ord(input_message[i]) + ord(key[i])) % 26
temp += ord('A')
output += chr(temp)
return output
# decryption vvv
def decrypt_vigenere(input_message, key):
if not check_case(input_message, key):
return 'Enter the message and the key using only upper case'
key = check_key(input_message, key)
output = ''
for i in range(len(input_message)):
if input_message[i] == ' ':
output += ' '
continue
temp = (ord(input_message[i]) - ord(key[i]) + 26) % 26
temp += ord('A')
output += chr(temp)
return output
def check_case(input_message, key):
for i in range(len(input_message)):
if input_message[i].islower():
return False
for i in range(len(key)):
if input_message[i].islower():
return False
return True
print(encrypt_vigenere('HELLO THERE', 'DISTLAB'))
print(decrypt_vigenere('KMDEZ UKMJX', 'DISTLAB'))