-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a9abacc
Showing
33 changed files
with
990 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
HUIfTQsPAh9PE048GmllH0kcDk4TAQsHThsBFkU2AB4BSWQgVB0dQzNTTmVS | ||
BgBHVBwNRU0HBAxTEjwMHghJGgkRTxRMIRpHKwAFHUdZEQQJAGQmB1MANxYG | ||
DBoXQR0BUlQwXwAgEwoFR08SSAhFTmU+Fgk4RQYFCBpGB08fWXh+amI2DB0P | ||
QQ1IBlUaGwAdQnQEHgFJGgkRAlJ6f0kASDoAGhNJGk9FSA8dDVMEOgFSGQEL | ||
QRMGAEwxX1NiFQYHCQdUCxdBFBZJeTM1CxsBBQ9GB08dTnhOSCdSBAcMRVhI | ||
CEEATyBUCHQLHRlJAgAOFlwAUjBpZR9JAgJUAAELB04CEFMBJhAVTQIHAh9P | ||
G054MGk2UgoBCVQGBwlTTgIQUwg7EAYFSQ8PEE87ADpfRyscSWQzT1QCEFMa | ||
TwUWEXQMBk0PAg4DQ1JMPU4ALwtJDQhOFw0VVB1PDhxFXigLTRkBEgcKVVN4 | ||
Tk9iBgELR1MdDAAAFwoFHww6Ql5NLgFBIg4cSTRWQWI1Bk9HKn47CE8BGwFT | ||
QjcEBx4MThUcDgYHKxpUKhdJGQZZVCFFVwcDBVMHMUV4LAcKQR0JUlk3TwAm | ||
HQdJEwATARNFTg5JFwQ5C15NHQYEGk94dzBDADsdHE4UVBUaDE5JTwgHRTkA | ||
Umc6AUETCgYAN1xGYlUKDxJTEUgsAA0ABwcXOwlSGQELQQcbE0c9GioWGgwc | ||
AgcHSAtPTgsAABY9C1VNCAINGxgXRHgwaWUfS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IcAAw3SVIaAQcVEU8QTyEaYy0fDE4ITlhI | ||
Jk8DCkkcC3hFMQIEC0EbAVIqCFZBO1IdBgZUVA4QTgUWSR4QJwwRTWM= |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import base64 | ||
|
||
def b64_to_bytes(b64_str): | ||
ascii_bytes = b64_str.encode("ascii") | ||
return base64.b64decode(ascii_bytes) | ||
|
||
|
||
def bytes_to_b64(bytearr): | ||
return base64.b64encode(bytearr).decode("ascii") | ||
|
||
|
||
if __name__ == "__main__": | ||
b = b64_to_bytes("BAAA") | ||
print(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
|
||
# Convert hexadecimal string to base 64 string | ||
def hex_to_b64(hexstr): | ||
hextable = "0123456789abcdef" | ||
|
||
pad = len(hexstr) % 3 | ||
if pad == 1: | ||
hexstr = "00" + hexstr | ||
elif pad == 2: | ||
hexstr = "0" + hexstr | ||
|
||
|
||
b64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" | ||
b64str = "" | ||
|
||
for i in range(0, len(hexstr), 3): | ||
n = 0 | ||
mul = 1 | ||
for j in range(2, -1, -1): | ||
single_hex = hextable.index(hexstr[i + j]) | ||
n += single_hex*mul | ||
mul = mul << 4 | ||
# now that we have 3 hex characters together, that is 2 base64 characters | ||
char2 = b64_table[n % 64] | ||
n = n // 64 | ||
char1 = b64_table[n % 64] | ||
b64str += char1 + char2 | ||
return b64str | ||
|
||
|
||
def test_hex_to_b64(): | ||
hex = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d" | ||
ans = "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t" | ||
val = hex_to_b64(hex) | ||
assert val == ans, f"{val=}" | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
n1 = 0x1c0111001f010100061a024b53535009181c | ||
n2 = 0x686974207468652062756c6c277320657965 | ||
|
||
print(hex(n1 ^ n2)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
with open("letter_frequency.csv") as f: | ||
freqs = f.readlines() | ||
|
||
english_freqs = {line.split(",")[0]:float(line.strip().split(",")[1]) for line in freqs[1:]} | ||
|
||
cipher = 0x1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736 | ||
|
||
|
||
def to_ascii(hex_num): | ||
ascii = "" | ||
while hex_num > 0: | ||
|
||
ascii += chr(hex_num % 2**8) | ||
|
||
hex_num = hex_num >> 8 | ||
return ascii[::-1] | ||
|
||
def list_to_num(lst, bits): | ||
num = 0 | ||
mul = 1 | ||
for item in lst[::-1]: | ||
|
||
num += item*mul | ||
mul = mul << bits | ||
|
||
return num | ||
|
||
def letter_freq_score(text): | ||
score = 0 | ||
text = text.lower() | ||
my_freqs = {} | ||
for character in text: | ||
my_freqs[character] = my_freqs.get(character, 0) + 1 | ||
|
||
for key in my_freqs.keys(): | ||
my_freqs[key] /= len(text) | ||
|
||
for key in my_freqs.keys(): | ||
if key in english_freqs: | ||
score += my_freqs[key]*english_freqs[key] | ||
|
||
return score | ||
|
||
output = [] | ||
|
||
for i in range(26*2 + 6): | ||
letter = ord("A") + i | ||
letters = [letter] * 34 | ||
|
||
key = list_to_num(letters, 8) | ||
|
||
text = to_ascii(cipher^key) | ||
score = letter_freq_score(text) | ||
output.append((chr(letter), text, score)) | ||
|
||
output = sorted(output, key=lambda x: x[2], reverse=True) | ||
for o in output[:5]: | ||
print(o) | ||
|
||
|
||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
|
||
|
||
with open("letter_frequency.csv") as f: | ||
freqs = f.readlines() | ||
|
||
english_freqs = {line.split(",")[0]:float(line.strip().split(",")[1]) for line in freqs[1:]} | ||
|
||
|
||
with open("challenge_4_encrypted.txt") as f: | ||
ciphers = f.read().splitlines() | ||
|
||
def to_ascii(num): | ||
ascii = "" | ||
while num > 0: | ||
|
||
ascii += chr(num % 2**8) | ||
|
||
num = num >> 8 | ||
return ascii[::-1] | ||
|
||
def list_to_num(lst, bits): | ||
num = 0 | ||
mul = 1 | ||
for item in lst[::-1]: | ||
|
||
num += item*mul | ||
mul = mul << bits | ||
|
||
return num | ||
|
||
def letter_freq_score(text): | ||
|
||
|
||
score = 0 | ||
text = text.lower() | ||
my_freqs = {} | ||
for character in text: | ||
my_freqs[character] = my_freqs.get(character, 0) + 1 | ||
|
||
for key in my_freqs.keys(): | ||
my_freqs[key] /= len(text) | ||
|
||
for key in my_freqs.keys(): | ||
if key in english_freqs: | ||
score += my_freqs[key]*english_freqs[key] | ||
|
||
return score | ||
|
||
output = [] | ||
for cipher in ciphers: | ||
n_chars = len(cipher)//2 | ||
n_cipher = int(cipher, 16) | ||
|
||
for i in range(256): | ||
letter = i | ||
letters = [letter] * n_chars | ||
|
||
key = list_to_num(letters, 8) | ||
|
||
text = to_ascii(n_cipher^key) | ||
score = letter_freq_score(text) | ||
output.append((cipher, chr(letter), text, score)) | ||
|
||
output = sorted(output, key=lambda x: x[3], reverse=True) | ||
for o in output[:5]: | ||
print(o) | ||
|
||
# Answer: | ||
# ('7b5a4215415d544115415d5015455447414c155c46155f4058455c5b523f', '5', 'Now that the party is jumping\n', 0.066741) | ||
|
||
|
||
|
||
|
Oops, something went wrong.