def main(): secret = trapdoor.loadtrapdoor() if len(sys.argv) < 5 or (sys.argv[-2] not in ("-out", "--out")): print("usage: python3 encrypt.py <realtext>", "<faketext0> [<faketext1> ...] -out <outfile>") return faketextfns = sys.argv[2:-2] realtextfn = sys.argv[1] outfn = sys.argv[-1] print("plaintext:", realtextfn) print("decoy texts:", faketextfns) print("encrypted output:", outfn) inbits = streamofbits.bits(realtextfn, infinitepad=True) fakestreams = [streamofbits.bits(fn, infinitepad=True) for fn in faketextfns] realkey = [] fakekeys = [] for fn in faketextfns: fakekeys.append([]) totalbits = 8 * streamofbits.maxfilesize([realtextfn] + faketextfns) with open(outfn, "wb") as outfile: for bitnum in range(totalbits): inbit = next(inbits) enc = encode(inbit, secret) asbytes = streamofbits.bits_to_bytes(enc) outfile.write(bytes(asbytes)) realkey.append(enc[0] ^ inbit) for fakestream,fakekey in zip(fakestreams,fakekeys): fakebit = next(fakestream) fakekey.append(enc[0] ^ fakebit) with open("realkey", "wb") as outfile: asbytes = streamofbits.bits_to_bytes(realkey) outfile.write(bytes(asbytes)) for i,fakekey in enumerate(fakekeys): with open("fakekey{0}".format(i), "wb") as outfile: asbytes = streamofbits.bits_to_bytes(fakekey) outfile.write(bytes(asbytes))
def main(): if len(sys.argv) != 3: print("usage: python3 decryptwithkey.py <infile> <keyfile>") return inbits = streamofbits.bits(sys.argv[1]) inkeybits = streamofbits.bits(sys.argv[2]) cipherbits = list(inbits) keybits = list(inkeybits) plainbits = [] encodinglength = constants.ELEMENT_LENGTH * constants.ELEMENTS_PER_BIT encodeds = streamofbits.n_at_a_time(cipherbits, encodinglength) for encoded,keybit in zip(encodeds, keybits): nextbit = encoded[0] ^ keybit plainbits.append(nextbit) print(bytes.decode(bytes(streamofbits.bits_to_bytes(plainbits))), end="")
def main(): secret = trapdoor.loadtrapdoor() if len(sys.argv) != 2: print("usage: python3 decrypt.py <infile>") return inbits = streamofbits.bits(sys.argv[1]) cipherbits = list(inbits) plainbits = [] for encoded in streamofbits.n_at_a_time(cipherbits, constants.ELEMENT_LENGTH * constants.ELEMENTS_PER_BIT): numS = countSs(encoded, secret) plainbits.append(numS % 2) print(bytes.decode(bytes(streamofbits.bits_to_bytes(plainbits))), end="")