I have attempted to make a Caesar Cipher for my first ever Python project at school. I kind of copied out code from a youtube video for the main cipher segment but when I encrypt the message a user types, it does a random cipher instead of a key that he inputs into the shell. This is the code:
abc = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
def main():
message = input("What's the message to encrypt/decrypt? ")
key = int(input("What number would you like for your key value? "))
choice = input("Choose: encrypt or decrypt. ")
if choice == "encrypt":
encrypt(message, key)
elif choice == "decrypt":
encrypt(message, key * (-1))
else:
print("Bad answer, try again.")
def encrypt(message, key):
cipherText = ""
for letter in message:
if letter in abc:
newPosition = (abc.find(letter) + key) % 26
cipherText += abc[newPosition]
else:
cipherText += letter
print(cipherText)
return cipherText
main()
Could someone help me solve this issue please. Also please don't make it very complex since I am a beginner at Python and I don't know very much at all.
THANK YOU!
The problem is that you're intertwining upper and lower case letters in your character set. So when you attempt to replace a character with a character, say, 5 characters ahead, you're actually flipping the case and moving 2-3 characters ahead (depending on what case you started with). There are better ways of accomplishing this, but it turns out one simple change can make your code work as expected:
If you double the key when you find your replacement character, then you'll be skipping over both the upper and lower case letters in the character set. And since your doubled key will always be even, you'll end up with the same case you started with. You'll also need to change the modulo correspondingly to 52 as pointed out by R.Sharp.