def Encrypt(bytes:list, seed, steps:int): global seedlen, debug assert len(seed) > 20 CA.r_initCA(seed, steps) #Start up the CA with the specified seed and steps m_data = bytes[:] m_head = 0 ca_m_head = 4 ca_enc_head = len(seed) // 2 - 4 d = dlast = [] while (CA.steps <= 0): dlast = d d = CA.update() if (d[ca_m_head] == 1): m_head += 1 if (m_head >= len(m_data)): m_head = 0 else: pass #don't move m_head b = "0b" r = d.range() for i in range(r[0] + ca_enc_head, r[0] + ca_enc_head + 8): b += str(d[i]) m_data[m_head] = m_data[m_head] ^ int(b, 2) DumpCurrentRow(CA.steps, m_head, m_data, ca_m_head, d, b) #debug.write("\n\n\n") return m_data
def Decrypt(bytes:list, seed, steps:int): assert len(seed) > 20 CA.r_initCA(seed, steps) #Start up the CA with the specified seed and steps m_data = bytes[:] m_head = 0 ca_m_head = 4 ca_enc_head = len(seed) // 2 - 4 d = dlast = [] while (CA.steps <= 0): dlast = d d = CA.update() if (d[ca_m_head] == 1): m_head += 1 if (m_head >= len(m_data)): m_head = 0 else: pass #don't move m_head debug.write("mhead: " + str(m_head) + "\tdelta mhead: " + str(d[ca_m_head]) + "\n") #debug.write("\n\n\n") CA.rows = [d, dlast] CA.steps = -steps #CA.init() # starts the pygame window ##Todo: refactor these two runs into the below code to maximize reuse for n in (d, dlast): dH = 0 #CA.update_screen(d) # updates the pygame window if (n[ca_m_head] == 1): dH = -1 if (m_head < 0): m_head = len(m_data) - 1 else: dH = 0 b = "0b" r = n.range() for i in range(r[0] + ca_enc_head, r[0] + ca_enc_head + 8): b += str(n[i]) m_data[m_head] = m_data[m_head] ^ int(b, 2) m_head += dH #debug.write("d\t") DumpCurrentRow(CA.steps, m_head, m_data, ca_m_head, d, b) while (CA.steps <= -2): dH = 0 d = CA.update() #CA.update_screen(d) # updates the pygame window if (d[ca_m_head] == 1): dH = -1 if (m_head < 0): m_head = len(m_data) - 1 else: dH = 0 b = "0b" r = d.range() for i in range(r[0] + ca_enc_head, r[0] + ca_enc_head + 8): b += str(d[i]) m_data[m_head] = m_data[m_head] ^ int(b, 2) m_head += dH DumpCurrentRow(CA.steps, m_head, m_data, ca_m_head, d, b) #debug.close() return m_data