Skip to content

Instantly share code, notes, and snippets.

@dishbreak
Last active April 16, 2019 16:19
Show Gist options
  • Save dishbreak/70d1765887fddccd029d83a683f43e6c to your computer and use it in GitHub Desktop.
Save dishbreak/70d1765887fddccd029d83a683f43e6c to your computer and use it in GitHub Desktop.
Solve the Ciphers!

What is this?

This is a program that brute-force solves the ciphers in Chapter 7 of Sherlock Holmes, Consulting Detective. Pass it ciphertext as an argument, and it will print out 26 different "plaintext" sequences, one for each rotation on the cipher wheel.

Obviously, this program can spoil a lot of the fun, so be careful with it!

import sys
OUTER_WHEEL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
INNER_WHEEL = "AOEPCTQIHJGFKBRYLVDZNXUWMS"
PUNCTUATION = [' ', '.']
PUNCTUATION_DECODER = dict(zip(PUNCTUATION, PUNCTUATION))
def rotate_wheel(spaces):
return INNER_WHEEL[-spaces:] + INNER_WHEEL[:-spaces]
def build_map(inner_wheel, outer_wheel):
decoder = dict(zip(inner_wheel, outer_wheel))
decoder.update(PUNCTUATION_DECODER)
return decoder
def decode_text(decoder, cipher):
return "".join([decoder[char] for char in cipher])
def main():
for i in range(0, 25):
decoder = build_map(rotate_wheel(i), OUTER_WHEEL)
print(decode_text(decoder, sys.argv[1]))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment