Skip to content

Instantly share code, notes, and snippets.

@bergpb
Created April 24, 2020 22:29
Show Gist options
  • Select an option

  • Save bergpb/e51d46263ffbbfa01d399952eb9ff6bd to your computer and use it in GitHub Desktop.

Select an option

Save bergpb/e51d46263ffbbfa01d399952eb9ff6bd to your computer and use it in GitHub Desktop.
Cifra de Vernan em Python
msg = "0 0 1 0 1 1 0 1 0 1 1 1"
pad = "1 0 0 1 1 1 0 0 1 0 1 1"
def str_to_list(itens: str) -> list:
"""Converte string para lista"""
return [int(i) for i in itens.split(" ")]
def list_to_str(itens: list) -> str:
"""Converte lista para string"""
string = ""
for _, item in enumerate(itens):
string += str(item) + " "
return string
def sending(msg: list, pad: list) -> list:
"""Envio de mensagem, onde e aplicado o xor em msg e pad, gerando a cifra"""
cipher = []
for i in range(0, len(msg)):
cipher.append(msg[i] ^ pad[i])
print("Cipher: {}".format(list_to_str(cipher)))
return cipher
def receiver(cipher: list, pad: list) -> list:
"""Recebimento de mensagem, onde e aplicado o xor em cifra e pad gerando a mensagem original"""
origin_msg = []
for i in range(0, len(cipher)):
origin_msg.append(cipher[i] ^ pad[i])
print("Message: {}".format(list_to_str(origin_msg)))
return origin_msg
cipher = sending(str_to_list(msg), str_to_list(pad))
receiver(cipher, str_to_list(pad))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment