Created
July 23, 2021 19:29
-
-
Save deparkes/768754e9902d2444c6c40df988c5fb3c to your computer and use it in GitHub Desktop.
Simple exploration of the Lorentz cipher concept using Python - see also https://wp.me/p4DE9r-1mc
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
# Baudot | |
import random | |
import string | |
# Taken from https://www.cryptomuseum.com/ref/ita2/index.htm | |
baudot = { 'a': '0b00011', | |
'b': '0b11001', | |
'c': '0b01110', | |
'd': '0b01001', | |
'e': '0b00001', | |
'f': '0b01101', | |
'g': '0b11010', | |
'h': '0b10100', | |
'i': '0b00110', | |
'j': '0b01011', | |
'k': '0b01111', | |
'l': '0b10010', | |
'm': '0b11100', | |
'n': '0b01100', | |
'o': '0b11000', | |
'p': '0b10110', | |
'q': '0b10111', | |
'r': '0b01010', | |
's': '0b00101', | |
't': '0b10000', | |
'u': '0b00111', | |
'v': '0b11110', | |
'w': '0b10011', | |
'x': '0b11101', | |
'y': '0b10101', | |
'z': '0b10001', | |
' ': '0b00100', | |
'N': '0b00000', | |
'C': '0b01000', | |
'L': '0b00010', | |
'F': '0b11011', | |
'T' : '0b11111'} | |
message = 'my secret message' | |
# Create keystring: | |
# https://stackoverflow.com/questions/2823316/generate-a-random-letter-in-python | |
#random_choice_characters = string.ascii_lowercase | |
#random_choice_characters = 's' | |
random_choice_characters = list(baudot.keys()) | |
key_string = ''.join(random.choice(random_choice_characters) for x in range(len(message))) | |
print('\nOriginal message: ') | |
print(message) | |
print('\nOriginal Baudot: ') | |
[print(baudot[i][2:7]) for i in message] | |
print('\nKey String: ') | |
print(key_string) | |
broadcast = [] | |
for i, letter in enumerate(message): | |
broadcast.append(int(baudot[letter], 2) ^ int(baudot[key_string[i]], 2)) | |
print('\nBroadcast signal: ') | |
[print(f"{i:#07b}"[2:7]) for i in broadcast] | |
# Receive signal | |
# Do reverse lookup of dictionary: | |
# https://stackoverflow.com/questions/8023306/get-key-by-value-in-dictionary | |
# Need to somehow handle zero padding | |
# https://stackoverflow.com/questions/16926130/convert-to-binary-and-keep-leading-zeros-in-python | |
# https://stackoverflow.com/questions/1523465/binary-numbers-in-python#1523581 | |
received_message = [] | |
for i in broadcast: | |
received_message.append(list(baudot.keys())[list(baudot.values()).index(f"{i:#07b}")]) | |
print('\nMessage Received: ') | |
print(''.join(received_message)) | |
decrypted_message = [] | |
# Decrypt signal | |
# Need to XOR with original key_string again | |
for i, letter in enumerate(received_message): | |
decrpyted_code = int(baudot[letter], 2) ^ int(baudot[key_string[i]], 2) | |
decrypted_message.append(list(baudot.keys())[list(baudot.values()).index(f"{decrpyted_code:#07b}")]) | |
print('\nMessage Decrypted: ') | |
print(''.join(decrypted_message)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment