# # One-Time (or Two-Time) Padding Code # ### This is "super secure"! We need a good cryptographic PRNG. from Crypto.Random import random ### ### Some routines for manipulating bits ### BITS = ('0', '1') def display_bits(b): """converts list of {0, 1}* to string""" return ''.join([BITS[e] for e in b]) def seq_to_bits(seq): return [0 if b == '0' else 1 for b in seq] def convert_to_bits(n, pad): result = [] while n > 0: result = [n % 2] + result n = n / 2 while len(result) < pad: result = [0] + result return result def string_to_bits(s): def chr_to_bit(c): return convert_to_bits(ord(c), 7) return [b for group in map(chr_to_bit, s) for b in group] def bits_to_char(b): assert len(b) == 7 value = 0 for e in b: value = (value * 2) + e return chr(value) def list_to_string(p): return ''.join(p) def bits_to_string(b): return ''.join([bits_to_char(b[i:i + 7]) for i in range(0, len(b), 7)]) def random_sequence(n): return map(lambda x: random.choice([0, 1]), range(n)) ### One-Time Pad - just XOR all message bits with corresponding key bits. def otp(m, k): assert len(m) == len(k) return [(mm + kk) % 2 for mm, kk in zip(m, k)]