commit 34d51f1d77f4f0a9b59aa6b81a266f541283c9fc
parent a855bb79bd062ab5f0a79230e397ddaf9828db12
Author: mpizzzle <michael.770211@gmail.com>
Date: Sat, 28 Oct 2017 18:03:41 +0100
minor refactors in several files
Diffstat:
12 files changed, 17 insertions(+), 42 deletions(-)
diff --git a/set1/break_repeating_key_xor.py b/set1/break_repeating_key_xor.py
@@ -1,12 +1,10 @@
-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')
def get_candidate_key_length(file, accuracy):
- candidate_distance = sys.float_info.max
+ candidate_distance = 9999999
candidate_length = 0
for key_length in range(2, 40):
@@ -23,7 +21,7 @@ def get_candidate_key_byte(transposed_block):
candidate = ''
candidate_frequency = 0
- for c in range(128):
+ for c in range(0xff):
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))])
diff --git a/set1/detect_single_character_xor.py b/set1/detect_single_character_xor.py
@@ -1,5 +1,3 @@
-import sys
-
with open('files/4.txt') as f:
hex_strings = f.read().splitlines()
diff --git a/set1/detect_single_character_xor_char_frequency.py b/set1/detect_single_character_xor_char_frequency.py
@@ -1,5 +1,3 @@
-import sys
-
with open('files/4.txt') as f:
hex_strings = f.read().splitlines()
diff --git a/set1/fixed_xor.py b/set1/fixed_xor.py
@@ -1,6 +1,4 @@
-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")
+print ''.join([chr(ord(a) ^ ord(b)) for a, b in zip(key.decode("hex"), msg.decode("hex"))])
diff --git a/set1/hex_to_base64.py b/set1/hex_to_base64.py
@@ -1,5 +1,3 @@
-import sys
-
hex = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
-print hex.decode("hex").encode("base64")-
\ No newline at end of file
+print hex.decode("hex").encode("base64")
diff --git a/set1/repeating_key_xor.py b/set1/repeating_key_xor.py
@@ -1,5 +1,3 @@
-import sys
-
def encrypt(key, msg):
return ''.join([chr(ord(key[i % len(key)]) ^ ord(char)) for i, char in enumerate(msg)])
diff --git a/set1/single_byte_xor_cipher.py b/set1/single_byte_xor_cipher.py
@@ -1,5 +1,3 @@
-import sys
-
hex = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"
for plaintext in [''.join([chr(x ^ ord(a)) for a in hex.decode("hex")]) for x in range(128)]:
diff --git a/set1/single_byte_xor_cipher_char_frequency.py b/set1/single_byte_xor_cipher_char_frequency.py
@@ -1,5 +1,3 @@
-import sys
-
hex = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"
frequent_letters = "etaoi "
candidate = ""
diff --git a/set2/byte_at_a_time_ecb_decryption.py b/set2/byte_at_a_time_ecb_decryption.py
@@ -1,4 +1,3 @@
-import sys
from Crypto.Cipher import AES
from Crypto import Random
diff --git a/set2/byte_at_a_time_ecb_decryption_harder.py b/set2/byte_at_a_time_ecb_decryption_harder.py
@@ -1,4 +1,3 @@
-import sys
from Crypto.Cipher import AES
from Crypto import Random
from Crypto.Random import random
diff --git a/set2/ecb_cbc_detection_oracle.py b/set2/ecb_cbc_detection_oracle.py
@@ -6,37 +6,30 @@ from sets import Set
def random_key():
return Random.new().read(AES.block_size)
-def encryption_oracle(key, msg):
+def encryption_oracle(msg):
plaintext = msg
ciphertext = ""
for i in range(random.randint(5, 10)):
- plaintext = '\x04' + plaintext
+ plaintext = '\x00' + plaintext
for i in range(random.randint(5, 10)):
- plaintext += '\x04'
+ plaintext += '\x00'
- mod = len(plaintext) % AES.block_size
-
- if mod != 0:
- for i in range(AES.block_size - mod):
- plaintext = plaintext + '\x04'
+ pad_len = AES.block_size - (len(plaintext) % AES.block_size)
+ plaintext += ''.join([chr(pad_len) for i in range(pad_len)])
if random.randint(0, 1):
- ciphertext = AES.new(key, AES.MODE_ECB).encrypt(plaintext)
- print '1'
+ ciphertext = AES.new(random_key(), AES.MODE_ECB).encrypt(plaintext)
+ print True
else:
- ciphertext += AES.new(key, AES.MODE_CBC, random_key()).encrypt(plaintext)
- print '0'
+ ciphertext = AES.new(random_key(), AES.MODE_CBC, random_key()).encrypt(plaintext)
+ print False
return ciphertext
with open('files/10_decrypted.txt') as f:
- file = f.read()
+ ciphertext = encryption_oracle(f.read())
-ciphertext = encryption_oracle(random_key(), file)
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'
-else:
- print '0'
+print len(blocks) != len(Set(blocks))
diff --git a/set2/ecb_cut_and_paste.py b/set2/ecb_cut_and_paste.py
@@ -8,7 +8,8 @@ def random_key():
key = random_key()
def encrypt(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)
+ pad_len = AES.block_size - (len(msg) % AES.block_size)
+ return AES.new(key, AES.MODE_ECB).encrypt(msg + ''.join([chr(pad_len) for i in range(pad_len)]))
def decrypt_and_parse(cipher):
return parse_string_to_dict(AES.new(key, AES.MODE_ECB).decrypt(cipher))