Exemple #1
0
 def test_transpose_to_str_edge_case(self):
     e = BA("0xff00aa00")
     blocks = transpose(e, 2)
     self.assertEquals(len(blocks), 2)
     self.assertEquals(BA('0xffaa'), blocks[0])
     to_str(blocks[0])
     self.assertEquals(BA('0x0000'), blocks[1])
Exemple #2
0
 def test_transpose3(self):
     e = BA("0x0b3637")
     blocks = transpose(e, 3)
     self.assertEqual(len(blocks), 3)
     self.assertEqual(BA('0x0b'), blocks[0])
     self.assertEqual(BA('0x36'), blocks[1])
     self.assertEqual(BA('0x37'), blocks[2])
Exemple #3
0
 def test_transpose2(self):
     e = BA("0x00112233445566778899")
     blocks = transpose(e, 4)
     self.assertEqual(len(blocks), 4)
     self.assertEquals(BA('0x004488'), blocks[0])
     self.assertEquals(BA('0x115599'), blocks[1])
     self.assertEquals(BA('0x2266'), blocks[2])
     self.assertEquals(BA('0x3377'), blocks[3])
Exemple #4
0
    def test_bytes_to_ba(self):
        b = BA("0x0000")
        bs = [b.bytes[0], b.bytes[1]]
        self.assertEquals(BA("0x0000"), bytes_to_ba(bs))

        b = BA("0xffaa")
        bs = [b.bytes[0], b.bytes[1]]
        self.assertEquals(BA("0xffaa"), bytes_to_ba(bs))
Exemple #5
0
    def test_letter_distribution1(self):
        ba1 = BA(b'Cooking MC\'s like a pound of bacon')
        s1 = realistic_letter_distribution(ba1)

        ba2 = BA(
            "0x705c5c585a5d54137e701440135f5a5856135213435c465d57135c55135152505c5d"
        )
        s2 = realistic_letter_distribution(ba2)

        self.assertTrue(s1 > s2)
Exemple #6
0
    def test_letter_distribution2(self):
        good = BA(
            "0x7b5a4215415d544115415d5015455447414c155c46155f4058455c5b523f")
        s1 = realistic_letter_distribution(good)

        fake = BA(
            "0x1c3df1135321a8e9241a5607f8305d571aa546001e3254555a11511924")
        s2 = realistic_letter_distribution(fake)

        self.assertTrue(s1 > s2)
Exemple #7
0
    def test_decrypt(self):
        # I guess assume these people are thinking in C
        # Single char is 1 byte (8 bit)
        e = BA(
            "0x1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"
        )
        self.assertEqual("Cooking MC's like a pound of bacon",
                         find_best_message(e))

        key = find_best_key(e)
        d = BA(hex(ord(key)) * len(e.bytes))
        self.assertEqual("Cooking MC's like a pound of bacon", to_str(e ^ d))
Exemple #8
0
    def test_letter_distribution(self):
        ba1 = BA(
            b'He determined to drop his litigation with the monastry, and relinguish his claims to the wood-cuting and fishery rihgts at once. He was the more ready to do this becuase the rights had becom much less valuable, and he had indeed the vaguest idea where the wood and river in quedtion were.'
        )
        s1 = realistic_letter_distribution(ba1)

        ba2 = BA(
            b'kjafjkf adfsjkafdsj 345oislnasdlkj qlktj oqeijg 0340hqi0jwqicdwjiwkpqwop lmknxlmc,zmcj adfi  iu qwpjaljkdfjdslk f.jk.mcvnvcn vndpijpifuw9- kls adlksajm.slksapij dfp- qw jkasjkad djkadsnkvlsadkjaw 9 e2j jadjlkadladfs jklajk ej ;j awefjlkasadjw pjowe ew ipjewji ewj iwejfal kas advpceq qe 9'
        )
        s2 = realistic_letter_distribution(ba2)

        self.assertTrue(s1 > s2)
Exemple #9
0
def attempt_all_keys(e: BA) -> [(int, chr, str, BA)]:
    scores = []
    e = pad8(e)
    for i in range(1, 256):
        if i < 16:
            key = BA(hex(i) * int(len(e) / float(4)))
        else:
            key = BA(hex(i) * int(len(e) / float(8)))

        candidate = (e ^ key)

        scores.append((realistic_letter_distribution(candidate), chr(i),
                       to_str(candidate), candidate, e))

    return scores
Exemple #10
0
 def test_example(self):
     x = b'Burning \'em, if you ain\'t quick and nimble\nI go crazy when I hear a cymbal'
     e = xor_encrypt_ba(b'ICE', x)
     a_ = BA(
         "0x0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f"
     )
     self.assertEqual(e, a_)
Exemple #11
0
 def test_end_to_end(self):
     e = xor_encrypt_ba(b'ICE', SAMPLE_TEXT.encode())
     print(to_str(e))
     t = top_n_key_sizes(20, e)
     pprint(t)
     blocks = transpose(e, 3)
     key = ""
     for i, b in enumerate(blocks):
         key += best_decrypt_key(b)[1]
     print(key)
     m = to_str(xor_cycle_encrypt(BA(key.encode()), e))
     print(m)
Exemple #12
0
def bytes_to_ba(bs: [int]) -> BA:
    l = []
    for b in bs:
        if b == 0:
            l.append("0x00")
        elif b < 15:
            l.append("0x0" + hex(b))
        else:
            l.append(hex(b))
    i = map(lambda s: s[2:], l)
    s = "0x" + "".join(i)
    return BA(s)
Exemple #13
0
 def test_decrypt_sample(self):
     # Make sure we can find the right key
     a = BA(
         "0x0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f"
     )
     blocks = transpose(a, 3)
     key = ""
     for i, b in enumerate(blocks):
         key += best_decrypt_key(b)[1]
     self.assertEqual("ICE", key)
     s = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"
     self.assertEqual(decrypt_to_str("ICE", a), s)
Exemple #14
0
def find_best_message(e: BA) -> str:
    scores = []
    for c in map(chr, range(256)):
        i = ord(c)
        key = BA(hex(i) * len(e.bytes))
        if len(key) < len(e):
            key = key * 2
        candidate = (e ^ key)

        scores.append((dictionary_word_count(candidate), c, to_str(candidate)))

    best = sorted(scores, key=lambda x: x[0])[-1]
    return best[2]
Exemple #15
0
    def test_example(self):
        # I guess assume these people are thinking in C
        # Single char is 1 byte (8 bit)
        scores = []
        blocks = []
        with open("challenge_inputs/4.txt", "r") as fd:
            for line in fd.readlines():
                ba = BA("0x" + line.strip())
                blocks.append(ba)

        for block in blocks:
            scores.append(best_decrypt_key(block))

        best = list(reversed(sorted(scores, key=lambda x: x[0])))
        self.assertEqual(best[0][2], "Now that the party is jumping\n")
Exemple #16
0
def find_best_key(e: BA) -> chr:
    scores = []
    for c in map(chr, range(256)):
        i = ord(c)
        ks = int((len(e) / 8))
        key = BA(hex(i)) * ks
        if len(key) < len(e):
            key = key * 2

        assert len(key) == len(e), "len(key)=%s len(e)=%s" % (len(key), len(e))
        candidate = (e ^ key)

        scores.append((realistic_letter_distribution(candidate), c))

    best = sorted(scores, key=lambda x: x[0])
    return best[-1][1]
def compactbit(b):
    b = np.array(b)
    nSamples, nbits = b.shape
    nwords = np.uint8(math.ceil(float(nbits)/8.0))
    cb = np.zeros((nSamples, nwords)).astype(np.uint8)

    for j in range(0, nbits):
        w = np.uint8(math.ceil(float(j+1)/8.0)) - 1
        cb_tmp = [BA('uint:8='+str(x)) for x in cb[:, w]]
        for x, y in zip(cb_tmp, b[:, j]):
            x.set(y, (7 - (j % 8)))

        cb_tmp = [x.uint for x in cb_tmp]
        cb[:, w] = cb_tmp

    return cb
Exemple #18
0
    def test_solve_for_key(self):
        e = challenge_input()
        keys = []
        for ks in KEY_SIZE:
            blocks = transpose(e, ks)
            key = ""
            for i, b in enumerate(blocks):
                best_keys = top_n_decrypt_key(5, b)
                key += best_keys[0][1]
                cs = list(map(lambda x: x[1], best_keys))
                print(cs)
            keys.append(key)
            print("%s: %s" % (ks, key))

        e = challenge_input()
        for k in keys:
            print(to_str(xor_cycle_encrypt(BA(k.encode()), e)))
Exemple #19
0
def decrypt_to_str(key, e):
    return to_str(xor_cycle_encrypt(BA(key.encode()), e))
Exemple #20
0
def b64_to_ba(s) -> BA:
    return BA(a2b_base64(s))
Exemple #21
0
def pad8(candidate: BA) -> BA:
    hangover = 8 - (len(candidate) % 8)
    if hangover != 8:
        candidate += BA("0x0") * (hangover / 4)
    return candidate
Exemple #22
0
def str_to_ba(i: str) -> BA:
    return BA(i.encode())
Exemple #23
0
 def test_decrypt_I_block(self):
     # Make sure that the transpose blocks allow us to detect "C" as the 2nd key char
     e = BA('0x362a632e2a3a632d632a63272a2f0a2c313a2b632b31632e2f')
     keys = top_n_decrypt_key(50, e)
     self.assertEqual("C", keys[0][1])
Exemple #24
0
 def test_hamming_weight2(self):
     a = BA(b'this is a test')
     b = BA(b'wokka wokka!!!')
     self.assertEqual(hamming_weight(a, b), 37)
Exemple #25
0
def xor_encrypt(key_text, ms) -> str:
    key_ = BA(key_text.encode())
    encrypt = xor_cycle_encrypt(key_, BA(ms.encode()))
    return encrypt.hex
Exemple #26
0
def xor_encrypt_ba(key_text: bytes, ms: bytes) -> BA:
    key_ = BA(key_text)
    encrypt = xor_cycle_encrypt(key_, BA(ms))
    return encrypt