Skip to content

Instantly share code, notes, and snippets.

View mikeecb's full-sized avatar

Michael Cypher mikeecb

View GitHub Profile
@mikeecb
mikeecb / cryptopals_1_6.py
Last active August 20, 2021 21:02
Cryptopals Challenge Set 1 Exercise 6
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
@mikeecb
mikeecb / cryptopals_1_6_simplified.py
Last active August 18, 2020 22:19
Cryptopals Challenge Set 1 Exercise 6 Simplified
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))
@mikeecb
mikeecb / cryptopals_1_7.py
Last active August 18, 2020 22:19
Cryptopals Challenge Set 1 Exercise 7
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
@mikeecb
mikeecb / cryptopals_1_8.py
Last active August 18, 2020 22:19
Cryptopals Challenge Set 1 Exercise 8
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
@mikeecb
mikeecb / cryptopals_1_9.py
Last active August 18, 2020 22:19
Cryptopals Challenge Set 1 Exercise 9
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)
@mikeecb
mikeecb / cryptopals_1_10.py
Last active August 18, 2020 22:18
Cryptopals Challenge Set 1 Exercise 10
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)
@mikeecb
mikeecb / cryptopals_1_10_simplified.py
Last active August 18, 2020 22:18
Cryptopals Challenge Set 1 Exercise 10 Simplified
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)
@mikeecb
mikeecb / unpad.py
Last active August 18, 2020 22:18
Unpad PKCS#7
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
@mikeecb
mikeecb / cryptopals_1_10_decrypted.py
Last active August 18, 2020 22:18
Cryptopals Challenge Set 1 Exercise 10 Decrypted
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
@mikeecb
mikeecb / random_key.py
Created April 25, 2017 12:58
Random Key
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')