2009-10-13 10:08:03 -06:00
|
|
|
#!/usr/bin/python3
|
|
|
|
'''This is the same as the previous, but it uses non-return to zero to encode
|
|
|
|
the binary.'''
|
|
|
|
|
|
|
|
import random
|
2009-10-15 16:29:52 -06:00
|
|
|
import crypto
|
2009-10-13 10:08:03 -06:00
|
|
|
|
|
|
|
lower = b'abcdefghijklmnopqrstuvwxyz'
|
|
|
|
upper = lower.upper()
|
|
|
|
|
2009-10-15 16:29:52 -06:00
|
|
|
alice = b'The next one is in Morris Code. Unlike the previous two, '\
|
|
|
|
b'they will actually need to determine some sort of key.'
|
|
|
|
bob = b'Morris code with a key? That sounds bizarre. probable cause'
|
2009-10-13 10:08:03 -06:00
|
|
|
|
|
|
|
def encode(text):
|
|
|
|
out = bytearray()
|
|
|
|
mask = 0x80
|
|
|
|
state = 0
|
|
|
|
for t in text:
|
|
|
|
for i in range(8):
|
|
|
|
next = t & mask
|
|
|
|
if not state and not next:
|
|
|
|
out.append(random.choice(upper))
|
|
|
|
out.append(random.choice(lower))
|
|
|
|
elif not state and next:
|
|
|
|
out.append(random.choice(lower))
|
|
|
|
out.append(random.choice(upper))
|
|
|
|
elif state and not next:
|
|
|
|
out.append(random.choice(upper))
|
|
|
|
out.append(random.choice(lower))
|
|
|
|
elif state and next:
|
|
|
|
out.append(random.choice(lower))
|
|
|
|
out.append(random.choice(upper))
|
|
|
|
state = next
|
|
|
|
t = t << 1
|
|
|
|
|
|
|
|
return bytes(out)
|
|
|
|
|
|
|
|
def decode(text):
|
|
|
|
out = bytearray()
|
|
|
|
i = 0
|
|
|
|
while i < len(text):
|
|
|
|
c = 0
|
|
|
|
mask = 0x80
|
|
|
|
for j in range(8):
|
|
|
|
a = 0 if text[i] in lower else 1
|
|
|
|
b = 0 if text[i+1] in lower else 1
|
|
|
|
assert a != b, 'bad encoding'
|
|
|
|
if b:
|
|
|
|
c = c + mask
|
|
|
|
mask = mask >> 1
|
|
|
|
i = i + 2
|
|
|
|
out.append(c)
|
|
|
|
return bytes(out)
|
|
|
|
|
2009-10-15 16:29:52 -06:00
|
|
|
crypto.mkIndex(encode, decode, alice, bob, crypto.groups)
|