Last active
June 1, 2022 08:53
-
-
Save Muntasir2001/7adcda2e1a29e507f3d019c43141e559 to your computer and use it in GitHub Desktop.
Caesar Cipher Encoder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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