Im starting to learn how to work on python beacause of school, and I haven't really had any problem at all; however, last week we were assigned to make a stream cipher, but I've really had a hard time working with it. The code is the following:
import binascii
mode = 0
def asktodo():
while True:
print('Do you wish to encrypt or decrypt your message?')
mode = input().lower()
if mode in 'encrypt e decrypt d'.split():
return mode
else:
print('Enter either "encrypt" or "e", or "decrypt" or "d".')
def message():
inp = input('Input your message:\n')
inp = inp.lower()
return inp
def gkey():
nkey = int(input('Input your key:\n'))
return nkey
def encrypt(plaintext,key):
ciphered=""
for x in plaintext:
ptd=ord(x)
cip=ptd^key
ciphered=ciphered+chr(cip)
ciphered=bytes(ciphered, 'utf-8')
return binascii.hexlify(ciphered)
def decrypt(ciphered,key):
ciphered=binascii.unhexlify(ciphered)
ciphered=str(ciphered, 'utf-8')
plain=''
for x in ciphered:
ctd=ord(x)
descip=ctd^key
plain=plain+chr(descip)
return plain
mode = asktodo()
mess= message()
xkey = gkey()
if mode == 'e' or mode == 'encrypt':
print(' ')
print('Your encrypted message is:')
print(encrypt(mess, xkey))
else:
print(' ')
print('Your decrypted message is:')
print(decrypt(mess, xkey))
input()
The encription part works perfectly. However, when I try to decrypt any message the following error pops out:
--line 33, in decrypt
- ciphered=binascii.unhexlify(ciphered)
Error: Odd-length string
I've tried many ways and followed many advises, but I just can't solve it.
P.S.: The code changes the values to hexadecimal before giving you the encrypted message.