Created
June 25, 2020 16:46
-
-
Save nicoguaro/d7717db5f0207fdf3365ddf77339d36f to your computer and use it in GitHub Desktop.
Cifra un mensaje haciendo un corrimiento hacia la derecha o izquierda.
This file contains hidden or 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
# -*- 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