-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubtitution_enc.py
43 lines (36 loc) · 1.19 KB
/
Subtitution_enc.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
# This is a program that will encrypt plain text
# by subtitution method
# Get user input
plainText = input("Enter plain text: ")
subKey = int(input("Enter subtitution key(integer): "))
cipherTextList = []
# Take mod of the key if it is bigger than 26
if subKey >= 26:
subKey = subKey % 26
upperAlphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lowerAlphabets = "abcdefghijklmnopqrstuvwxyz"
'''
Following for loop will only encrypt the upper and lower Alphabets
characters. Numbers and symbols in plainText will be escaped and produced
same in the cipherText
'''
for a in plainText:
if a in upperAlphabets:
i = upperAlphabets.index(a)
i = i + subKey
if i > 25:
i = i % 26
cipherTextList.append(upperAlphabets[i])
continue
elif a in lowerAlphabets:
i = lowerAlphabets.index(a)
i = i + subKey
if i > 25:
i = i % 26
cipherTextList.append(lowerAlphabets[i])
continue
else:
cipherTextList.append(a)
# Join the characters in list and to produce string and output cipherText
cipherText = "".join(cipherTextList)
print("Ciphertext is: ", cipherText)