From 4cf86f2e53881e473d881072e55b21179e4dd593 Mon Sep 17 00:00:00 2001 From: William Carroll Date: Wed, 29 Dec 2021 12:11:42 -0400 Subject: feat(wpcarro/scratch): Upload my solutions to cryptopals More beginner problems/solutions for CTF-style challenges. Change-Id: Ide229e99e3ccc1ede5a5ca1c2ad039498e49ea4c Reviewed-on: https://cl.tvl.fyi/c/depot/+/4740 Reviewed-by: wpcarro Autosubmit: wpcarro Tested-by: BuildkiteCI --- users/wpcarro/scratch/cryptopals/set1/c3.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 users/wpcarro/scratch/cryptopals/set1/c3.py (limited to 'users/wpcarro/scratch/cryptopals/set1/c3.py') diff --git a/users/wpcarro/scratch/cryptopals/set1/c3.py b/users/wpcarro/scratch/cryptopals/set1/c3.py new file mode 100644 index 0000000000..187b58c1c7 --- /dev/null +++ b/users/wpcarro/scratch/cryptopals/set1/c3.py @@ -0,0 +1,28 @@ +from c2 import fixed_xor +from collections import Counter +import string + +alphabet = string.ascii_lowercase + string.ascii_uppercase + +def score(bs): + chars = [b for b in bs if b in alphabet] + return sum(Counter(chars).values()) + +def decode_cipher(x): + x = bytearray.fromhex(x) + num_bytes = len(x) + + mx, result, key = 0, None, None + for c in alphabet: + mask = bytearray(bytes(c, 'ascii') * num_bytes) + y = fixed_xor(x, mask, decode_hex=False, encode_hex=False).decode('ascii') + test = score(y) + if test > mx: + result = y + mx = test + key = mask.decode('ascii') + return result + +run_tests = False +if run_tests: + print(decode_cipher("1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736")) -- cgit 1.4.1