cryptopals

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 537195233bef28dbfecfddd536133b21ee8860a8
parent 8dd7af8dd56090f744dc5f8d3789874035e888d6
Author: mpizzzle <michael.770211@gmail.com>
Date:   Sun, 15 Oct 2017 14:24:55 +0100

minor refactoring

Diffstat:
Mset1/break_repeating_key_xor.py | 53++++++++++++++++++++++++++---------------------------
Mset1/detect_single_character_xor.py | 9++++-----
Mset1/detect_single_character_xor_char_frequency.py | 23+++++++++++------------
Mset1/fixed_xor.py | 5++---
Mset1/repeating_key_xor.py | 5++---
Mset1/single_byte_xor_cipher.py | 5++---
Mset2/aes_in_cbc_mode.py | 4++--
Mset2/byte_at_a_time_ecb_decryption.py | 2+-
Mset2/ecb_cbc_detection_oracle.py | 6+++---
9 files changed, 53 insertions(+), 59 deletions(-)

diff --git a/set1/break_repeating_key_xor.py b/set1/break_repeating_key_xor.py @@ -3,44 +3,44 @@ import sys frequent_letters = "etaoi ETAOI" def distance(str1, str2): - return ''.join([bin(ord(a) ^ ord(b)) for a, b in zip(str1, str2)]).count('1') + return ''.join([bin(ord(a) ^ ord(b)) for a, b in zip(str1, str2)]).count('1') def get_candidate_key_length(file, accuracy): - candidate_distance = sys.float_info.max - candidate_length = 0 + candidate_distance = sys.float_info.max + candidate_length = 0 - for key_length in range(2, 40): - this_distance = sum([distance(file[key_length * x:key_length * (x + 1)], file[key_length * (x + 1):key_length * (x + 2)]) for x in range(accuracy)]) - average_distance = this_distance / float(key_length * (accuracy)) + for key_length in range(2, 40): + this_distance = sum([distance(file[key_length * x:key_length * (x + 1)], file[key_length * (x + 1):key_length * (x + 2)]) for x in range(accuracy)]) + average_distance = this_distance / float(key_length * (accuracy)) - if average_distance < candidate_distance: - candidate_distance = average_distance - candidate_length = key_length + if average_distance < candidate_distance: + candidate_distance = average_distance + candidate_length = key_length - return candidate_length + return candidate_length def get_candidate_key_byte(transposed_block): - candidate = '' - candidate_frequency = 0 + candidate = '' + candidate_frequency = 0 - for c in range(128): - plaintext = ''.join([chr(c ^ ord(a)) for a in transposed_block]) - frequency = sum([plaintext.count(frequent_letters[n]) for n in range(len(frequent_letters))]) + for c in range(128): + plaintext = ''.join([chr(c ^ ord(a)) for a in transposed_block]) + frequency = sum([plaintext.count(frequent_letters[n]) for n in range(len(frequent_letters))]) - if frequency > candidate_frequency: - candidate = chr(c) - candidate_frequency = frequency + if frequency > candidate_frequency: + candidate = chr(c) + candidate_frequency = frequency - return candidate + return candidate def decrypt(key, msg): - return ''.join([chr(ord(key[i % len(key)]) ^ ord(char)) for i, char in enumerate(msg)]) - + return ''.join([chr(ord(key[i % len(key)]) ^ ord(char)) for i, char in enumerate(msg)]) + def get_key(file): - key_length = get_candidate_key_length(file, 10) - split_file = [file[i:i + key_length] for i in range(0, len(file), key_length)] - transposed_blocks = [''.join([block[x] for block in split_file[:len(split_file) - 1]]) for x in range(key_length)] - return ''.join([get_candidate_key_byte(block) for block in transposed_blocks]) + key_length = get_candidate_key_length(file, 10) + split_file = [file[i:i + key_length] for i in range(0, len(file), key_length)] + transposed_blocks = [''.join([block[x] for block in split_file[:len(split_file) - 1]]) for x in range(key_length)] + return ''.join([get_candidate_key_byte(block) for block in transposed_blocks]) with open('files/6.txt') as f: file = f.read().decode("base64") @@ -52,4 +52,4 @@ key1 = get_key(file) key2 = get_key(project_euler_59) print "key: \"" + key1 + "\"\n" + decrypt(key1, file) -print "key: \"" + key2 + "\"\n" + decrypt(key2, project_euler_59)- \ No newline at end of file +print "key: \"" + key2 + "\"\n" + decrypt(key2, project_euler_59) diff --git a/set1/detect_single_character_xor.py b/set1/detect_single_character_xor.py @@ -2,8 +2,8 @@ import sys with open('files/4.txt') as f: hex_strings = f.read().splitlines() - + for hex in hex_strings: - for plaintext in [''.join([chr(x ^ ord(a)) for a in hex.decode("hex")]) for x in range(128)]: - if " the " in plaintext: - print plaintext- \ No newline at end of file + for plaintext in [''.join([chr(x ^ ord(a)) for a in hex.decode("hex")]) for x in range(128)]: + if " the " in plaintext: + print plaintext diff --git a/set1/detect_single_character_xor_char_frequency.py b/set1/detect_single_character_xor_char_frequency.py @@ -8,15 +8,15 @@ candidate = "" candidate_frequency = 0 for hex in hex_strings: - for plaintext in [''.join([chr(x ^ ord(a)) for a in hex.decode("hex")]) for x in range(128)]: - frequency = 0 + for plaintext in [''.join([chr(x ^ ord(a)) for a in hex.decode("hex")]) for x in range(128)]: + frequency = 0 - for char in plaintext: - if char in frequent_letters: - frequency += 1 - - if frequency > candidate_frequency: - candidate = plaintext - candidate_frequency = frequency - -print candidate- \ No newline at end of file + for char in plaintext: + if char in frequent_letters: + frequency += 1 + + if frequency > candidate_frequency: + candidate = plaintext + candidate_frequency = frequency + +print candidate diff --git a/set1/fixed_xor.py b/set1/fixed_xor.py @@ -2,5 +2,5 @@ import sys key = "1c0111001f010100061a024b53535009181c" msg = "686974207468652062756c6c277320657965" - -print ''.join([chr(ord(a) ^ ord(b)) for a, b in zip(key.decode("hex"), msg.decode("hex"))]).encode("hex")- \ No newline at end of file + +print ''.join([chr(ord(a) ^ ord(b)) for a, b in zip(key.decode("hex"), msg.decode("hex"))]).encode("hex") diff --git a/set1/repeating_key_xor.py b/set1/repeating_key_xor.py @@ -1,10 +1,10 @@ import sys def encrypt(key, msg): - return ''.join([chr(ord(key[i % len(key)]) ^ ord(char)) for i, char in enumerate(msg)]) + return ''.join([chr(ord(key[i % len(key)]) ^ ord(char)) for i, char in enumerate(msg)]) plaintext = "Burning 'em, if you ain't quick and nimble" plaintext2 = "I go crazy when I hear a cymbal" key = "ICE" -print encrypt(key, plaintext + "\n" + plaintext2).encode("hex")- \ No newline at end of file +print encrypt(key, plaintext + "\n" + plaintext2).encode("hex") diff --git a/set1/single_byte_xor_cipher.py b/set1/single_byte_xor_cipher.py @@ -3,5 +3,5 @@ import sys hex = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736" for plaintext in [''.join([chr(x ^ ord(a)) for a in hex.decode("hex")]) for x in range(128)]: - if " a " in plaintext: - print plaintext- \ No newline at end of file + if " a " in plaintext: + print plaintext diff --git a/set2/aes_in_cbc_mode.py b/set2/aes_in_cbc_mode.py @@ -3,10 +3,10 @@ from Crypto.Cipher import AES with open('files/10.txt') as f: file = f.read().decode("base64") -split_file = [file[i:i + 16] for i in range(0, len(file), 16)] +split_file = [file[i:i + AES.block_size] for i in range(0, len(file), AES.block_size)] key = "YELLOW SUBMARINE" aes = AES.new(key, AES.MODE_ECB) -iv = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +iv = ''.join(['\x00' for i in range(AES.block_size)]) plaintext = "" for cipher_text in split_file: diff --git a/set2/byte_at_a_time_ecb_decryption.py b/set2/byte_at_a_time_ecb_decryption.py @@ -6,7 +6,7 @@ def random_key(): return Random.new().read(AES.block_size) def encryption_oracle(key, msg): - return AES.new(key, AES.MODE_ECB).encrypt(msg + ''.join(['\x04' for i in range(16 - (len(msg) % 16))]) if len(msg) % 16 != 0 else msg) + return AES.new(key, AES.MODE_ECB).encrypt(msg + ''.join(['\x04' for i in range(AES.block_size - (len(msg) % AES.block_size))]) if len(msg) % AES.block_size != 0 else msg) pt1 = "Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkg\n" pt2 = "aGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq\n" diff --git a/set2/ecb_cbc_detection_oracle.py b/set2/ecb_cbc_detection_oracle.py @@ -15,10 +15,10 @@ def encryption_oracle(key, msg): for i in range(random.randint(5, 10)): plaintext += '\x04' - mod = len(plaintext) % 16 + mod = len(plaintext) % AES.block_size if mod != 0: - for i in range(16 - mod): + for i in range(AES.block_size - mod): plaintext = plaintext + '\x04' if random.randint(0, 1): @@ -34,7 +34,7 @@ with open('files/10_decrypted.txt') as f: file = f.read() ciphertext = encryption_oracle(random_key(), file) -blocks = [ciphertext[i:i + 16] for i in range(0, len(ciphertext), 16)] +blocks = [ciphertext[i:i + AES.block_size] for i in range(0, len(ciphertext), AES.block_size)] if len(blocks) != len(Set(blocks)): print '1'