Beispiel #1
0
def main():
    ciphertext = bytearray(open('1-6.input', 'rb').read().decode('base64'))
    guess_keysize(ciphertext, 2, 40, 30) # 29 bytes has lowest score
    vignere_profile(ciphertext, 29, 1)

    key = [84, 101, 114, 109, 105, 110, 97, 116, 111, 114, 32, 88, 58, 32, 66, 114, 105, 110, 103, 32, 116, 104, 101, 32, 110, 111, 105, 115, 101] # Terminator X: Bring the noise
    decoded_cipher = byte_conversions.change_format(byte_conversions.binary_xor_cyclic(''.join(format(i, '08b') for i in ciphertext), ''.join(format(i, '08b') for i in key)), "bin2hex")
    print decoded_cipher.decode("hex")
Beispiel #2
0
def vignere_profile(ciphertext, keysize, score_threshold):
    for register_shift in range(0,keysize):
        guess_dict = {}
        for key in range(0,255):
            guess = byte_conversions.change_format(byte_conversions.binary_xor_cyclic(''.join(format(i, '08b') for i in ciphertext[register_shift::keysize]), format(key, '08b')), "bin2hex")
            score = score_plaintext.score_prob(guess.decode("hex").lower())
        
            if score < 0.6:
                guess_dict[score] = [key, guess.decode("hex")]
        
        for key in sorted(guess_dict, reverse=True):
            print "Register %s -- %s: %s" % (register_shift, key, guess_dict[key])
Beispiel #3
0
import byte_conversions
import score_plaintext


guess_dict = {}
with open("1-4.input", "r") as encrypted_list:
    for encrypted_text in encrypted_list:
        encrypted_text = encrypted_text.rstrip('\n')
        secret_binary = byte_conversions.change_format(str(encrypted_text), "hex2bin")
        for key in range(0,255):
            guess = byte_conversions.change_format(byte_conversions.binary_xor_cyclic(secret_binary, '{0:08b}'.format(key)), "bin2hex")
            score = score_plaintext.score_freq(guess.decode("hex").lower())
            
            if score > 0:
                guess_dict[score] = [guess.decode("hex"), encrypted_text]
        
for key in sorted(guess_dict):
    print "%s: %s" % (key, guess_dict[key])
Beispiel #4
0
import byte_conversions
import score_plaintext

secret_binary = byte_conversions.change_format(
    "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736", "hex2bin"
)
guess_dict = {}
for key in range(0, 255):
    guess = byte_conversions.change_format(
        byte_conversions.binary_xor_cyclic(secret_binary, "{0:08b}".format(key)), "bin2hex"
    )
    score = score_plaintext.score_prob(guess.decode("hex").lower())
    print "Score %s -- %s: %s" % (score, "{0:08b}".format(key), guess.decode("hex"))

    if score > 0:
        guess_dict[score] = guess.decode("hex")

for key in sorted(guess_dict):
    print "%s: %s" % (key, guess_dict[key])