def pass1_decrypt(namespace): block_size = dpget(namespace, "/config/block.size") pass1_data = dpget(namespace, "/block/pass1") total_data = b"" for block, id, key, masher in pass1_data: c = 0 k = 0 d = [] key_size = len(key) for e in block[:8]: d.append(key[k] ^ e) k += 1 if k >= key_size: k = 0 data_size = struct.unpack('l', np.array(d, dtype='B').tostring())[0] d = [] for e in block[8:data_size+8]: if k >= key_size: k = 0 d.append(key[k] ^ e) k += 1 total_data += np.array(d, dtype='B').tostring() data = msgpack.loads(total_data) dpset(namespace, "/block/cleartext", data) return namespace
def pass1_encrypt(namespace, data=None): block_size = dpget(namespace, "/config/block.size") d = msgpack.dumps(data) namespace = bm_next(namespace) _d = [d[i:i+block_size] for i in range(0, len(d), block_size)] dpset(namespace, "/block/pass1", []) pass1_data = dpget(namespace, "/block/pass1") for block in _d: block_size = np.fromstring(struct.pack('l', len(block)), dtype='B') b_block = dpget(namespace, "/block/data") key = dpget(namespace, "/key/private") masher = dpget(namespace, "/key/masher") id = dpget(namespace, "/key/id") src_block = np.fromstring(block, dtype='B') b_block[:8] = block_size c = 0 k = 0 key_size = len(key) for e in b_block[:8]: b_block[c] = key[k] ^ e c += 1 k += 1 if k >= key_size: k = 0 c = 0 for e in src_block: if k >= key_size: k = 0 src_block[c] = key[k] ^ e c += 1 k += 1 b_block[8:len(src_block)+8] = src_block pass1_data.append((b_block, id, key, masher)) return namespace
def next(namespace, **kw): dpset(namespace, "/config/stamp", time.time()) dpset(namespace, "/config/id", str(uuid.uuid4())) if (dpget(namespace, "/config/block.size") % 8) != 0 or (dpget(namespace, "/config/key.size") % 8) != 0: raise BitmasherKeysizeException namespace = cryptobook_get(namespace) namespace = make_block(namespace) return namespace
def pass2_decrypt(namespace): dpset(namespace, "/block/pass1", []) pass1 = dpget(namespace, "/block/pass1") pass2 = dpget(namespace, "/block/pass2") for data, id, key, masher in pass2: deforest = data[::2] pass1.append((deforest, id, key, masher)) return namespace
def pass2_encrypt(namespace): block_size = dpget(namespace, "/config/block.size") pass1 = dpget(namespace, "/block/pass1") dpset(namespace, "/block/pass2", []) pass2 = dpget(namespace, "/block/pass2") for data, id, key, masher in pass1: forest = np.random.randint(0, 255, size=len(data)*2, dtype='B') forest[::2] = data pass2.append((forest, id, key, masher)) return namespace
def decrypt(namespace, *blocks): dpset(namespace, "/block/pass3", []) pass3 = dpget(namespace, "/block/pass3") used = dpget(namespace, "/cryptobook/used") for id, data in blocks: if id not in used: raise BitmasherMissedKeyException pass3.append((np.frombuffer(data, dtype='B'), id, used[id][0], used[id][1])) namespace = pass3_decrypt(namespace) namespace = pass2_decrypt(namespace) namespace = pass1_decrypt(namespace) return dpget(namespace, "/block/cleartext")
def pass3_encrypt(namespace): pass2 = dpget(namespace, "/block/pass2") dpset(namespace, "/block/pass3", []) pass3 = dpget(namespace, "/block/pass3") for data, id, key, masher in pass2: b = bitstring.BitString(data.tostring()) c = 0 for m in masher: #print(m,key[c]) if m is True: b.rol(key[c]) else: b.ror(key[c]) c += 1 pass3.append((b.tobytes(), id, key, masher)) return namespace
def genkey(namespace): key_size = dpget(namespace, "/config/key.size") key, masher = return_key(namespace) dpset(namespace, "/key/private", key) dpset(namespace, "/key/masher", masher) dpset(namespace, "/key/id", str(uuid.uuid4())) return namespace
def cryptobook_get(namespace): queue = dpget(namespace, "/cryptobook/queue") used = dpget(namespace, "/cryptobook/used") try: id, key, masher = queue.get_nowait() used[id] = (key, masher) dpset(namespace, "/key/private", key) dpset(namespace, "/key/masher", masher) dpset(namespace, "/key/id", id) except queue.Empty: namespace = cryptobook_add(namespace) return cryptobook_get(namespace) return namespace
def make_block(namespace): block_size = dpget(namespace, "/config/block.size") b = np.random.randint(0, 255, size=block_size + 8, dtype='B') dpset(namespace, "/block/data", b) return namespace