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 optimize_comprehensions import optimize_comprehensions | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import timeit | |
from functools import partial | |
from time import sleep | |
def list_comprehension(expensive_func): |
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
import inspect | |
from ast import comprehension [136/153] | |
from ast import dump | |
from ast import fix_missing_locations | |
from ast import parse | |
from ast import List | |
from ast import Load | |
from ast import DictComp | |
from ast import GeneratorExp | |
from ast import SetComp |
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 | |
from random import randint | |
def xor(b1, b2): | |
b = bytearray(len(b1)) | |
for i in range(len(b1)): | |
b[i] = b1[i] ^ b2[i] | |
return b | |
def random_key(length): |
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 crack(): | |
first_block = bytearray('A' * AES.block_size) | |
second_block = bytearray("AadminAtrueA") | |
plaintext = first_block + second_block | |
ciphertext = encryption_oracle(plaintext) | |
# We 'know' the prefix is two blocks long | |
offset = 32 |
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
key = bytes(random_key(AES.block_size)) | |
iv = bytearray(random_key(AES.block_size)) | |
def encryption_oracle(input_data): | |
input_data = input_data.replace(';','%3b').replace('=','%3d') | |
plaintext = bytearray( | |
"comment1=cooking%20MCs;userdata=" + | |
input_data + | |
";comment2=%20like%20a%20pound%20of%20bacon" | |
) |
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_valid_pkcs7(buffer): | |
padding = buffer[-1] | |
for i in range(len(buffer)-1, len(buffer)-padding, -1): | |
if buffer[i] != buffer[-1]: | |
raise Exception("Bad PKCS#7 padding.") | |
new_buffer = bytearray() | |
new_buffer[:] = buffer[:-padding] | |
return new_buffer | |
unpad_valid_pkcs7(bytearray("ICE ICE BABY\x04\x04\x04\x03")) |
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
key = bytes(random_key(16)) | |
random_prefix = random_key(randint(0, 256)) | |
def encryption_oracle(data): | |
unknown_string = bytearray(( | |
"Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkg\n" + | |
"aGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq\n" + | |
"dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUg\n" + | |
"YnkK" | |
).decode("base64")) |
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 | |
from random import randint | |
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()) |
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 | |
from random import randint | |
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()) |
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 | |
from random import randint | |
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) |
NewerOlder