Skip to content

Instantly share code, notes, and snippets.

@Muntasir2001
Last active June 1, 2022 08:53
Show Gist options
  • Save Muntasir2001/7adcda2e1a29e507f3d019c43141e559 to your computer and use it in GitHub Desktop.
Save Muntasir2001/7adcda2e1a29e507f3d019c43141e559 to your computer and use it in GitHub Desktop.
Caesar Cipher Encoder
def convert_to_caesar():
sentence = input('Enter your sentence to be encrypted:').upper().replace('.', 'X').replace(' ', '')
shift = int(input('Enter the shift value (integers only):'))
encrypt = ""
# basic algorithm
# c = (x + n) % 26
# Where c is the encoded character, x is the actual character, and n is the number of positions we want to shift the character x by. We’re taking mod with 26 because there are 26 letters in the English alphabet.
for a in sentence:
a_unicode = ord(a)
a_index = ord(a) - ord("A")
# shift the character
new_index = (a_index + shift) % 26
# convert to new character
new_unicode = new_index + ord('A')
new_char = chr(new_unicode)
encrypt = encrypt + new_char
print(encrypt)
return encrypt
convert_to_caesar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment