Added images for breaking keyword ciphers
[cipher-tools.git] / Untitled.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 2,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "from support.utilities import *\n",
10 "from support.language_models import *\n",
11 "from support.norms import *\n",
12 "from cipher.keyword_cipher import *"
13 ]
14 },
15 {
16 "cell_type": "code",
17 "execution_count": 3,
18 "metadata": {},
19 "outputs": [],
20 "source": [
21 "def keyword_encipher_p(message, keyword, wrap_alphabet=KeywordWrapAlphabet.from_a):\n",
22 " cipher_alphabet = keyword_cipher_alphabet_of(keyword, wrap_alphabet)\n",
23 " cipher_translation = {p: c for p, c in zip(string.ascii_lowercase, cipher_alphabet)}\n",
24 " return cat(keyword_encipher_letter(letter, cipher_translation) for letter in message)"
25 ]
26 },
27 {
28 "cell_type": "code",
29 "execution_count": 8,
30 "metadata": {},
31 "outputs": [],
32 "source": [
33 "def keyword_decipher_p(message, keyword, wrap_alphabet=KeywordWrapAlphabet.from_a):\n",
34 " cipher_alphabet = keyword_cipher_alphabet_of(keyword, wrap_alphabet)\n",
35 " plaintext_translation = {c: p for p, c in zip(string.ascii_lowercase, cipher_alphabet)}\n",
36 " return cat(keyword_encipher_letter(letter, plaintext_translation) for letter in message)"
37 ]
38 },
39 {
40 "cell_type": "code",
41 "execution_count": 4,
42 "metadata": {},
43 "outputs": [],
44 "source": [
45 "def keyword_encipher_letter(letter, cipher_translation):\n",
46 " if letter in cipher_translation:\n",
47 " return cipher_translation[letter]\n",
48 " else:\n",
49 " return letter"
50 ]
51 },
52 {
53 "cell_type": "code",
54 "execution_count": 5,
55 "metadata": {},
56 "outputs": [
57 {
58 "data": {
59 "text/plain": [
60 "'qopq hoppkdo'"
61 ]
62 },
63 "execution_count": 5,
64 "metadata": {},
65 "output_type": "execute_result"
66 }
67 ],
68 "source": [
69 "keyword_encipher('test message', 'keyword')"
70 ]
71 },
72 {
73 "cell_type": "code",
74 "execution_count": 9,
75 "metadata": {},
76 "outputs": [
77 {
78 "data": {
79 "text/plain": [
80 "'qopq hoppkdo'"
81 ]
82 },
83 "execution_count": 9,
84 "metadata": {},
85 "output_type": "execute_result"
86 }
87 ],
88 "source": [
89 "keyword_encipher_p('test message', 'keyword')"
90 ]
91 },
92 {
93 "cell_type": "code",
94 "execution_count": 10,
95 "metadata": {},
96 "outputs": [
97 {
98 "data": {
99 "text/plain": [
100 "'test message'"
101 ]
102 },
103 "execution_count": 10,
104 "metadata": {},
105 "output_type": "execute_result"
106 }
107 ],
108 "source": [
109 "keyword_decipher_p('qopq hoppkdo', 'keyword')"
110 ]
111 },
112 {
113 "cell_type": "code",
114 "execution_count": null,
115 "metadata": {},
116 "outputs": [],
117 "source": []
118 }
119 ],
120 "metadata": {
121 "kernelspec": {
122 "display_name": "Python 3",
123 "language": "python",
124 "name": "python3"
125 },
126 "language_info": {
127 "codemirror_mode": {
128 "name": "ipython",
129 "version": 3
130 },
131 "file_extension": ".py",
132 "mimetype": "text/x-python",
133 "name": "python",
134 "nbconvert_exporter": "python",
135 "pygments_lexer": "ipython3",
136 "version": "3.4.5"
137 }
138 },
139 "nbformat": 4,
140 "nbformat_minor": 2
141 }