This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def xor(b1, b2): | |
b = bytearray(len(b1)) | |
for i in range(len(b1)): | |
b[i] = b1[i] ^ b2[i] | |
return b | |
def score(s): | |
freq = {} | |
freq[' '] = 700000000 | |
freq['e'] = 390395169 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for KEYSIZE, _ in normalized_distances[:5]: | |
block_bytes = [[] for _ in range(KEYSIZE)] | |
for i, byte in enumerate(b): | |
block_bytes[i % KEYSIZE].append(byte) | |
keys = "" | |
for bbytes in block_bytes: | |
keys += break_single_key_xor(bbytes)[0] | |
key = bytearray(keys * len(b)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Crypto.Cipher import AES | |
obj = AES.new("YELLOW SUBMARINE", AES.MODE_ECB) | |
ciphertext = "".join(list(open("7.txt", "r"))).decode("base64") | |
plaintext = obj.decrypt(ciphertext) | |
# I'm back and I'm ringin' the bell | |
# A rockin' on the mike while the fly girls yell | |
# In ecstasy in the back of me | |
# Well that's my DJ Deshay cuttin' all them Z's | |
# Hittin' hard and the girlies goin' crazy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
def repeated_blocks(buffer, block_length=16): | |
reps = defaultdict(lambda: -1) | |
for i in range(0, len(buffer), block_length): | |
block = bytes(buffer[i:i + block_length]) | |
reps[block] += 1 | |
return sum(reps.values()) | |
max_reps = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pad_pkcs7(buffer, block_size): | |
if len(buffer) % block_size: | |
padding = (len(buffer) / block_size + 1) * block_size - len(buffer) | |
else: | |
padding = 0 | |
# Padding size must be less than a byte | |
assert 0 <= padding <= 255 | |
new_buffer = bytearray() | |
new_buffer[:] = buffer | |
new_buffer += bytearray([chr(padding)] * padding) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Crypto.Cipher import AES | |
def xor(b1, b2): | |
b = bytearray(len(b1)) | |
for i in range(len(b1)): | |
b[i] = b1[i] ^ b2[i] | |
return b | |
def aes_128_ecb_enc(buffer, key): | |
obj = AES.new(key, AES.MODE_ECB) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def aes_128_ecb_enc(buffer, key): | |
obj = AES.new(key, AES.MODE_ECB) | |
return bytearray(obj.encrypt(bytes(buffer))) | |
def aes_128_ecb_dec(buffer, key): | |
obj = AES.new(key, AES.MODE_ECB) | |
return bytearray(obj.decrypt(bytes(buffer))) | |
def aes_128_cbc_enc(buffer, key, iv): | |
plaintext = pad_pkcs7(buffer, AES.block_size) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def unpad_pkcs7(buffer): | |
padding = buffer[-1] | |
for i in range(len(buffer)-1, len(buffer)-padding, -1): | |
if buffer[i] != buffer[-1]: | |
return buffer | |
new_buffer = bytearray() | |
new_buffer[:] = buffer[:-padding] | |
return new_buffer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ciphertext = bytearray("".join(list(open("10.txt", "r"))).decode("base64")) | |
aes_128_cbc_dec(ciphertext, key, iv) | |
# I'm back and I'm ringin' the bell | |
# A rockin' on the mike while the fly girls yell | |
# In ecstasy in the back of me | |
# Well that's my DJ Deshay cuttin' all them Z's | |
# Hittin' hard and the girlies goin' crazy | |
# Vanilla's on the mike, man I'm not lazy. | |
# I'm lettin' my drug kick in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randint | |
def random_key(length): | |
key = bytearray(length) | |
for i in range(length): | |
key[i] = chr(randint(0, 255)) | |
return key | |
print repr(random_key(16)) | |
# bytearray(b'\xe0L\xa7\xb3Z\xd6\xc0e\x87vc\xc4*\x96,\x14') |