about summary refs log tree commit diff
path: root/users/wpcarro/scratch/cryptopals/set1/c4.py
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2022-01-04T20·42-0800
committerclbot <clbot@tvl.fyi>2022-01-08T05·51+0000
commit7aaddb3d31dabff196eb9ef27666099c00dd5338 (patch)
treec9594bc2e90cd33af32748dd0f0bbd338609f609 /users/wpcarro/scratch/cryptopals/set1/c4.py
parent7ab4493c756a566339734b478a3e2eb91ad6f608 (diff)
fix(wpcarro/cryptopals): Update cleartext scoring algorithm r/3547
Create a frequency table of alphabetic characters by reading each character in
"Alice in Wonderland"; use this frequency table to score cleartext when decoding
ciphers.

Change-Id: Id322af64d792c15231a1a02794f396c46196c207
Reviewed-on: https://cl.tvl.fyi/c/depot/+/4788
Tested-by: BuildkiteCI
Reviewed-by: wpcarro <wpcarro@gmail.com>
Autosubmit: wpcarro <wpcarro@gmail.com>
Diffstat (limited to 'users/wpcarro/scratch/cryptopals/set1/c4.py')
-rw-r--r--users/wpcarro/scratch/cryptopals/set1/c4.py15
1 files changed, 13 insertions, 2 deletions
diff --git a/users/wpcarro/scratch/cryptopals/set1/c4.py b/users/wpcarro/scratch/cryptopals/set1/c4.py
index 6775d73ec8cd..c546419a3388 100644
--- a/users/wpcarro/scratch/cryptopals/set1/c4.py
+++ b/users/wpcarro/scratch/cryptopals/set1/c4.py
@@ -2,11 +2,22 @@ import c3
 
 content = None
 with open('4.txt', 'r') as f:
-    c3.decode_cipher
     content = f.read().splitlines()
+if not content:
+    raise Error("Need content to proceed")
 
+xs = []
 for line in content:
     try:
-        print(c3.decode_cipher(line))
+        x = c3.decode_cipher(line)
+        if x: xs.append(x)
     except:
         continue
+
+freqs = c3.frequency_table()
+print(max(xs, key=lambda x: c3.score(x, freqs)))
+
+################################################################################
+# Answer
+################################################################################
+"Now that the party is jumping"