Last active
November 10, 2015 07:37
-
-
Save horstjens/15ca8b640fa18c5d9051 to your computer and use it in GitHub Desktop.
caesars chiffre 1
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
# Caesar Cipher | |
# from Al Sweigarts invent with python book https://inventwithpython.com/chapter14.html | |
MAX_KEY_SIZE = 26 | |
def getMode(): | |
while True: | |
print('Do you wish to encrypt or decrypt a message?') | |
mode = input().lower() | |
if mode in 'encrypt e decrypt d'.split(): | |
return mode | |
else: | |
print('Enter either "encrypt" or "e" or "decrypt" or "d".') | |
def getMessage(): | |
print('Enter your message:') | |
return input() | |
def getKey(): | |
key = 0 | |
while True: | |
print('Enter the key number (1-%s)' % (MAX_KEY_SIZE)) | |
key = int(input()) | |
if (key >= 1 and key <= MAX_KEY_SIZE): | |
return key | |
def getTranslatedMessage(mode, message, key): | |
if mode[0] == 'd': | |
key = -key | |
translated = '' | |
for symbol in message: | |
if symbol.isalpha(): | |
num = ord(symbol) | |
num += key | |
if symbol.isupper(): | |
if num > ord('Z'): | |
num -= 26 | |
elif num < ord('A'): | |
num += 26 | |
elif symbol.islower(): | |
if num > ord('z'): | |
num -= 26 | |
elif num < ord('a'): | |
num += 26 | |
translated += chr(num) | |
else: | |
translated += symbol | |
return translated | |
mode = getMode() | |
message = getMessage() | |
key = getKey() | |
print('Your translated text is:') | |
print(getTranslatedMessage(mode, message, key)) |
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
table = {"a":"x", | |
"b":"y", | |
"c":"z", | |
"d":"0", | |
"e":"#"} | |
def encrypt(raw): | |
final = "" | |
for char in raw: | |
#if char in table: | |
# final+= table[char] | |
#else: | |
# final += char | |
final += table[char] if char in table else char | |
print("decrypted text:",raw) | |
print("encrypted text:",final) | |
#return final | |
def decrypt(raw): | |
final = "" | |
for char in raw: | |
#for (a,b) in table.items(): | |
# if b == char: | |
# match = b | |
# break | |
#else: | |
# match = char | |
match = [a for (a,b) in table.items() if b == char] | |
# match is a list! | |
if len(match) == 1: | |
final += match[0] | |
else: | |
final += char | |
print("encrypted text:", raw ) | |
print("decrypted text:", final) | |
#return final | |
text = input("bitte text eingeben und [ENTER] drücken: >>>") | |
mode = input(""" | |
bitte auswählen: | |
encrypt:............e + [ENTER] | |
decrypt:............d + [ENTER] | |
""") | |
if mode == "e": | |
encrypt(text) | |
elif mode == "d": | |
decrypt(text) | |
else: | |
print("bitte nur e oder d drücken") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment