Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f45e392

Browse files
authoredDec 30, 2024
Fix sphinx/build_docs warnings for ciphers (TheAlgorithms#12485)
* Fix sphinx/build_docs warnings for ciphers * Fix
1 parent 94b3777 commit f45e392

7 files changed

+170
-125
lines changed
 

‎ciphers/autokey.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""
22
https://en.wikipedia.org/wiki/Autokey_cipher
3+
34
An autokey cipher (also known as the autoclave cipher) is a cipher that
45
incorporates the message (the plaintext) into the key.
56
The key is generated from the message in some automated fashion,
@@ -10,8 +11,9 @@
1011

1112
def encrypt(plaintext: str, key: str) -> str:
1213
"""
13-
Encrypt a given plaintext (string) and key (string), returning the
14+
Encrypt a given `plaintext` (string) and `key` (string), returning the
1415
encrypted ciphertext.
16+
1517
>>> encrypt("hello world", "coffee")
1618
'jsqqs avvwo'
1719
>>> encrypt("coffee is good as python", "TheAlgorithms")
@@ -74,8 +76,9 @@ def encrypt(plaintext: str, key: str) -> str:
7476

7577
def decrypt(ciphertext: str, key: str) -> str:
7678
"""
77-
Decrypt a given ciphertext (string) and key (string), returning the decrypted
79+
Decrypt a given `ciphertext` (string) and `key` (string), returning the decrypted
7880
ciphertext.
81+
7982
>>> decrypt("jsqqs avvwo", "coffee")
8083
'hello world'
8184
>>> decrypt("vvjfpk wj ohvp su ddylsv", "TheAlgorithms")

‎ciphers/caesar_cipher.py

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,58 @@ def encrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
77
"""
88
encrypt
99
=======
10+
1011
Encodes a given string with the caesar cipher and returns the encoded
1112
message
1213
1314
Parameters:
1415
-----------
15-
* input_string: the plain-text that needs to be encoded
16-
* key: the number of letters to shift the message by
16+
17+
* `input_string`: the plain-text that needs to be encoded
18+
* `key`: the number of letters to shift the message by
1719
1820
Optional:
19-
* alphabet (None): the alphabet used to encode the cipher, if not
21+
22+
* `alphabet` (``None``): the alphabet used to encode the cipher, if not
2023
specified, the standard english alphabet with upper and lowercase
2124
letters is used
2225
2326
Returns:
27+
2428
* A string containing the encoded cipher-text
2529
2630
More on the caesar cipher
2731
=========================
32+
2833
The caesar cipher is named after Julius Caesar who used it when sending
2934
secret military messages to his troops. This is a simple substitution cipher
3035
where every character in the plain-text is shifted by a certain number known
3136
as the "key" or "shift".
3237
3338
Example:
3439
Say we have the following message:
35-
"Hello, captain"
40+
``Hello, captain``
3641
3742
And our alphabet is made up of lower and uppercase letters:
38-
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
43+
``abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ``
3944
40-
And our shift is "2"
45+
And our shift is ``2``
4146
42-
We can then encode the message, one letter at a time. "H" would become "J",
43-
since "J" is two letters away, and so on. If the shift is ever two large, or
47+
We can then encode the message, one letter at a time. ``H`` would become ``J``,
48+
since ``J`` is two letters away, and so on. If the shift is ever two large, or
4449
our letter is at the end of the alphabet, we just start at the beginning
45-
("Z" would shift to "a" then "b" and so on).
50+
(``Z`` would shift to ``a`` then ``b`` and so on).
4651
47-
Our final message would be "Jgnnq, ecrvckp"
52+
Our final message would be ``Jgnnq, ecrvckp``
4853
4954
Further reading
5055
===============
56+
5157
* https://en.m.wikipedia.org/wiki/Caesar_cipher
5258
5359
Doctests
5460
========
61+
5562
>>> encrypt('The quick brown fox jumps over the lazy dog', 8)
5663
'bpm yCqks jzwEv nwF rCuxA wDmz Bpm tiHG lwo'
5764
@@ -85,23 +92,28 @@ def decrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
8592
"""
8693
decrypt
8794
=======
95+
8896
Decodes a given string of cipher-text and returns the decoded plain-text
8997
9098
Parameters:
9199
-----------
92-
* input_string: the cipher-text that needs to be decoded
93-
* key: the number of letters to shift the message backwards by to decode
100+
101+
* `input_string`: the cipher-text that needs to be decoded
102+
* `key`: the number of letters to shift the message backwards by to decode
94103
95104
Optional:
96-
* alphabet (None): the alphabet used to decode the cipher, if not
105+
106+
* `alphabet` (``None``): the alphabet used to decode the cipher, if not
97107
specified, the standard english alphabet with upper and lowercase
98108
letters is used
99109
100110
Returns:
111+
101112
* A string containing the decoded plain-text
102113
103114
More on the caesar cipher
104115
=========================
116+
105117
The caesar cipher is named after Julius Caesar who used it when sending
106118
secret military messages to his troops. This is a simple substitution cipher
107119
where very character in the plain-text is shifted by a certain number known
@@ -110,27 +122,29 @@ def decrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
110122
111123
Example:
112124
Say we have the following cipher-text:
113-
"Jgnnq, ecrvckp"
125+
``Jgnnq, ecrvckp``
114126
115127
And our alphabet is made up of lower and uppercase letters:
116-
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
128+
``abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ``
117129
118-
And our shift is "2"
130+
And our shift is ``2``
119131
120132
To decode the message, we would do the same thing as encoding, but in
121-
reverse. The first letter, "J" would become "H" (remember: we are decoding)
122-
because "H" is two letters in reverse (to the left) of "J". We would
123-
continue doing this. A letter like "a" would shift back to the end of
124-
the alphabet, and would become "Z" or "Y" and so on.
133+
reverse. The first letter, ``J`` would become ``H`` (remember: we are decoding)
134+
because ``H`` is two letters in reverse (to the left) of ``J``. We would
135+
continue doing this. A letter like ``a`` would shift back to the end of
136+
the alphabet, and would become ``Z`` or ``Y`` and so on.
125137
126-
Our final message would be "Hello, captain"
138+
Our final message would be ``Hello, captain``
127139
128140
Further reading
129141
===============
142+
130143
* https://en.m.wikipedia.org/wiki/Caesar_cipher
131144
132145
Doctests
133146
========
147+
134148
>>> decrypt('bpm yCqks jzwEv nwF rCuxA wDmz Bpm tiHG lwo', 8)
135149
'The quick brown fox jumps over the lazy dog'
136150
@@ -150,41 +164,44 @@ def brute_force(input_string: str, alphabet: str | None = None) -> dict[int, str
150164
"""
151165
brute_force
152166
===========
167+
153168
Returns all the possible combinations of keys and the decoded strings in the
154169
form of a dictionary
155170
156171
Parameters:
157172
-----------
158-
* input_string: the cipher-text that needs to be used during brute-force
173+
174+
* `input_string`: the cipher-text that needs to be used during brute-force
159175
160176
Optional:
161-
* alphabet: (None): the alphabet used to decode the cipher, if not
177+
178+
* `alphabet` (``None``): the alphabet used to decode the cipher, if not
162179
specified, the standard english alphabet with upper and lowercase
163180
letters is used
164181
165182
More about brute force
166183
======================
184+
167185
Brute force is when a person intercepts a message or password, not knowing
168186
the key and tries every single combination. This is easy with the caesar
169187
cipher since there are only all the letters in the alphabet. The more
170188
complex the cipher, the larger amount of time it will take to do brute force
171189
172190
Ex:
173-
Say we have a 5 letter alphabet (abcde), for simplicity and we intercepted the
174-
following message:
175-
176-
"dbc"
177-
191+
Say we have a ``5`` letter alphabet (``abcde``), for simplicity and we intercepted
192+
the following message: ``dbc``,
178193
we could then just write out every combination:
179-
ecd... and so on, until we reach a combination that makes sense:
180-
"cab"
194+
``ecd``... and so on, until we reach a combination that makes sense:
195+
``cab``
181196
182197
Further reading
183198
===============
199+
184200
* https://en.wikipedia.org/wiki/Brute_force
185201
186202
Doctests
187203
========
204+
188205
>>> brute_force("jFyuMy xIH'N vLONy zILwy Gy!")[20]
189206
"Please don't brute force me!"
190207

‎ciphers/decrypt_caesar_with_chi_squared.py

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,103 +11,106 @@ def decrypt_caesar_with_chi_squared(
1111
"""
1212
Basic Usage
1313
===========
14+
1415
Arguments:
15-
* ciphertext (str): the text to decode (encoded with the caesar cipher)
16+
* `ciphertext` (str): the text to decode (encoded with the caesar cipher)
1617
1718
Optional Arguments:
18-
* cipher_alphabet (list): the alphabet used for the cipher (each letter is
19-
a string separated by commas)
20-
* frequencies_dict (dict): a dictionary of word frequencies where keys are
21-
the letters and values are a percentage representation of the frequency as
22-
a decimal/float
23-
* case_sensitive (bool): a boolean value: True if the case matters during
24-
decryption, False if it doesn't
19+
* `cipher_alphabet` (list): the alphabet used for the cipher (each letter is
20+
a string separated by commas)
21+
* `frequencies_dict` (dict): a dictionary of word frequencies where keys are
22+
the letters and values are a percentage representation of the frequency as
23+
a decimal/float
24+
* `case_sensitive` (bool): a boolean value: ``True`` if the case matters during
25+
decryption, ``False`` if it doesn't
2526
2627
Returns:
27-
* A tuple in the form of:
28-
(
29-
most_likely_cipher,
30-
most_likely_cipher_chi_squared_value,
31-
decoded_most_likely_cipher
32-
)
28+
* A tuple in the form of:
29+
(`most_likely_cipher`, `most_likely_cipher_chi_squared_value`,
30+
`decoded_most_likely_cipher`)
3331
34-
where...
35-
- most_likely_cipher is an integer representing the shift of the smallest
36-
chi-squared statistic (most likely key)
37-
- most_likely_cipher_chi_squared_value is a float representing the
38-
chi-squared statistic of the most likely shift
39-
- decoded_most_likely_cipher is a string with the decoded cipher
40-
(decoded by the most_likely_cipher key)
32+
where...
33+
- `most_likely_cipher` is an integer representing the shift of the smallest
34+
chi-squared statistic (most likely key)
35+
- `most_likely_cipher_chi_squared_value` is a float representing the
36+
chi-squared statistic of the most likely shift
37+
- `decoded_most_likely_cipher` is a string with the decoded cipher
38+
(decoded by the most_likely_cipher key)
4139
4240
4341
The Chi-squared test
4442
====================
4543
4644
The caesar cipher
4745
-----------------
46+
4847
The caesar cipher is a very insecure encryption algorithm, however it has
4948
been used since Julius Caesar. The cipher is a simple substitution cipher
5049
where each character in the plain text is replaced by a character in the
5150
alphabet a certain number of characters after the original character. The
5251
number of characters away is called the shift or key. For example:
5352
54-
Plain text: hello
55-
Key: 1
56-
Cipher text: ifmmp
57-
(each letter in hello has been shifted one to the right in the eng. alphabet)
53+
| Plain text: ``hello``
54+
| Key: ``1``
55+
| Cipher text: ``ifmmp``
56+
| (each letter in ``hello`` has been shifted one to the right in the eng. alphabet)
5857
5958
As you can imagine, this doesn't provide lots of security. In fact
6059
decrypting ciphertext by brute-force is extremely easy even by hand. However
61-
one way to do that is the chi-squared test.
60+
one way to do that is the chi-squared test.
6261
6362
The chi-squared test
64-
-------------------
63+
--------------------
64+
6565
Each letter in the english alphabet has a frequency, or the amount of times
6666
it shows up compared to other letters (usually expressed as a decimal
6767
representing the percentage likelihood). The most common letter in the
68-
english language is "e" with a frequency of 0.11162 or 11.162%. The test is
69-
completed in the following fashion.
68+
english language is ``e`` with a frequency of ``0.11162`` or ``11.162%``.
69+
The test is completed in the following fashion.
7070
7171
1. The ciphertext is decoded in a brute force way (every combination of the
72-
26 possible combinations)
72+
``26`` possible combinations)
7373
2. For every combination, for each letter in the combination, the average
7474
amount of times the letter should appear the message is calculated by
75-
multiplying the total number of characters by the frequency of the letter
75+
multiplying the total number of characters by the frequency of the letter.
76+
77+
| For example:
78+
| In a message of ``100`` characters, ``e`` should appear around ``11.162``
79+
times.
7680
77-
For example:
78-
In a message of 100 characters, e should appear around 11.162 times.
81+
3. Then, to calculate the margin of error (the amount of times the letter
82+
SHOULD appear with the amount of times the letter DOES appear), we use
83+
the chi-squared test. The following formula is used:
7984
80-
3. Then, to calculate the margin of error (the amount of times the letter
81-
SHOULD appear with the amount of times the letter DOES appear), we use
82-
the chi-squared test. The following formula is used:
85+
Let:
86+
- n be the number of times the letter actually appears
87+
- p be the predicted value of the number of times the letter should
88+
appear (see item ``2``)
89+
- let v be the chi-squared test result (referred to here as chi-squared
90+
value/statistic)
8391
84-
Let:
85-
- n be the number of times the letter actually appears
86-
- p be the predicted value of the number of times the letter should
87-
appear (see #2)
88-
- let v be the chi-squared test result (referred to here as chi-squared
89-
value/statistic)
92+
::
9093
91-
(n - p)^2
92-
--------- = v
93-
p
94+
(n - p)^2
95+
--------- = v
96+
p
9497
9598
4. Each chi squared value for each letter is then added up to the total.
9699
The total is the chi-squared statistic for that encryption key.
97100
5. The encryption key with the lowest chi-squared value is the most likely
98101
to be the decoded answer.
99102
100103
Further Reading
101-
================
104+
===============
102105
103-
* http://practicalcryptography.com/cryptanalysis/text-characterisation/chi-squared-
104-
statistic/
106+
* http://practicalcryptography.com/cryptanalysis/text-characterisation/chi-squared-statistic/
105107
* https://en.wikipedia.org/wiki/Letter_frequency
106108
* https://en.wikipedia.org/wiki/Chi-squared_test
107109
* https://en.m.wikipedia.org/wiki/Caesar_cipher
108110
109111
Doctests
110112
========
113+
111114
>>> decrypt_caesar_with_chi_squared(
112115
... 'dof pz aol jhlzhy jpwoly zv wvwbshy? pa pz avv lhzf av jyhjr!'
113116
... ) # doctest: +NORMALIZE_WHITESPACE

‎ciphers/enigma_machine2.py

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
"""
2-
Wikipedia: https://en.wikipedia.org/wiki/Enigma_machine
3-
Video explanation: https://youtu.be/QwQVMqfoB2E
4-
Also check out Numberphile's and Computerphile's videos on this topic
2+
| Wikipedia: https://en.wikipedia.org/wiki/Enigma_machine
3+
| Video explanation: https://youtu.be/QwQVMqfoB2E
4+
| Also check out Numberphile's and Computerphile's videos on this topic
55
6-
This module contains function 'enigma' which emulates
6+
This module contains function ``enigma`` which emulates
77
the famous Enigma machine from WWII.
8+
89
Module includes:
9-
- enigma function
10+
11+
- ``enigma`` function
1012
- showcase of function usage
11-
- 9 randomly generated rotors
13+
- ``9`` randomly generated rotors
1214
- reflector (aka static rotor)
1315
- original alphabet
1416
@@ -73,7 +75,7 @@ def _validator(
7375
rotpos: RotorPositionT, rotsel: RotorSelectionT, pb: str
7476
) -> tuple[RotorPositionT, RotorSelectionT, dict[str, str]]:
7577
"""
76-
Checks if the values can be used for the 'enigma' function
78+
Checks if the values can be used for the ``enigma`` function
7779
7880
>>> _validator((1,1,1), (rotor1, rotor2, rotor3), 'POLAND')
7981
((1, 1, 1), ('EGZWVONAHDCLFQMSIPJBYUKXTR', 'FOBHMDKEXQNRAULPGSJVTYICZW', \
@@ -83,7 +85,7 @@ def _validator(
8385
:param rotpos: rotor_positon
8486
:param rotsel: rotor_selection
8587
:param pb: plugb -> validated and transformed
86-
:return: (rotpos, rotsel, pb)
88+
:return: (`rotpos`, `rotsel`, `pb`)
8789
"""
8890
# Checks if there are 3 unique rotors
8991

@@ -118,9 +120,10 @@ def _plugboard(pbstring: str) -> dict[str, str]:
118120
>>> _plugboard('POLAND')
119121
{'P': 'O', 'O': 'P', 'L': 'A', 'A': 'L', 'N': 'D', 'D': 'N'}
120122
121-
In the code, 'pb' stands for 'plugboard'
123+
In the code, ``pb`` stands for ``plugboard``
122124
123125
Pairs can be separated by spaces
126+
124127
:param pbstring: string containing plugboard setting for the Enigma machine
125128
:return: dictionary containing converted pairs
126129
"""
@@ -168,31 +171,34 @@ def enigma(
168171
plugb: str = "",
169172
) -> str:
170173
"""
171-
The only difference with real-world enigma is that I allowed string input.
174+
The only difference with real-world enigma is that ``I`` allowed string input.
172175
All characters are converted to uppercase. (non-letter symbol are ignored)
173-
How it works:
174-
(for every letter in the message)
176+
177+
| How it works:
178+
| (for every letter in the message)
175179
176180
- Input letter goes into the plugboard.
177-
If it is connected to another one, switch it.
181+
If it is connected to another one, switch it.
182+
183+
- Letter goes through ``3`` rotors.
184+
Each rotor can be represented as ``2`` sets of symbol, where one is shuffled.
185+
Each symbol from the first set has corresponding symbol in
186+
the second set and vice versa.
178187
179-
- Letter goes through 3 rotors.
180-
Each rotor can be represented as 2 sets of symbol, where one is shuffled.
181-
Each symbol from the first set has corresponding symbol in
182-
the second set and vice versa.
188+
example::
183189
184-
example:
185-
| ABCDEFGHIJKLMNOPQRSTUVWXYZ | e.g. F=D and D=F
186-
| VKLEPDBGRNWTFCJOHQAMUZYIXS |
190+
| ABCDEFGHIJKLMNOPQRSTUVWXYZ | e.g. F=D and D=F
191+
| VKLEPDBGRNWTFCJOHQAMUZYIXS |
187192
188193
- Symbol then goes through reflector (static rotor).
189-
There it is switched with paired symbol
190-
The reflector can be represented as2 sets, each with half of the alphanet.
191-
There are usually 10 pairs of letters.
194+
There it is switched with paired symbol.
195+
The reflector can be represented as ``2`` sets, each with half of the alphanet.
196+
There are usually ``10`` pairs of letters.
197+
198+
Example::
192199
193-
Example:
194-
| ABCDEFGHIJKLM | e.g. E is paired to X
195-
| ZYXWVUTSRQPON | so when E goes in X goes out and vice versa
200+
| ABCDEFGHIJKLM | e.g. E is paired to X
201+
| ZYXWVUTSRQPON | so when E goes in X goes out and vice versa
196202
197203
- Letter then goes through the rotors again
198204
@@ -211,9 +217,9 @@ def enigma(
211217
212218
213219
:param text: input message
214-
:param rotor_position: tuple with 3 values in range 1..26
215-
:param rotor_selection: tuple with 3 rotors ()
216-
:param plugb: string containing plugboard configuration (default '')
220+
:param rotor_position: tuple with ``3`` values in range ``1``.. ``26``
221+
:param rotor_selection: tuple with ``3`` rotors
222+
:param plugb: string containing plugboard configuration (default ``''``)
217223
:return: en/decrypted string
218224
"""
219225

‎ciphers/rsa_factorization.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
44
The program can efficiently factor RSA prime number given the private key d and
55
public key e.
6-
Source: on page 3 of https://crypto.stanford.edu/~dabo/papers/RSA-survey.pdf
7-
More readable source: https://www.di-mgt.com.au/rsa_factorize_n.html
6+
7+
| Source: on page ``3`` of https://crypto.stanford.edu/~dabo/papers/RSA-survey.pdf
8+
| More readable source: https://www.di-mgt.com.au/rsa_factorize_n.html
9+
810
large number can take minutes to factor, therefore are not included in doctest.
911
"""
1012

@@ -17,13 +19,14 @@
1719
def rsafactor(d: int, e: int, n: int) -> list[int]:
1820
"""
1921
This function returns the factors of N, where p*q=N
20-
Return: [p, q]
22+
23+
Return: [p, q]
2124
2225
We call N the RSA modulus, e the encryption exponent, and d the decryption exponent.
2326
The pair (N, e) is the public key. As its name suggests, it is public and is used to
24-
encrypt messages.
27+
encrypt messages.
2528
The pair (N, d) is the secret key or private key and is known only to the recipient
26-
of encrypted messages.
29+
of encrypted messages.
2730
2831
>>> rsafactor(3, 16971, 25777)
2932
[149, 173]

‎ciphers/simple_keyword_cypher.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
def remove_duplicates(key: str) -> str:
22
"""
33
Removes duplicate alphabetic characters in a keyword (letter is ignored after its
4-
first appearance).
4+
first appearance).
5+
56
:param key: Keyword to use
67
:return: String with duplicates removed
8+
79
>>> remove_duplicates('Hello World!!')
810
'Helo Wrd'
911
"""
@@ -18,6 +20,7 @@ def remove_duplicates(key: str) -> str:
1820
def create_cipher_map(key: str) -> dict[str, str]:
1921
"""
2022
Returns a cipher map given a keyword.
23+
2124
:param key: keyword to use
2225
:return: dictionary cipher map
2326
"""
@@ -43,9 +46,11 @@ def create_cipher_map(key: str) -> dict[str, str]:
4346
def encipher(message: str, cipher_map: dict[str, str]) -> str:
4447
"""
4548
Enciphers a message given a cipher map.
49+
4650
:param message: Message to encipher
4751
:param cipher_map: Cipher map
4852
:return: enciphered string
53+
4954
>>> encipher('Hello World!!', create_cipher_map('Goodbye!!'))
5055
'CYJJM VMQJB!!'
5156
"""
@@ -55,9 +60,11 @@ def encipher(message: str, cipher_map: dict[str, str]) -> str:
5560
def decipher(message: str, cipher_map: dict[str, str]) -> str:
5661
"""
5762
Deciphers a message given a cipher map
63+
5864
:param message: Message to decipher
5965
:param cipher_map: Dictionary mapping to use
6066
:return: Deciphered string
67+
6168
>>> cipher_map = create_cipher_map('Goodbye!!')
6269
>>> decipher(encipher('Hello World!!', cipher_map), cipher_map)
6370
'HELLO WORLD!!'
@@ -70,6 +77,7 @@ def decipher(message: str, cipher_map: dict[str, str]) -> str:
7077
def main() -> None:
7178
"""
7279
Handles I/O
80+
7381
:return: void
7482
"""
7583
message = input("Enter message to encode or decode: ").strip()

‎ciphers/trifid_cipher.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
def __encrypt_part(message_part: str, character_to_number: dict[str, str]) -> str:
2424
"""
25-
Arrange the triagram value of each letter of 'message_part' vertically and join
25+
Arrange the triagram value of each letter of `message_part` vertically and join
2626
them horizontally.
2727
2828
>>> __encrypt_part('ASK', TEST_CHARACTER_TO_NUMBER)
@@ -65,8 +65,8 @@ def __prepare(
6565
"""
6666
A helper function that generates the triagrams and assigns each letter of the
6767
alphabet to its corresponding triagram and stores this in a dictionary
68-
("character_to_number" and "number_to_character") after confirming if the
69-
alphabet's length is 27.
68+
(`character_to_number` and `number_to_character`) after confirming if the
69+
alphabet's length is ``27``.
7070
7171
>>> test = __prepare('I aM a BOy','abCdeFghijkLmnopqrStuVwxYZ+')
7272
>>> expected = ('IAMABOY','ABCDEFGHIJKLMNOPQRSTUVWXYZ+',
@@ -75,24 +75,28 @@ def __prepare(
7575
True
7676
7777
Testing with incomplete alphabet
78+
7879
>>> __prepare('I aM a BOy','abCdeFghijkLmnopqrStuVw')
7980
Traceback (most recent call last):
8081
...
8182
KeyError: 'Length of alphabet has to be 27.'
8283
8384
Testing with extra long alphabets
85+
8486
>>> __prepare('I aM a BOy','abCdeFghijkLmnopqrStuVwxyzzwwtyyujjgfd')
8587
Traceback (most recent call last):
8688
...
8789
KeyError: 'Length of alphabet has to be 27.'
8890
8991
Testing with punctuations that are not in the given alphabet
92+
9093
>>> __prepare('am i a boy?','abCdeFghijkLmnopqrStuVwxYZ+')
9194
Traceback (most recent call last):
9295
...
9396
ValueError: Each message character has to be included in alphabet!
9497
9598
Testing with numbers
99+
96100
>>> __prepare(500,'abCdeFghijkLmnopqrStuVwxYZ+')
97101
Traceback (most recent call last):
98102
...
@@ -130,9 +134,9 @@ def encrypt_message(
130134
PARAMETERS
131135
----------
132136
133-
* message: The message you want to encrypt.
134-
* alphabet (optional): The characters to be used for the cipher .
135-
* period (optional): The number of characters you want in a group whilst
137+
* `message`: The message you want to encrypt.
138+
* `alphabet` (optional): The characters to be used for the cipher .
139+
* `period` (optional): The number of characters you want in a group whilst
136140
encrypting.
137141
138142
>>> encrypt_message('I am a boy')
@@ -169,20 +173,21 @@ def decrypt_message(
169173
decrypt_message
170174
===============
171175
172-
Decrypts a trifid_cipher encrypted message .
176+
Decrypts a trifid_cipher encrypted message.
173177
174178
PARAMETERS
175179
----------
176180
177-
* message: The message you want to decrypt .
178-
* alphabet (optional): The characters used for the cipher.
179-
* period (optional): The number of characters used in grouping when it
181+
* `message`: The message you want to decrypt.
182+
* `alphabet` (optional): The characters used for the cipher.
183+
* `period` (optional): The number of characters used in grouping when it
180184
was encrypted.
181185
182186
>>> decrypt_message('BCDGBQY')
183187
'IAMABOY'
184188
185189
Decrypting with your own alphabet and period
190+
186191
>>> decrypt_message('FMJFVOISSUFTFPUFEQQC','FELIXMARDSTBCGHJKNOPQUVWYZ+',5)
187192
'AIDETOILECIELTAIDERA'
188193
"""

0 commit comments

Comments
 (0)
Please sign in to comment.