Created
March 10, 2017 22:36
-
-
Save marttosc/77bdf20793182b1b4c960beb44c5806e to your computer and use it in GitHub Desktop.
Caesar and Vigenère ciphers
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
MAX_KEY_SIZE = 26 | |
def get_mode(): | |
while True: | |
# Você quer criptografar ou decriptografar? | |
print('Wollen Sie zu verschlüsseln [v] oder zu entschlüsseln [e]?') | |
mode = input().lower() | |
if mode in 'verschlusseln v entschlusseln e'.split(): | |
return mode | |
else: | |
# Escreva "criptografar" ou "descriptografar".'' | |
print('Geben Sie "Verschlüsseln" oder "entschlüsseln".') | |
def get_message(): | |
# Entre com a mensagem. | |
print('Geben Sie Ihre Nachricht: ') | |
return input() | |
def get_key(): | |
key = 0 | |
while True: | |
# Escolha um número entre 1 e 26 (MAX_KEY_SIZE). | |
print('Geben Sie eine Nummer (1-%s)' % (MAX_KEY_SIZE)) | |
key = int(input()) | |
if key >= 1 and key <= MAX_KEY_SIZE: | |
return key | |
def get_translated_msg(mode, message, key): | |
if mode[0] == 'e': | |
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 = get_mode() | |
message = get_message() | |
key = get_key() | |
# Texto traduzido. | |
print('Übersetzt Text: ') | |
print(get_translated_msg(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
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
def get_mode(): | |
while True: | |
# Você quer criptografar ou decriptografar? | |
print('Wollen Sie zu verschlüsseln [v] oder zu entschlüsseln [e]?') | |
mode = input().lower() | |
if mode in 'verschlusseln v entschlusseln e'.split(): | |
return mode | |
else: | |
# Escreva "criptografar" ou "descriptografar".'' | |
print('Geben Sie "Verschlüsseln" oder "entschlüsseln".') | |
def get_key(message): | |
while True: | |
# Informe a chave de segurança. | |
print('Geben Sie den Sicherheitsschlüssel ein.') | |
key = input() | |
if len(key) <= len(message): | |
return key | |
def get_translated_msg(mode, message, key): | |
translated = [] | |
key_idx = 0 | |
key = key.upper() | |
for symbol in message: | |
num = LETTERS.find(symbol.upper()) | |
if num != -1: | |
if mode[0] == 'v': | |
num += LETTERS.find(key[key_idx]) | |
elif mode[0] == 'e': | |
num -= LETTERS.find(key[key_idx]) | |
num %= len(LETTERS) | |
if symbol.isupper(): | |
translated.append(LETTERS[num]) | |
elif symbol.islower(): | |
translated.append(LETTERS[num].lower()) | |
key_idx += 1 | |
if key_idx == len(key): | |
key_idx = 0 | |
else: | |
translated.append(symbol) | |
return ''.join(translated) | |
def get_message(): | |
# Entre com a mensagem. | |
print('Geben Sie Ihre Nachricht: ') | |
return input() | |
mode = get_mode() | |
message = get_message() | |
key = get_key(message) | |
# Texto traduzido. | |
print('Übersetzt Text: ') | |
print(get_translated_msg(mode, message, key)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment