I'm trying to make a Caesar cipher encryptor/decryptor in Python, by getting the ASCII index of a letter and adding/subtracting from it. However, it seems to be using Unicode instead because I get letters with accents instead of just English alphabet letters. For example, if I enter the word 'weasel' and encrypt it by 2, what I get is 'yÞĿƲȗʃ'. How do I use ASCII?
Here is my code:
def manipulate(text, key, e_or_d):
ciphertext = ''
for i in text:
asciiCode = ord(i)
if e_or_d == 'e':
key = ord(i) + key
elif e_or_d == 'd':
key = ord(i) - key
newChar = chr(key)
ciphertext += newChar
if e_or_d == 'e':
print('Here is your encrypted text: ')
elif e_or_d == 'd':
print('Here is your decrypted text: ')
print(ciphertext)
text = input('Enter text: ')
e_or_d = input('Enter \'e\' for encryption or \'d\' for decryption: ')
key = int(input('Enter encryption key: '))
manipulate(text, key, e_or_d)
I used chr and ord here to turn an index into a character and identify the index of a character, respectively.
Sorry if this is incoherent and disorganised.
keyvariable that arrives to the function. Trynew_ord = ord(i) + keyinstead ofkey = ord(i) + keyto keepkeythe same throughout i.e., what user supplied. Similar goes for decryptionif.manipulate("weasel", 4, "e")will give you unexpected result and you need to handle it.