-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathVernam_Cipher.py
137 lines (110 loc) · 4.17 KB
/
Vernam_Cipher.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
130
131
132
133
134
135
136
'''In Vernam cipher, also known as the One-Time Pad, the length of the
plaintext, ciphertext, and key is the same. A number is assigned to
each character of the plain-text and the key according to alphabetical
order. Both the number are added and it is greater than 26, then %26 is
taken to form the corresponding cipher-text.
'''
import sys
'''This function assigns a number from
[0-25] to characters [a-z] respectively.'''
def ascii_to_dec(text):
ascii_text = []
text = text.lower()
for i in text:
ascii_text.append(ord(i) - 97)
return ascii_text
'''This is the encryption function which takes plaintext
and keyword as input. It then converts both plaintext
and keyword to a number. Adds them. Take %26 to form
the corresponding ciphertext.'''
def encryption(message, keyword):
pad = ascii_to_dec(keyword)
mess = ascii_to_dec(message)
enc = []
for i in range(len(message)):
character = chr(((mess[i] + pad[i]) % 26) + 97)
if message[i].islower():
enc.append(character)
else:
enc.append(character.upper())
return ''.join(enc)
'''This is the decryption function which takes ciphertext
and keyword as input. It then converts both ciphertext
and keyword to a number. Subtracts them. Takes %26 to
form the corresponding plaintext'''
def decryption(ciphertext, keyword):
pad = ascii_to_dec(keyword)
cipher = ascii_to_dec(ciphertext)
dec = []
for i in range(len(ciphertext)):
character = chr(((cipher[i] - pad[i]) % 26) + 97)
if ciphertext[i].islower():
dec.append(character)
else:
dec.append(character.upper())
return ''.join(dec)
# Driver function
if __name__=='__main__':
print("-"*20, "Vernam Cipher", "-"*20)
print("1. Encrypt a plaintext")
print("2. Decrypt a ciphertext")
print("3. Exit")
# Taking choice from user
choice = int(input("Enter your choice: "))
if(choice == 3):
print("Exiting program.")
sys.exit(0)
# If the user chooses to encrypt a message
elif(choice == 1):
# Take plaintext and keyword as input
print("Plaintext and key must be of same length.")
plaintext = input("Enter a message you want to encrypt: ")
key = input("Enter a key to encrypt the message : ")
# Check if the length of keyword is same as plaintext or not
if(len(plaintext) != len(key)):
print("Please enter a valid input.")
sys.exit(0)
# Call the encrpytion function to encrypt the text
encrypted_message = encryption(plaintext, key)
# Print the encrypted message
print("Encrypted message is", " "*13, ":", encrypted_message)
# If the user chooses to decrypt a ciphertext
elif(choice == 2):
# Take ciphertext and keyword as input
print("Ciphertext and key must be of same length.")
ciphertext = input("Enter a ciphertext you want to decrypt: ")
key = input("Enter a key to decrypt the ciphertext : ")
# Check if length of keyword is equal to ciphertext or not
if(len(ciphertext) != len(key)):
print("Please enter a valid input.")
sys.exit(0)
# Call the decryption function to decrypt the ciphertext
decrypted_message = decryption(ciphertext, key)
# Print the decrypted ciphertext
print("Decrypted message is", " "*16, ":", decrypted_message)
# If an invalid input is provided
else:
print("Invalid choice.")
'''
Sample I/O:
a)
-------------------- Vernam Cipher --------------------
1. Encrypt a plaintext
2. Decrypt a ciphertext
3. Exit
Enter your choice: 1
Plaintext and key must be of same length.
Enter a message you want to encrypt: testcase
Enter a key to encrypt the message : mercurie
Encrypted message is : fijvwrai
b)
-------------------- Vernam Cipher --------------------
1. Encrypt a plaintext
2. Decrypt a ciphertext
3. Exit
Enter your choice: 2
Ciphertext and key must be of same length.
Enter a ciphertext you want to decrypt: fijvwrai
Enter a key to decrypt the ciphertext : mercurie
Decrypted message is : testcase
'''