5 <meta name=
"viewport" content=
"width=device-width, initial-scale=1, minimum-scale=1" />
6 <meta name=
"generator" content=
"pdoc 0.9.2" />
7 <title>szyfrow.caesar API documentation
</title>
8 <meta name=
"description" content=
"Enciphering and deciphering using the [Caesar cipher](https://en.wikipedia.org/wiki/Caesar_cipher).
9 Also attempts to break messages that use a Caesar …" />
10 <link rel=
"preload stylesheet" as=
"style" href=
"https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/sanitize.min.css" integrity=
"sha256-PK9q560IAAa6WVRRh76LtCaI8pjTJ2z11v0miyNNjrs=" crossorigin
>
11 <link rel=
"preload stylesheet" as=
"style" href=
"https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/typography.min.css" integrity=
"sha256-7l/o7C8jubJiy74VsKTidCy1yBkRtiUGbVkYBylBqUg=" crossorigin
>
12 <link rel=
"stylesheet preload" as=
"style" href=
"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/github.min.css" crossorigin
>
13 <style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:
1.5em}#content{padding:
20px}#sidebar{padding:
30px;overflow:hidden}#sidebar
> *:last-child{margin-bottom:
2cm}.http-server-breadcrumbs{font-size:
130%;margin:
0 0 15px
0}#footer{font-size:
.75em;padding:
5px
30px;border-top:
1px solid #ddd;text-align:right}#footer p{margin:
0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:
30px}h1,h2,h3,h4,h5{font-weight:
300}h1{font-size:
2.5em;line-height:
1.1em}h2{font-size:
1.75em;margin:
1em
0 .50em
0}h3{font-size:
1.4em;margin:
25px
0 10px
0}h4{margin:
0;font-size:
105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:
.2em
0}a{color:#
058;text-decoration:none;transition:color
.3s ease-in-out}a:hover{color:#e82}.title code{font-weight:bold}h2[id^=
"header-"]{margin-top:
2em}.ident{color:#
900}pre code{background:#f8f8f8;font-size:
.8em;line-height:
1.4em}code{background:#f2f2f1;padding:
1px
4px;overflow-wrap:break-word}h1 code{background:transparent}pre{background:#f8f8f8;border:
0;border-top:
1px solid #ccc;border-bottom:
1px solid #ccc;margin:
1em
0;padding:
1ex}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:
10%}#http-server-module-list p{margin-top:
0}.toc ul,#index{list-style-type:none;margin:
0;padding:
0}#index code{background:transparent}#index h3{border-bottom:
1px solid #ddd}#index ul{padding:
0}#index h4{margin-top:
.6em;font-weight:bold}@media (min-width:
200ex){#index .two-column{column-count:
2}}@media (min-width:
300ex){#index .two-column{column-count:
3}}dl{margin-bottom:
2em}dl dl:last-child{margin-bottom:
4em}dd{margin:
0 0 1em
3em}#header-classes + dl
> dd{margin-bottom:
3em}dd dd{margin-left:
2em}dd p{margin:
10px
0}.name{background:#eee;font-weight:bold;font-size:
.85em;padding:
5px
10px;display:inline-block;min-width:
40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name
> span:first-child{white-space:nowrap}.name.class
> span:nth-child(
2){margin-left:
.4em}.inherited{color:#
999;border-left:
5px solid #eee;padding-left:
1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:
400;font-size:
1.25em}.desc h3{font-size:
1em}.desc dt code{background:inherit}.source summary,.git-link-div{color:#
666;text-align:right;font-weight:
400;font-size:
.8em;text-transform:uppercase}.source summary
> *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:
1em}.source pre{max-height:
500px;overflow:auto;margin:
0}.source pre code{font-size:
12px;overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\
2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:
1em}img{max-width:
100%}td{padding:
0 .5em}.admonition{padding:
.1em
.5em;margin-bottom:
1em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}
</style>
14 <style media=
"screen and (min-width: 700px)">@media screen and (min-width:
700px){#sidebar{width:
30%;height:
100vh;overflow:auto;position:sticky;top:
0}#content{width:
70%;max-width:
100ch;padding:
3em
4em;border-left:
1px solid #ddd}pre code{font-size:
1em}.item .name{font-size:
1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul{padding-left:
1.5em}.toc
> ul
> li{margin-top:
.5em}}
</style>
15 <style media=
"print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#
000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:
" (" attr(href)
")";font-size:
90%}a[href][title]:after{content:none}abbr[title]:after{content:
" (" attr(title)
")"}.ir a:after,a[href^=
"javascript:"]:after,a[href^=
"#"]:after{content:
""}pre,blockquote{border:
1px solid #
999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:
100% !important}@page{margin:
0.5cm}p,h2,h3{orphans:
3;widows:
3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}
</style>
16 <script defer
src=
"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js" integrity=
"sha256-Uv3H6lx7dJmRfRvH8TH6kJD1TSK1aFcwgx+mdg3epi8=" crossorigin
></script>
17 <script>window.addEventListener('DOMContentLoaded', () =
> hljs.initHighlighting())
</script>
21 <article id=
"content">
23 <h1 class=
"title">Module
<code>szyfrow.caesar
</code></h1>
25 <section id=
"section-intro">
26 <p>Enciphering and deciphering using the
<a href=
"https://en.wikipedia.org/wiki/Caesar_cipher">Caesar cipher
</a>.
27 Also attempts to break messages that use a Caesar cipher.
</p>
28 <p>The Caesar cipher operates one letter at a time. It converts each letter to a
29 number, then enciphers that number by adding the key. The result is taken mod
30 26 and converted back into a letter.
</p>
31 <details class=
"source">
33 <span>Expand source code
</span>
35 <pre><code class=
"python">"""Enciphering and deciphering using the [Caesar cipher](https://en.wikipedia.org/wiki/Caesar_cipher).
36 Also attempts to break messages that use a Caesar cipher.
38 The Caesar cipher operates one letter at a time. It converts each letter to a
39 number, then enciphers that number by adding the key. The result is taken mod
40 26 and converted back into a letter.
44 from szyfrow.support.utilities import *
45 from szyfrow.support.language_models import *
47 def caesar_encipher_letter(accented_letter, shift):
48 """Encipher a letter, given a shift amount.
50 Accented version of latin letters (such as é and ö) are converted to their
51 non-accented versions before encryption.
53 >>> caesar_encipher_letter(
'a
',
1)
55 >>> caesar_encipher_letter(
'a
',
2)
57 >>> caesar_encipher_letter(
'b
',
2)
59 >>> caesar_encipher_letter(
'x
',
2)
61 >>> caesar_encipher_letter(
'y
',
2)
63 >>> caesar_encipher_letter(
'z
',
2)
65 >>> caesar_encipher_letter(
'z
', -
1)
67 >>> caesar_encipher_letter(
'a
', -
1)
69 >>> caesar_encipher_letter(
'A
',
1)
71 >>> caesar_encipher_letter(
'é
',
1)
74 # letter = unaccent(accented_letter)
75 # if letter in string.ascii_letters:
76 # if letter in string.ascii_uppercase:
77 # alphabet_start = ord(
'A
')
79 # alphabet_start = ord(
'a
')
80 # return chr(((ord(letter) - alphabet_start + shift) %
26) +
85 letter = unaccent(accented_letter)
86 if letter in string.ascii_letters:
87 cipherletter = unpos(pos(letter) + shift)
88 if letter in string.ascii_uppercase:
89 return cipherletter.upper()
95 def caesar_decipher_letter(letter, shift):
96 """Decipher a letter, given a shift amount
98 >>> caesar_decipher_letter(
'b
',
1)
100 >>> caesar_decipher_letter(
'b
',
2)
103 return caesar_encipher_letter(letter, -shift)
105 def caesar_encipher(message, shift):
106 """Encipher a message with the Caesar cipher of given shift
108 >>> caesar_encipher(
'abc
',
1)
110 >>> caesar_encipher(
'abc
',
2)
112 >>> caesar_encipher(
'abcxyz
',
2)
114 >>> caesar_encipher(
'ab cx yz
',
2)
116 >>> caesar_encipher(
'Héllo World!
',
2)
117 'Jgnnq Yqtnf!
'
119 enciphered = [caesar_encipher_letter(l, shift) for l in message]
120 return cat(enciphered)
122 def caesar_decipher(message, shift):
123 """Decipher a message with the Caesar cipher of given shift
125 >>> caesar_decipher(
'bcd
',
1)
127 >>> caesar_decipher(
'cde
',
2)
129 >>> caesar_decipher(
'cd ez ab
',
2)
131 >>> caesar_decipher(
'Jgnnq Yqtnf!
',
2)
132 'Hello World!
'
134 return caesar_encipher(message, -shift)
137 def caesar_break(message, fitness=Pletters):
138 """Breaks a Caesar cipher using frequency analysis
140 It tries all possible keys, scores the fitness of the text decipherd with
141 each key, and returns the key that produces the most fit deciphered text.
143 >>> caesar_break(
'ibxcsyorsaqcheyklxivoexlevmrimwxsfiqevvmihrsasrxliwyrh
' \
144 'ecjsppsamrkwleppfmergefifvmhixscsymjcsyqeoixlm
') # doctest: +ELLIPSIS
145 (
4, -
130.849989015...)
146 >>> caesar_break(
'wxwmaxdgheetgwuxztgptedbgznitgwwhpguxyhkxbmhvvtlbhgtee
' \
147 'raxlmhiixweblmxgxwmhmaxybkbgztgwztsxwbgmxgmert
') # doctest: +ELLIPSIS
148 (
19, -
128.82410410...)
149 >>> caesar_break(
'yltbbqnqnzvguvaxurorgenafsbezqvagbnornfgsbevpnaabjurer
' \
150 'svaquvzyvxrnznazlybequrvfohgriraabjtbaruraprur
') # doctest: +ELLIPSIS
151 (
13, -
126.25403935...)
153 sanitised_message = sanitise(message)
155 best_fit = float(
'-inf
')
156 for shift in range(
26):
157 plaintext = caesar_decipher(sanitised_message, shift)
158 fit = fitness(plaintext)
160 if fit
> best_fit:
164 return best_shift, best_fit
</code></pre>
172 <h2 class=
"section-title" id=
"header-functions">Functions
</h2>
174 <dt id=
"szyfrow.caesar.caesar_break"><code class=
"name flex">
175 <span>def
<span class=
"ident">caesar_break
</span></span>(
<span>message, fitness=
<function Pletters
>)
</span>
178 <div class=
"desc"><p>Breaks a Caesar cipher using frequency analysis
</p>
179 <p>It tries all possible keys, scores the fitness of the text decipherd with
180 each key, and returns the key that produces the most fit deciphered text.
</p>
181 <pre><code class=
"language-python-repl">>>> caesar_break('ibxcsyorsaqcheyklxivoexlevmrimwxsfiqevvmihrsasrxliwyrh' 'ecjsppsamrkwleppfmergefifvmhixscsymjcsyqeoixlm') # doctest: +ELLIPSIS
182 (
4, -
130.849989015...)
183 >>> caesar_break('wxwmaxdgheetgwuxztgptedbgznitgwwhpguxyhkxbmhvvtlbhgtee' 'raxlmhiixweblmxgxwmhmaxybkbgztgwztsxwbgmxgmert') # doctest: +ELLIPSIS
184 (
19, -
128.82410410...)
185 >>> caesar_break('yltbbqnqnzvguvaxurorgenafsbezqvagbnornfgsbevpnaabjurer' 'svaquvzyvxrnznazlybequrvfohgriraabjtbaruraprur') # doctest: +ELLIPSIS
186 (
13, -
126.25403935...)
188 <details class=
"source">
190 <span>Expand source code
</span>
192 <pre><code class=
"python">def caesar_break(message, fitness=Pletters):
193 """Breaks a Caesar cipher using frequency analysis
195 It tries all possible keys, scores the fitness of the text decipherd with
196 each key, and returns the key that produces the most fit deciphered text.
198 >>> caesar_break(
'ibxcsyorsaqcheyklxivoexlevmrimwxsfiqevvmihrsasrxliwyrh
' \
199 'ecjsppsamrkwleppfmergefifvmhixscsymjcsyqeoixlm
') # doctest: +ELLIPSIS
200 (
4, -
130.849989015...)
201 >>> caesar_break(
'wxwmaxdgheetgwuxztgptedbgznitgwwhpguxyhkxbmhvvtlbhgtee
' \
202 'raxlmhiixweblmxgxwmhmaxybkbgztgwztsxwbgmxgmert
') # doctest: +ELLIPSIS
203 (
19, -
128.82410410...)
204 >>> caesar_break(
'yltbbqnqnzvguvaxurorgenafsbezqvagbnornfgsbevpnaabjurer
' \
205 'svaquvzyvxrnznazlybequrvfohgriraabjtbaruraprur
') # doctest: +ELLIPSIS
206 (
13, -
126.25403935...)
208 sanitised_message = sanitise(message)
210 best_fit = float(
'-inf
')
211 for shift in range(
26):
212 plaintext = caesar_decipher(sanitised_message, shift)
213 fit = fitness(plaintext)
215 if fit
> best_fit:
219 return best_shift, best_fit
</code></pre>
222 <dt id=
"szyfrow.caesar.caesar_decipher"><code class=
"name flex">
223 <span>def
<span class=
"ident">caesar_decipher
</span></span>(
<span>message, shift)
</span>
226 <div class=
"desc"><p>Decipher a message with the Caesar cipher of given shift
</p>
227 <pre><code class=
"language-python-repl">>>> caesar_decipher('bcd',
1)
229 >>> caesar_decipher('cde',
2)
231 >>> caesar_decipher('cd ez ab',
2)
233 >>> caesar_decipher('Jgnnq Yqtnf!',
2)
236 <details class=
"source">
238 <span>Expand source code
</span>
240 <pre><code class=
"python">def caesar_decipher(message, shift):
241 """Decipher a message with the Caesar cipher of given shift
243 >>> caesar_decipher(
'bcd
',
1)
245 >>> caesar_decipher(
'cde
',
2)
247 >>> caesar_decipher(
'cd ez ab
',
2)
249 >>> caesar_decipher(
'Jgnnq Yqtnf!
',
2)
250 'Hello World!
'
252 return caesar_encipher(message, -shift)
</code></pre>
255 <dt id=
"szyfrow.caesar.caesar_decipher_letter"><code class=
"name flex">
256 <span>def
<span class=
"ident">caesar_decipher_letter
</span></span>(
<span>letter, shift)
</span>
259 <div class=
"desc"><p>Decipher a letter, given a shift amount
</p>
260 <pre><code class=
"language-python-repl">>>> caesar_decipher_letter('b',
1)
262 >>> caesar_decipher_letter('b',
2)
265 <details class=
"source">
267 <span>Expand source code
</span>
269 <pre><code class=
"python">def caesar_decipher_letter(letter, shift):
270 """Decipher a letter, given a shift amount
272 >>> caesar_decipher_letter(
'b
',
1)
274 >>> caesar_decipher_letter(
'b
',
2)
277 return caesar_encipher_letter(letter, -shift)
</code></pre>
280 <dt id=
"szyfrow.caesar.caesar_encipher"><code class=
"name flex">
281 <span>def
<span class=
"ident">caesar_encipher
</span></span>(
<span>message, shift)
</span>
284 <div class=
"desc"><p>Encipher a message with the Caesar cipher of given shift
</p>
285 <pre><code class=
"language-python-repl">>>> caesar_encipher('abc',
1)
287 >>> caesar_encipher('abc',
2)
289 >>> caesar_encipher('abcxyz',
2)
291 >>> caesar_encipher('ab cx yz',
2)
293 >>> caesar_encipher('Héllo World!',
2)
296 <details class=
"source">
298 <span>Expand source code
</span>
300 <pre><code class=
"python">def caesar_encipher(message, shift):
301 """Encipher a message with the Caesar cipher of given shift
303 >>> caesar_encipher(
'abc
',
1)
305 >>> caesar_encipher(
'abc
',
2)
307 >>> caesar_encipher(
'abcxyz
',
2)
309 >>> caesar_encipher(
'ab cx yz
',
2)
311 >>> caesar_encipher(
'Héllo World!
',
2)
312 'Jgnnq Yqtnf!
'
314 enciphered = [caesar_encipher_letter(l, shift) for l in message]
315 return cat(enciphered)
</code></pre>
318 <dt id=
"szyfrow.caesar.caesar_encipher_letter"><code class=
"name flex">
319 <span>def
<span class=
"ident">caesar_encipher_letter
</span></span>(
<span>accented_letter, shift)
</span>
322 <div class=
"desc"><p>Encipher a letter, given a shift amount.
</p>
323 <p>Accented version of latin letters (such as é and ö) are converted to their
324 non-accented versions before encryption.
</p>
325 <pre><code class=
"language-python-repl">>>> caesar_encipher_letter('a',
1)
327 >>> caesar_encipher_letter('a',
2)
329 >>> caesar_encipher_letter('b',
2)
331 >>> caesar_encipher_letter('x',
2)
333 >>> caesar_encipher_letter('y',
2)
335 >>> caesar_encipher_letter('z',
2)
337 >>> caesar_encipher_letter('z', -
1)
339 >>> caesar_encipher_letter('a', -
1)
341 >>> caesar_encipher_letter('A',
1)
343 >>> caesar_encipher_letter('é',
1)
346 <details class=
"source">
348 <span>Expand source code
</span>
350 <pre><code class=
"python">def caesar_encipher_letter(accented_letter, shift):
351 """Encipher a letter, given a shift amount.
353 Accented version of latin letters (such as é and ö) are converted to their
354 non-accented versions before encryption.
356 >>> caesar_encipher_letter(
'a
',
1)
358 >>> caesar_encipher_letter(
'a
',
2)
360 >>> caesar_encipher_letter(
'b
',
2)
362 >>> caesar_encipher_letter(
'x
',
2)
364 >>> caesar_encipher_letter(
'y
',
2)
366 >>> caesar_encipher_letter(
'z
',
2)
368 >>> caesar_encipher_letter(
'z
', -
1)
370 >>> caesar_encipher_letter(
'a
', -
1)
372 >>> caesar_encipher_letter(
'A
',
1)
374 >>> caesar_encipher_letter(
'é
',
1)
377 # letter = unaccent(accented_letter)
378 # if letter in string.ascii_letters:
379 # if letter in string.ascii_uppercase:
380 # alphabet_start = ord(
'A
')
382 # alphabet_start = ord(
'a
')
383 # return chr(((ord(letter) - alphabet_start + shift) %
26) +
388 letter = unaccent(accented_letter)
389 if letter in string.ascii_letters:
390 cipherletter = unpos(pos(letter) + shift)
391 if letter in string.ascii_uppercase:
392 return cipherletter.upper()
396 return letter
</code></pre>
399 <dt id=
"szyfrow.caesar.cat"><code class=
"name flex">
400 <span>def
<span class=
"ident">cat
</span></span>(
<span>iterable, /)
</span>
403 <div class=
"desc"><p>Concatenate any number of strings.
</p>
404 <p>The string whose method is called is inserted in between each given string.
405 The result is returned as a new string.
</p>
406 <p>Example: '.'.join(['ab', 'pq', 'rs']) -
> 'ab.pq.rs'
</p></div>
408 <dt id=
"szyfrow.caesar.lcat"><code class=
"name flex">
409 <span>def
<span class=
"ident">lcat
</span></span>(
<span>iterable, /)
</span>
412 <div class=
"desc"><p>Concatenate any number of strings.
</p>
413 <p>The string whose method is called is inserted in between each given string.
414 The result is returned as a new string.
</p>
415 <p>Example: '.'.join(['ab', 'pq', 'rs']) -
> 'ab.pq.rs'
</p></div>
417 <dt id=
"szyfrow.caesar.wcat"><code class=
"name flex">
418 <span>def
<span class=
"ident">wcat
</span></span>(
<span>iterable, /)
</span>
421 <div class=
"desc"><p>Concatenate any number of strings.
</p>
422 <p>The string whose method is called is inserted in between each given string.
423 The result is returned as a new string.
</p>
424 <p>Example: '.'.join(['ab', 'pq', 'rs']) -
> 'ab.pq.rs'
</p></div>
437 <li><h3>Super-module
</h3>
439 <li><code><a title=
"szyfrow" href=
"index.html">szyfrow
</a></code></li>
442 <li><h3><a href=
"#header-functions">Functions
</a></h3>
444 <li><code><a title=
"szyfrow.caesar.caesar_break" href=
"#szyfrow.caesar.caesar_break">caesar_break
</a></code></li>
445 <li><code><a title=
"szyfrow.caesar.caesar_decipher" href=
"#szyfrow.caesar.caesar_decipher">caesar_decipher
</a></code></li>
446 <li><code><a title=
"szyfrow.caesar.caesar_decipher_letter" href=
"#szyfrow.caesar.caesar_decipher_letter">caesar_decipher_letter
</a></code></li>
447 <li><code><a title=
"szyfrow.caesar.caesar_encipher" href=
"#szyfrow.caesar.caesar_encipher">caesar_encipher
</a></code></li>
448 <li><code><a title=
"szyfrow.caesar.caesar_encipher_letter" href=
"#szyfrow.caesar.caesar_encipher_letter">caesar_encipher_letter
</a></code></li>
449 <li><code><a title=
"szyfrow.caesar.cat" href=
"#szyfrow.caesar.cat">cat
</a></code></li>
450 <li><code><a title=
"szyfrow.caesar.lcat" href=
"#szyfrow.caesar.lcat">lcat
</a></code></li>
451 <li><code><a title=
"szyfrow.caesar.wcat" href=
"#szyfrow.caesar.wcat">wcat
</a></code></li>
458 <p>Generated by
<a href=
"https://pdoc3.github.io/pdoc"><cite>pdoc
</cite> 0.9.2</a>.
</p>