Skip to content

Instantly share code, notes, and snippets.

@mikeecb
mikeecb / cryptopals_1_1.py
Last active August 18, 2020 22:20
Cryptopals Challenge Set 1 Exercise 1
hex_str = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
decoded = hex_str.decode("hex")
# I'm killing your brain like a poisonous mushroom
base64_str = decoded.encode("base64")
# SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t\n
@mikeecb
mikeecb / cryptopals_1_2.py
Last active August 18, 2020 22:20
Cryptopals Challenge Set 1 Exercise 2
def xor(b1, b2):
b = bytearray(len(b1))
for i in range(len(b1)):
b[i] = b1[i] ^ b2[i]
return b
b1 = bytearray.fromhex("1c0111001f010100061a024b53535009181c")
b2 = bytearray.fromhex("686974207468652062756c6c277320657965")
b = bytes(xor(b1, b2))
@mikeecb
mikeecb / cryptopals_1_3.py
Last active August 18, 2020 22:20
Cryptopals Challenge Set 1 Exercise 3
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_3_simplified.py
Last active August 18, 2020 22:20
Cryptopals Challenge Set 1 Exercise 3 Simplified
def score(s):
freq = {}
freq[' '] = 700000000
freq['e'] = 390395169
freq['t'] = 282039486
freq['a'] = 248362256
freq['o'] = 235661502
# ...
freq['z'] = 2456495
score = 0
@mikeecb
mikeecb / cryptopals_1_4.py
Last active August 18, 2020 22:19
Cryptopals Challenge Set 1 Exercise 4
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_4_simplified.py
Created April 20, 2017 12:50
Cryptopals Challenge Set 1 Exercise 4 Simplified
max_score = None
english_plaintext = None
key = None
for line in open("4.txt", "r"):
line = line.rstrip()
b1 = bytearray.fromhex(line)
for i in range(256):
@mikeecb
mikeecb / cryptopals_1_5_simplified.py
Last active August 18, 2020 22:19
Cryptopals Challenge Set 1 Exercise 5 Simplified
lines = [
"Burning 'em, if you ain't quick and nimble\n",
"I go crazy when I hear a cymbal",
]
text = "".join(lines)
key = bytearray("ICE" * len(text))
plaintext = bytes(xor(bytearray(text), key))
plaintext.encode("hex")
# 0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f
@mikeecb
mikeecb / cryptopals_1_5.py
Last active August 18, 2020 22:19
Cryptopals Challenge Set 1 Exercise 5
def xor(b1, b2):
b = bytearray(len(b1))
for i in range(len(b1)):
b[i] = b1[i] ^ b2[i]
return b
lines = [
"Burning 'em, if you ain't quick and nimble\n",
"I go crazy when I hear a cymbal",
]
@mikeecb
mikeecb / hamming_distance_simplified.py
Last active August 18, 2020 22:19
Hamming Distance Simplified
def hamming_distance(enc_str1, enc_str2):
differing_bits = 0
for byte in xor(b1, b2):
differing_bits += bin(byte).count("1")
return differing_bits
b1 = bytearray("this is a test")
b2 = bytearray("wokka wokka!!!")
hamming_distance(b1, b2)
@mikeecb
mikeecb / key_size_simplified.py
Last active August 18, 2020 22:19
Key Size Simplified
b = bytearray("".join(list(open("6.txt", "r"))).decode("base64"))
normalized_distances = []
for KEYSIZE in range(2, 40):
b1 = b[:KEYSIZE]
b2 = b[KEYSIZE:KEYSIZE*2]
b3 = b[KEYSIZE*2:KEYSIZE*3]
b4 = b[KEYSIZE*3:KEYSIZE*4]
normalized_distance = float(