This is a simple program to encrypt and decrypt some text. But it seems it doesn't work as expected.. I've also been told to use 2.7 no matter what Your help is Appreciated, Thanks.
The Code in Python 2.7.16:
letterspace = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
input1e = ""
keyse = ""
input1d = ""
keysd = ""
def inpute():
global input1e
global keyse
input1e = raw_input("Enter the message to be encrypted : ")
input1e = input1e.upper()
#keyinput = "Input the key (Key should be of length " + str(len(input1e)) + ") : "
keyse = raw_input("Enter the key : ")
if len(keyse) == len(input1e):
return
else:
print("Please input the values correctly!")
inpute()
def inputd():
global input1d
global keysd
input1d = raw_input("Enter the message to be decrypted : ")
input1d = input1d.upper()
#keyinput = "Input the key (Key should be of length " + str(len(input1d)) + ") : "
keysd = raw_input("Enter the key : ")
if len(keysd) == len(input1d):
return
else:
print("Please input the values correctly!")
inputd()
def conv(x):
for k, v in enumerate(letterspace):
if v == x:
print(k)
return k
else:
continue
def reconv(y):
for k, v in enumerate(letterspace):
if k == y:
print(v)
return str(v)
else:
continue
def encryptor(inpe, keye):
encm = []
enck = []
encf = []
ence = ""
for i in inpe:
encm.append(conv(i))
for k in keye:
enck.append(conv(k))
for x in range(0,len(encm)):
z = int(encm[x]) + int(enck[x])
if z > 26:
z -= 26
encf.append(z)
for w in encf:
ence += reconv(w)
print ence
def decryptor(inpd, keyd):
decm = []
deck = []
decf = []
dece = ""
for i in inpd:
decm.append(conv(i))
for k in keyd:
deck.append(conv(k))
for x in range(0,len(decm)):
z = decm[x] - deck[x]
if z < 0:
z += 26
decf.append(z)
for w in decf:
dece += reconv(w)
print dece
def menu():
print("---------------------ONE-TIME-PAD-PROGRAM---------------------")
sel = raw_input("Type E for Encryption or D for Decryption :")
if len(sel) > 1:
sel = sel[0]
if sel == "E" or sel == "e":
inpute()
encryptor(input1e, keyse)
elif sel == "D" or sel == "d":
inputd()
decryptor(input1d, keyse)
else:
print("Please enter correct value!")
menu()
menu()
Output: A is 0, B is 1 and so on..
---------------------ONE-TIME-PAD-PROGRAM---------------------
Type E for Encryption or D for Decryption :e
Enter the message to be encrypted : welcome
Enter the key : pragyan
22
4
11
2
14
12
4
Traceback (most recent call last):
File "C:/Users/mnsan/Downloads/Pri/OTP.py", line 110, in <module>
menu()
File "C:/Users/mnsan/Downloads/Pri/OTP.py", line 100, in menu
encryptor(input1e, keyse)
File "C:/Users/mnsan/Downloads/Pri/OTP.py", line 61, in encryptor
z = int(encm[x]) + int(enck[x])
TypeError: int() argument must be a string or a number, not 'NoneType'
It skips the second for loop in line 57. My idea : https://en.wikipedia.org/wiki/One-time_pad
I have run the script. Is this the outcome you are waiting for?
Type E for Encryption or D for Decryption :e
Enter the message to be encrypted : WELCOME
Enter the key : PRAGYAN
22
4
11
2
14
12
4
15
17
0
6
24
0
13
L
V
L
I
M
M
R
LVLIMMR
The problem is that your letterspace is in upper and the input you give is in lowerletter. So when it tries to use the function conv
,it doesn't find anything to match to, so it returns empty and int(enck[x])
is full of Nones.
You don't have problem with the first word because its just numbers.
So, one thing you can do, is correctly define your letterspace, with both uppers and lowers.