Skip to content

Instantly share code, notes, and snippets.

@APAC-GOLD
Last active November 10, 2023 16:20
Show Gist options
  • Save APAC-GOLD/8e79dd07738d7d2b40c7f96d303bb2d9 to your computer and use it in GitHub Desktop.
Save APAC-GOLD/8e79dd07738d7d2b40c7f96d303bb2d9 to your computer and use it in GitHub Desktop.
Caesar Cipher
#Caesar Cipher Python Part 1 of 2 - Encryption
#dictionary
text_id = {"a":0, "b":1, "c":2, "d":3, "e":4, "f":5, "g":6, "h":7, "i":8, "j":9, "k":10, "l":11, "m":12, "n":13, "o":14, "p":15, "q":16, "r":17, "s":18, "t":19, "u":20, "v":21, "w":22, "x":23, "y":24, "z":25}
id_text = {0:"a", 1:"b", 2:"c", 3:"d", 4:"e", 5:"f", 6:"g", 7:"h", 8:"i", 9:"j", 10:"k", 11:"l", 12:"m", 13:"n", 14:"o", 15:"p", 16:"q", 17:"r", 18:"s", 19:"t", 20:"u", 21:"v", 22:"w", 23:"x", 24:"y", 25:"z"}
#shift by a number
n=1
#input
raw_message = input("please enter one word. HINT: Do not use hyphens, special characters, spaces, or numbers.")
print(raw_message)
#convert imput to encrypted text i.e. Cat
for character in raw_message:
print(character)
converted_number = text_id[character] #converted num line
shifted_number = converted_number + n #mapping letters to numbers
print(shifted_number)
if (shifted_number>25): #this is the if statement so we don't go over the 26 number of characters in the alphabet
shifted_number = shifted_number -26
print(shifted_number)
print(id_text[shifted_number]) #this line prints the letter associated with the "shifted_number"
#Caesar Cipher Python Part 2 of 2 - Decryption
#dictionary
text_id = {"a":0, "b":1, "c":2, "d":3, "e":4, "f":5, "g":6, "h":7, "i":8, "j":9, "k":10, "l":11, "m":12, "n":13, "o":14, "p":15, "q":16, "r":17, "s":18, "t":19, "u":20, "v":21, "w":22, "x":23, "y":24, "z":25}
id_text = {0:"a", 1:"b", 2:"c", 3:"d", 4:"e", 5:"f", 6:"g", 7:"h", 8:"i", 9:"j", 10:"k", 11:"l", 12:"m", 13:"n", 14:"o", 15:"p", 16:"q", 17:"r", 18:"s", 19:"t", 20:"u", 21:"v", 22:"w", 23:"x", 24:"y", 25:"z"}
#input
raw_message = "lbu" #manual
#shift by a number
n=1
print(raw_message)
#step 6. decrypt the message
for character in raw_message:
print("------------------")
print(character)
converted_number = text_id[character] #converted num line
print(converted_number)
shifted_number = converted_number - n #mapping letters to numbers
print(shifted_number)
if (shifted_number<0): #this is the if statement so we don't go over the 26 number of characters in the alphabet
shifted_number = shifted_number + 26 #this is the converse of the code above where we subtracted 26
print(shifted_number)
print("------------------")
print(id_text[shifted_number]) #this line prints the letter associated with the "shifted_number"
#turn decrypted text into one string
print("------------------")
@APAC-GOLD
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment