-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathVigenere.py
129 lines (101 loc) · 3.68 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# *********
# -*- Made by VoxelPixel
# -*- For YouTube Tutorial
# -*- https://github.com/VoxelPixel
# -*- Support me on Patreon: https://www.patreon.com/voxelpixel
# *********
def msg_and_key():
msg = input("Enter message: ").upper()
key = input("Enter key: ").upper()
# variable to store mapped key
key_map = ""
j=0
for i in range(len(msg)):
if ord(msg[i]) == 32:
# ignoring space
key_map += " "
else:
if j < len(key):
key_map += key[j]
j += 1
else:
j = 0
key_map += key[j]
j += 1
# print(key_map)
return msg, key_map
def create_vigenere_table():
table = []
for i in range(26):
table.append([])
for row in range(26):
for column in range(26):
if (row + 65) + column > 90:
# moving back to A after Z
# after first row, each letter will shift left by one position compared to row above it
table[row].append(chr((row+65) + column - 26))
else:
# after first row, each letter will shift left by one position compared to row above it
table[row].append(chr((row+65)+column))
# printing the table
# for row in table:
# for column in row:
# print(column, end=" ")
# print(end="\n")
return table
def cipher_encryption(message, mapped_key):
table = create_vigenere_table()
encrypted_text = ""
for i in range(len(message)):
if message[i] == chr(32):
# ignoring space
encrypted_text += " "
else:
# getting element at specific index of table
row = ord(message[i])-65
column = ord(mapped_key[i]) - 65
encrypted_text += table[row][column]
print("Encrypted Message: {}".format(encrypted_text))
def itr_count(mapped_key, message):
counter = 0
result = ""
# starting alphabets from mapped_key letter and finishing all 26 letters from it, (after z we move to a)
for i in range(26):
if mapped_key + i > 90:
result += chr(mapped_key+(i-26))
else:
result += chr(mapped_key+i)
# counting the number of iterations it take from mapped key letter to ciphertext letter
for i in range(len(result)):
if result[i] == chr(message):
break
else:
counter += 1
return counter
def cipher_decryption(message, mapped_key):
table = create_vigenere_table()
decrypted_text = ""
for i in range(len(message)):
if message[i] == chr(32):
# ignoring space
decrypted_text += " "
else:
# adding number of iterations, it takes to reach from mapped key letter to cipher letter in 65
# by doing so we get column header of ciphertext letter, which happens to be decrypted letter
decrypted_text += chr(65 + itr_count(ord(mapped_key[i]), ord(message[i])))
print("Decrypted Message: {}".format(decrypted_text))
def main():
print("Key and Message can only be alphabetic")
choice = int(input("1. Encryption\n2. Decryption\nChoose(1,2): "))
if choice == 1:
print("---Encryption---")
message, mapped_key = msg_and_key()
cipher_encryption(message, mapped_key)
elif choice == 2:
print("---Decryption---")
message, mapped_key = msg_and_key()
cipher_decryption(message, mapped_key)
else:
print("Wrong choice")
if __name__ == "__main__":
main()