Cifra un mensaje haciendo un corrimiento hacia la derecha o izquierda.
# -*- coding: utf-8 -*- | |
""" | |
Cifra una mensaje haciendo un corrimiento del alfabeto. | |
@author: Nicolás Guarín-Zapata | |
""" | |
ALFABETO = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', | |
'm', 'n', 'ñ', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', | |
'x', 'y', 'z'] | |
def crear_cypher(n=10): | |
cypher = {} | |
for cont, letra in enumerate(ALFABETO): | |
cypher[letra] = ALFABETO[(cont - n) % len(ALFABETO)] | |
cypher[" "] = " " | |
cypher[","] = "," | |
cypher["."] = "." | |
cypher["!"] = "!" | |
cypher["¡"] = "¡" | |
return cypher | |
def cifrar_mensaje(cypher, mensaje): | |
mensaje_cifrado = [] | |
for letra in mensaje: | |
if letra.isupper(): | |
mensaje_cifrado.append(cypher[letra.lower()].upper()) | |
else: | |
mensaje_cifrado.append(cypher[letra]) | |
mensaje_cifrado = "".join(mensaje_cifrado) | |
return mensaje_cifrado | |
def descifrar_mensaje(cypher, mensaje_cifrado): | |
inv_cypher = {v: k for k, v in cypher.items()} | |
mensaje = [] | |
for letra in mensaje_cifrado: | |
if letra.isupper(): | |
mensaje.append(inv_cypher[letra.lower()].upper()) | |
else: | |
mensaje.append(inv_cypher[letra]) | |
mensaje = "".join(mensaje) | |
return mensaje | |
if __name__ == "__main__": | |
mensaje = "Pedro Pablo Pereira, pobre pintor portugues pinta paisajes "\ | |
+ "por poco precio para pronto poder pasar por puente para "\ | |
+ "Paris." | |
cypher = crear_cypher(n=17) | |
mensaje_cifrado = cifrar_mensaje(cypher, mensaje) | |
mensaje_recu = descifrar_mensaje(cypher, mensaje_cifrado) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment