Skip to content

Instantly share code, notes, and snippets.

@ikr7
Last active June 27, 2021 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikr7/87ca6cb0ee148338789c575c53ca27a1 to your computer and use it in GitHub Desktop.
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 )
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