2009-10-13 10:08:03 -06:00
|
|
|
#!/usr/bin/python3
|
|
|
|
"""This is morris code, except the dots and dashes are each represented by
|
|
|
|
many different possible characters. The 'encryption key' is the set of
|
|
|
|
characters that represent dots, and the set that represents dashes."""
|
|
|
|
|
|
|
|
import random
|
2009-10-15 16:29:52 -06:00
|
|
|
import crypto
|
2009-10-13 10:08:03 -06:00
|
|
|
|
|
|
|
dots = b'acdfhkjnpsrtx'
|
|
|
|
dashes = b'begilmoquvwyz'
|
|
|
|
|
|
|
|
morris = {'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': '--..',
|
2009-10-13 16:08:11 -06:00
|
|
|
'.': '.-.-.-',
|
2009-10-13 10:08:03 -06:00
|
|
|
',': '--..--',
|
|
|
|
':': '---...'}
|
|
|
|
|
|
|
|
imorris = {}
|
|
|
|
for k in morris:
|
|
|
|
imorris[morris[k]] = k
|
|
|
|
|
2009-10-13 16:08:11 -06:00
|
|
|
plaintext = [b'it is fun to make up bizarre cyphers, but the next one is '
|
2009-10-13 10:08:03 -06:00
|
|
|
b'something a little more standard.',
|
2009-10-13 16:08:11 -06:00
|
|
|
b'all i have to say is: giant chickens.']
|
|
|
|
|
2009-10-13 10:08:03 -06:00
|
|
|
|
|
|
|
|
|
|
|
def encode(text):
|
|
|
|
out = bytearray()
|
|
|
|
for t in text:
|
|
|
|
if t == ord(' '):
|
2009-10-13 16:08:11 -06:00
|
|
|
out.extend(b' ')
|
2009-10-13 10:08:03 -06:00
|
|
|
else:
|
|
|
|
for bit in morris[chr(t)]:
|
|
|
|
if bit == '.':
|
|
|
|
out.append(random.choice(dots))
|
|
|
|
else:
|
|
|
|
out.append(random.choice(dashes))
|
2009-10-13 16:08:11 -06:00
|
|
|
out.append(ord(' '))
|
|
|
|
return bytes(out[:-1])
|
2009-10-13 10:08:03 -06:00
|
|
|
|
|
|
|
def decode(text):
|
2009-10-13 16:08:11 -06:00
|
|
|
text = text.replace(b' ', b'&')
|
|
|
|
# print(text)
|
2009-10-13 10:08:03 -06:00
|
|
|
words = text.split(b'&')
|
|
|
|
out = bytearray()
|
|
|
|
for word in words:
|
2009-10-13 16:08:11 -06:00
|
|
|
# print(word)
|
|
|
|
word = word.strip()
|
|
|
|
for parts in word.split(b' '):
|
|
|
|
code = []
|
|
|
|
for part in parts:
|
|
|
|
if part in dots:
|
|
|
|
code.append('.')
|
|
|
|
else:
|
|
|
|
code.append('-')
|
|
|
|
code = ''.join(code)
|
|
|
|
out.append(ord(imorris[code]))
|
|
|
|
out.append(ord(' '))
|
|
|
|
return bytes(out[:-1])
|
2009-10-13 10:08:03 -06:00
|
|
|
|
|
|
|
c = encode(plaintext[0])
|
|
|
|
print('<dl><dt>Alice<dd>', str(c, 'utf-8'))
|
|
|
|
assert decode(c) == plaintext[0]
|
|
|
|
c = encode(plaintext[1])
|
|
|
|
print('<dt>Bob<dd>', str(c, 'utf-8'), '</dl>')
|
|
|
|
assert decode(c) == plaintext[1]
|