Skip to content

Instantly share code, notes, and snippets.

@Richienb
Created July 1, 2018 08:30
Show Gist options
  • Save Richienb/2f8582e8ca173dd11a0c87219392ab44 to your computer and use it in GitHub Desktop.
Save Richienb/2f8582e8ca173dd11a0c87219392ab44 to your computer and use it in GitHub Desktop.
Text Cipher In Python

Text Cipher For Python

What is this?

This is a simple text-based cipher for python

python textcipher.py
from pprint import pprint
lettertocode = {"d": "a", "i": "b", "o": "c", "x": "d", "v": "e", "k": "f", "e": "g", "q": "h", "y": "i", "s": "j", "m": "k", "g": "l", "a": "m", "c": "n", "j": "o", "n": "p", "w": "q", "z": "r", "u": "s", "r": "t", "f": "u", "l": "v", "t": "w", "p": "x", "h": "y", "b": "z"}
codetoletter = {"a": "d", "b": "i", "c": "o", "d": "x", "e": "v", "f": "k", "g": "e", "h": "q", "i": "y", "j": "s", "k": "m", "l": "g", "m": "a", "n": "c", "o": "j", "p": "n", "q": "w", "r": "z", "s": "u", "t": "r", "u": "f", "v": "l", "w": "t", "x": "p", "y": "h", "z": "b"}
def encode(message):
for item in enumerate(message):
outputtext = ""
try:
outputtext += lettertocode[item[1]]
except KeyError:
outputtext += item[1]
return outputtext
def decode(message):
for item in enumerate(message):
outputtext = ""
try:
outputtext += codetoletter[item[1]]
except KeyError:
outputtext += item[1]
return outputtext
def userchoose():
print("Welcome to Text Cipher")
print("You can choose to (e)ncode, (d)ecode and (v)iew the cipher used.")
menuchoice = input("Your Choice: ")
menuchoice = menuchoice.lower()
if menuchoice == "e":
print("Your encoded message is: \n" + encode(input("Enter a message to encode: ").lower()), end="\n\n")
elif menuchoice == "d":
print("Your decoded message is: \n" + decode(input("Enter a message to decode: ").lower()), end="\n\n")
elif menuchoice == "v":
print("The cipher code is as follows (cipher on the left, output on the right)")
pprint(codetoletter)
print("")
else:
print("Invalid menu choice.", end="\n\n")
while True:
userchoose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment