I've written an encryption function that works by performing an XOR function on a letter in the plaintext and the corresponding letter in the key. See the code below:
def vernam(y):
ciphertext = "" # this declares the ciphertext variable
vernamkey = []
for letter in y:
individualletterkey = secrets.choice(string.ascii_letters) # this generates a different key for each letter
vernamkey.append(individualletterkey)
newletter = chr(ord(letter) ^ ord(individualletterkey))
print(newletter)
ciphertext += newletter
for element in vernamkey: # this loop ensures that the key for every letter is in a text file that can be passed
# on to the intended recipient for them to decrypt
vkey.write(str(element))
vkey.write("\n")
return ciphertext
While the encrypt function works, for certain unicode characters that pycharm (my IDE) can seemingly not represent, the returned ciphertext has hexadecimal in it:
Enter the message to be encrypted Hello world
8
?
;
l
=
6
('\x01\x178?;l\x07\x00=\x0e6')
As you can see, for certain characters in the ciphertext what I'm assuming is a sort of placeeholder is used. These characters are then represented as hexadecimal in the final outputted key at the bottom. This is a problem because I wish to use this key to decrypt this text, and for that to be done one of two things has to happen:
Convert the hexadecimal into a unicode character in the final key. Not sure if that would be wise as multiple different characters will be represented by the same answer
Have the decryption algorithm recognise the hexadecimal characters in the text and convert them into unicode themselves
How would I accomplish either of these?