Exemplo n.º 1
0
    def test_1_fixed_length_XOR_with_unequal_length_buffers_should_truncate(
            self):
        a = '5468697320697320612074657374206d6573736167652e'
        b = '4e61727920626565662e20556e6c65737320646561642e'
        excess = 'deadbeef' * 5
        expected = bytes.fromhex(
            '1a091b0a000b1645070e54301d18451e16531704060100')

        result0 = fixedXOR(a + excess, b)
        result1 = fixedXOR(a, b + excess)
        self.assertEqual(result0, expected)
        self.assertEqual(result1, expected)
Exemplo n.º 2
0
def _crackRepeatingXOR(byteObject, frequentLetters, keySizeRange, blockNumber):
    if not isBytes(byteObject):
        raise ValueError("Input must be a bytes object")

    keySizes = getLikelyKeySizes(byteObject,
                                 keySizeRange,
                                 blockNumber,
                                 howMany=3)
    bestGuess = Candidate(0, b'', b'', b'')
    for keySize in keySizes:
        blocks = divideIntoBlocks(byteObject, keySize)
        possibleKey = bytearray()
        for block in blocks:
            candidate = _crackSingleXOR(block, frequentLetters)
            # This key length can never be the correct key length
            if candidate.key == b'':
                break

            possibleKey.append(candidate.key[0])

        if len(possibleKey) != keySize:
            continue

        possibleKey = bytes(possibleKey)
        secret = fixedXOR(byteObject,
                          repeatPerCharacter(possibleKey, len(byteObject)))
        score = calculateScore(secret, frequentLetters)
        if score > bestGuess.score and encodedByUTF8(secret):
            bestGuess = Candidate(score, possibleKey, byteObject, secret)

    return bestGuess
Exemplo n.º 3
0
    def test_0_fixed_length_XOR_with_equal_length_buffers(self):
        a = '5468697320697320612074657374206d6573736167652e'
        b = '4e61727920626565662e20556e6c65737320646561642e'
        expected = bytes.fromhex(
            '1a091b0a000b1645070e54301d18451e16531704060100')

        result = fixedXOR(a, b)
        self.assertEqual(result, expected)
Exemplo n.º 4
0
def fixed_XOR():
    # Set up
    args = getArgs()
    challengeData = getData()[getFilename(__file__)]
    if args.input:
        challengeData['values'] = args.input

    # Complete challenge
    values = challengeData['values']
    output = fixedXOR(values[0], values[1])
    challengeData['output'] = bytes2hex(output)
    return challengeData
Exemplo n.º 5
0
def _crackSingleXOR(byteObject, frequentLetters):
    if not isBytes(byteObject):
        raise ValueError("Input must be a bytes object")

    bestCandidate = Candidate(0, b'', b'', b'')
    # We skip 0, since XORing against a bunch of 0s isn't going to do anything
    byteLength = len(byteObject)
    for i in range(1, 256):
        possibleKey = repeatPerCharacter(bytes([i]), byteLength)
        secret = fixedXOR(byteObject, possibleKey)
        score = calculateScore(secret, frequentLetters)
        if score > bestCandidate.score and encodedByUTF8(secret):
            bestCandidate = Candidate(score, possibleKey, byteObject, secret)

    return bestCandidate