Created
September 28, 2015 04:29
-
-
Save croepha/4d934dbd55598e0cc8de to your computer and use it in GitHub Desktop.
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
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Deciphered text: count the number of times Bob , Jim , and Alice are in the tale . the FLAG is \n", | |
| " Alice Jim Bob and the count of each with no spaces . \n", | |
| "\n", | |
| "Flag: AliceJimBox1255\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "#!/usr/bin/env python3\n", | |
| "\n", | |
| "known_cipher_text = 'www.pwndu.edu/~gwalton/homework/crypto/Assignment.cipher.txt'\n", | |
| "known_plain_text = 'www.pwndu.edu/~gwalton/homework/crypto/Assignment.plain.txt'\n", | |
| "target_cipher_text = 'www.pwndu.edu/~gwalton/homework/crypto/secret.txt'\n", | |
| "\n", | |
| "import collections\n", | |
| "\n", | |
| "def get_blocks(file_name):\n", | |
| " with open(file_name, 'rb') as f:\n", | |
| " return list(iter(lambda:f.read(0x10), b''))\n", | |
| "\n", | |
| "def group_blocks(blocks):\n", | |
| " groups = collections.defaultdict(set)\n", | |
| " for i, _cipher_text in enumerate(blocks):\n", | |
| " groups[_cipher_text].add(i)\n", | |
| " return groups\n", | |
| " \n", | |
| "def invert_dict(d):\n", | |
| " return {tuple(sorted(v)): k for k, v in d.items()}\n", | |
| "\n", | |
| "def build_code_book(cipher_blocks, plain_blocks):\n", | |
| " p_lines = invert_dict(plain_blocks)\n", | |
| " return {invert_dict(cipher_blocks)[k]: p_lines[k] for k in p_lines}\n", | |
| "\n", | |
| "def decipher(cipher_blocks, code_book):\n", | |
| " for block in cipher_blocks:\n", | |
| " if block in code_book:\n", | |
| " yield code_book[block]\n", | |
| "\n", | |
| "def string_from_blocks(blocks):\n", | |
| " return ' '.join(_.decode('ascii').rstrip(' ') for _ in blocks)\n", | |
| " \n", | |
| "print(\"Deciphered text: \" + string_from_blocks(decipher(\n", | |
| " get_blocks(target_cipher_text),\n", | |
| " build_code_book(\n", | |
| " group_blocks(get_blocks(known_cipher_text)),\n", | |
| " group_blocks(get_blocks(known_plain_text)),\n", | |
| " )\n", | |
| ")))\n", | |
| "\n", | |
| "tale = string_from_blocks(get_blocks(known_plain_text)).lower()\n", | |
| "print(\"Flag: AliceJimBox%s%s%s\" % (\n", | |
| " tale.count('alice'),\n", | |
| " tale.count('jim'),\n", | |
| " tale.count('bob')\n", | |
| "))\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.4.3" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment