Пример #1
0
def findEncryptedLine(fileName):
	lines = [line.rstrip('\n') for line in open(fileName)]
	score_decryptedLine_map = {}

	for line in lines:
		decodedLine = decodeMessage(line)
		score_decryptedLine_map[score(decodedLine)] = decodedLine
	
	return score_decryptedLine_map[max(score_decryptedLine_map)]
Пример #2
0
def single_byte_decode_file(f):
	results_scores = {}
	results_meta = {}
	for i,line in enumerate(open(f).read().strip().split("\n")):
		result = challenge3.single_byte_decode(line)
		results_scores[result[1]] = challenge3.score(result[1])
		results_meta[result[1]] = [result[0],i] 
	answer = max(results_scores, key=results_scores.get)
	return results_meta[answer]+[answer]
Пример #3
0
    def fixed_len_repeated_key_xor_decrypt(keylen):
        """
        Decrypts a CT hex string that has been encrypted by a repeating key
        XOR with a key of a known length

        @param keylen [int]: Repitition length of the repeating key.
        @returns [tuple]: ([int], [str], [int]) where t[0] is the score
                                                      t[1] is the PT
                                                      t[2] is the key length
        """

        p = Pool(10)
        pt_segments = p.map(
            decrypt, [''.join(ct_split[i::keylen]) for i in range(keylen)])
        pt_score = sum([score(pt_segment) for pt_segment in pt_segments])
        # zip the segments
        pt = ''.join([
            pt_segments[j][i] for i in range(len(pt_segments[0]))
            for j in range(len(pt_segments)) if i < len(pt_segments[j])
        ])
        return (pt_score, pt, keylen)
Пример #4
0
 def score(i):
     return challenge3.score(brokenLines[i])
Пример #5
0

def getLines(fname):
    with open(fname, 'rt') as f:
        for line in f:
            yield bytes.fromhex(line.strip())


if __name__ == '__main__':
    lines = getLines('4.txt')
    best_score = 99999999
    best_solve = None
    best_line_no = None
    # while this could likely be minimized to a single min call...
    # the result wouldn't be as readable or as debuggable
    for line_no, line in enumerate(lines):
            key = challenge3.find_key(line)
            solved = challenge3.xor_single(key, line)
            score = challenge3.score(solved)
            if score < best_score:
                try:
                    decoded = solved.decode()
                    # print(line_no, key, score, decoded)
                    best_key = key
                    best_solve = decoded
                    best_score = score
                    best_line_no = line_no
                except UnicodeDecodeError:
                    pass
    print(best_line_no, best_key, best_score, best_solve)
Пример #6
0
 def key(p):
     return score(p[1])
Пример #7
0
def work(s):
    pt = decrypt(s)
    return (score(pt), pt)
Пример #8
0
import base64
from challenge3 import find_best_string, score

if __name__ == "__main__":
  lines = [line.strip() for line in open('input/4.txt').readlines()]
  strings = [find_best_string(line) or '' for line in lines]
  scores = [score([x for x in bytes(string,'utf8')]) for key, string in strings]
  key, decrypted = strings[scores.index(max(scores))]
  decrypted = decrypted.strip()
  assert(decrypted == "Now that the party is jumping")
  print(" xor %r => %r" % (key, decrypted))