Last active
June 27, 2021 14:17
-
-
Save ikr7/87ca6cb0ee148338789c575c53ca27a1 to your computer and use it in GitHub Desktop.
Vigenère cipher analyzer for puzzle 04-03 of Cypher ( https://store.steampowered.com/app/746710/Cypher )
This file contains hidden or 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
import sys | |
key = sys.argv[1] | |
cipher = input() | |
print(f'{key = !r}') | |
print(f'{cipher = !r}') | |
print() | |
A = ord('A') | |
def o(c): | |
return ord(c) - A | |
plain = '' | |
monos = [ '' for _ in key ] | |
for i, c in enumerate(cipher): | |
k = key[i % len(key)] | |
monos[i % len(key)] += c | |
plain += chr((o(c) + o(k) + 1) % 26 + A) | |
for j, (k, mono) in enumerate(zip(key, monos)): | |
print(mono, f'==[{k}]=>', plain[j:-1:len(key)]) | |
freq = sorted([ (l, mono.count(l)) for l in [ chr(i + ord('A')) for i in range(26) ] ], key=lambda o:o[1]) | |
print(*[ f'{l}-{f}' for l, f in reversed(freq) ]) | |
print(*[ f'{chr((o(l) + o(k) + 1) % 26 + A)}-{f}' for l, f in reversed(freq) ]) | |
print() | |
print() | |
print(f'{cipher = !r}') | |
print(f'{ plain = !r}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment