I am trying to make a python program that will take in plain text and encrypt it using a key. It is written with python 2.7.4
This is my code so far
def encrypter(intext, shift, modder):
plain = list(intext)
out = ''
j = 0
key = list(shift)
for i in plain:
if mod > 0:
x = chr((ord(i) + ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48)
if mod < 0:
x = chr(((ord(i) - ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48))
out += x
j += 1
return out
sel = raw_input("Encrypt (e)/ Decrypt (d)")
if sel == 'e':
mod = 1
elif sel == 'd':
mod = -1
else:
mod = 0
print('Enter a proper value!')
if mod != 0:
print(encrypter(raw_input('Enter the text:'), raw_input('Enter the key-phrase:'), mod)
When I encrypt something I get this:
C:\Users\aesha_000\py\Scripts\python.exe "D:/Programming/Computing GCSE/Resources/functiontest3.py"
Encrypt (e)/ Decrypt (d)e
Enter the text:alex
Enter the key-phrase:pass
]Yd:
Process finished with exit code 0
But the problem is when I then Decrypt it again I get the wrong output:
C:\Users\aesha_000\py\Scripts\python.exe "D:/Programming/Computing GCSE/Resources/functiontest3.py"
Encrypt (e)/ Decrypt (d)d
Enter the text:]Yd:
Enter the key-phrase:pass
a2e>
Process finished with exit code 0
Does anyone know the answer to this or am I just being stupid?