Skip to content

Commit ef75dfb

Browse files
committed
- Fixed docstring spelling errors
- Other small docstring adjustments - Added more decryption tests - Added docstring to test class - Improved existing test method docstrings - Improved "./README.md" - Changes to "./setup.py" - Removed unused import from "./__init__.py" - Small changes to encryption method output text Signed-off-by: schlopp96 <[email protected]>
1 parent 114f884 commit ef75dfb

File tree

5 files changed

+81
-42
lines changed

5 files changed

+81
-42
lines changed

MyCaesarCipher/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
from .main import CaesarCipher

MyCaesarCipher/main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
from random import randint
44
from typing import Optional
55

6+
__version__ = '0.4.0'
7+
68

79
class CaesarCipher:
8-
"""Cryptographic encryption/decryption techniques using the Caesar-Cipher algorithm.
10+
"""Cryptographic encryption/decryption using the Caesar-Cipher algorithm.
911
1012
---
1113
@@ -21,7 +23,7 @@ class CaesarCipher:
2123
def encrypt(text: str,
2224
key: Optional[int] = None,
2325
stdout_output: bool = True) -> str:
24-
"""Encrypt text using the Caesar-Cypher cryptography algorithm.
26+
"""Encrypt text using the Caesar-Cipher cryptography algorithm.
2527
2628
- Generates encrypted form of :param:`text` using the Caesar-Cipher algorithm.
2729
- If no :param:`key` is provided, a random integer value is generated.
@@ -64,7 +66,7 @@ def encrypt(text: str,
6466
else:
6567
output += char
6668

67-
info: str = f'> Original Msg : {text}\n\n> Shift-key : {key}\n\n> Encrypted Result: {output}\n'
69+
info: str = f'> Original Txt : {text}\n\n> Shift-key : {key}\n\n> Encrypted Result: {output}\n'
6870

6971
if stdout_output:
7072
print(info)

MyCaesarCipher/tests/MyCaesarCipher_Test.py

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@
33

44

55
class CaesarCipherTesting:
6+
"""Provides methods for testing the encryption and decryption functionality of the `CaesarCipher` class.
7+
8+
---
9+
10+
Contains the following test methods:
11+
12+
- :func:`test_encrypt(self, text: str, key: int, assertion: str, output_results: bool = True) -> None`
13+
- Test encryption process using given text and shift-key.
14+
- Output of test results to stdout enabled by default.
15+
16+
- :func:`test_decrypt(self, text: str, assertion: str, output_results: bool = True) -> None`
17+
- Test decryption method using given text.
18+
- Output of test results to stdout enabled by default.
19+
"""
620

721
def __init__(self, test_class=CaesarCipher()) -> None:
822
self.test_class: Any = test_class
@@ -15,8 +29,9 @@ def test_encrypt(
1529
output_results: bool = True,
1630
) -> None:
1731
"""
18-
Tests encryption of given text and shift-key and asserts the result
19-
is equal to the output.
32+
Test encryption method using given text and shift-key.
33+
34+
---
2035
2136
:param text: text to encrypt
2237
:type text: :class:`str`
@@ -25,58 +40,75 @@ def test_encrypt(
2540
:param assertion: expected output of the encryption
2641
:type assertion: :class:`str`
2742
:param output_results: toggle result output, defaults to :bool:`True`
28-
:type output_results: :class:`bool` (optional)
43+
:type output_results: :class:`bool`, optional
44+
:return: result of encryption test
45+
:rtype: None
2946
"""
3047

3148
assert self.test_class.encrypt(text, key, output_results) == assertion
3249

3350
def test_decrypt(self,
3451
text: str,
3552
assertion: str,
36-
output_results: bool = False) -> None:
53+
output_results: bool = True) -> None:
3754
"""
38-
Tests decryption of given text and whether the assertion can be found within resulting output.
55+
Test decryption method using given text.
3956
4057
---
4158
42-
:param text: the text to be decrypted
59+
:param text: text to be decrypted
4360
:type text: :class:`str`
4461
:param assertion: expected result to be found within output
4562
:type assertion: :class:`str`
4663
:param output_results: toggle output of results to `stdout`, defaults to `False`
47-
:type output_results: :class:`bool` (optional)
64+
:type output_results: :class:`bool`, optional
65+
:return: result of decryption test
66+
:rtype: None
4867
"""
4968

5069
assert assertion in self.test_class.decrypt(text, output_results)
5170

5271

53-
# Create test class instance.
72+
# Create test class instance
5473
testCC: CaesarCipherTesting = CaesarCipherTesting()
5574

5675

57-
# Test encryption of string containing 5 uppercase characters.
76+
# Test encryption of string containing uppercase characters
5877
def test_encodeA() -> None:
5978
testCC.test_encrypt('TESTING', 5, 'YJXYNSL')
6079

6180

62-
# Test encryption of string containing both lowercase and uppercase characters.
81+
# Test encryption of string containing both lowercase and uppercase characters
6382
def test_encodeB() -> None:
6483
testCC.test_encrypt("TeStInG", 5, 'YjXyNsL')
6584

6685

67-
# Test encryption of string containing digits.
86+
# Test encryption of string containing digits
6887
def test_encodeC() -> None:
6988
testCC.test_encrypt('16', 5, '61')
7089

7190

72-
# Test encryption of string containing lowercase/uppercase characters and digits.
91+
# Test encryption of string containing lowercase/uppercase characters and digits
7392
def test_encodeD() -> None:
7493
testCC.test_encrypt("Password123", 5, 'Ufxxbtwi678', False)
7594

7695

96+
# Test decryption of string containing lowercase characters
7797
def test_decodeA() -> None:
78-
testCC.test_decrypt('1djjktcqttccwprztsd', '0syyzirfiirrlegoihs')
98+
testCC.test_decrypt('djjktcqttccwprztsd', 'syyzirfiirrlegoihs')
7999

80100

101+
# Test decryption of string containing lowercase/uppercase characters and digits
81102
def test_decodeB() -> None:
82-
testCC.test_decrypt('2ekkludruuddxosaute', '1djjktcqttccwnrztsd')
103+
testCC.test_decrypt('2EkkLudRuuddXosaute123', '1DjjKtcQttccWnrztsd012')
104+
105+
106+
# Test decryption of string containing lowercase/uppercase characters and punctuation
107+
def test_decodeC() -> None:
108+
testCC.test_decrypt('Szh\'d te rztyr? Hlye ez slyr zfe zc dzxpestyr?',
109+
'How\'s it going? Want to hang out or something?')
110+
111+
112+
# Test decryption of string containing lowercase/uppercase characters, numbers and punctuation
113+
def test_decodeD() -> None:
114+
testCC.test_decrypt('3120854 - Idbbn Ijidct', '8675309 - Tommy Tutone')

README.md

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,24 @@
1010

1111
- Each letter in the plaintext entry is replaced by a letter found at a certain number of positions down the alphabet.
1212

13-
- This project was created as an exercise while I was taking the ["Cracking Codes with Python"](https://inventwithpython.com/cracking/) course - which I _highly_ recommend for both beginners and experienced python programmers interested in cryptography!
13+
- This project was created as an exercise while I was taking the ["Cracking Codes with Python"](https://inventwithpython.com/cracking/) course - which I _highly_ recommend for both beginners and experienced Python programmers interested in cryptography!
1414

1515
---
1616

1717
## Installation
1818

1919
### Using pip _(Recommended)_
2020

21-
> _Easiest_ method. _**Highly recommended over manual installation.**_
21+
> **_Easiest_ method.**
22+
> _Highly recommended over manual installation._
2223
2324
- Run the following to install _**`MyCaesarCipher`**_ using `pip`:
2425

2526
- ```python
2627
pip install MyCaesarCipher
2728
```
2829

29-
- You should now be able to import/run _**`MyCaesarCipher`**_ within your python environment by entering the following:
30+
- You should now be able to import/run _**`MyCaesarCipher`**_ within your Python environment by entering the following:
3031

3132
- ```python
3233
>>> from MyCaesarCipher import CaesarCipher
@@ -39,14 +40,15 @@
3940

4041
### Manual Installation
4142

42-
> _Not_ recommended. _**Only use this method if you are unable to use `pip`**_.
43+
> **_Not_ recommended.**
44+
> _Only use this method if you are unable to install using `pip`_.
4345

4446
1. Before use, navigate to the intended installation location, and create a new directory.
4547

4648
2. Please only do one of the following:
4749

4850
- **A.** Clone repository with the git client of your preference.
49-
- **B.** Download and extract the source code `zip` archive from the ["[releases"](https://github.com/schlopp96/MyCaesarCipher/releases) page to the newly created directory.
51+
- **B.** Download and extract the source code `zip` archive from the ["releases"](https://github.com/schlopp96/MyCaesarCipher/releases) page to the newly created directory.
5052

5153
3. Install all dependencies for this package within the installation directory using the following command:
5254

@@ -64,29 +66,29 @@
6466

6567
- Within a Python environment or **`.py`** project, simply import the _**`MyCaesarCipher`**_ module to start encryption/decryption of ciphers.
6668

67-
### Message Encryption
69+
### Text Encryption
6870

6971
- For encrypting text, use the **`CaesarCipher.encrypt`** class method:
7072

7173
- ```python
7274
>>> from MyCaesarCipher import CaesarCipher
7375

7476
>>> cipher = CaesarCipher() # Create new class instance.
75-
>>> msg = 'Test Cipher'
76-
>>> cipher.encrypt(text=msg, key=200, stdout_output=True)
77+
>>> txt = 'Test Cipher'
78+
>>> cipher.encrypt(text=txt, key=15, stdout_output=True)
7779

7880
------------------------------------
7981

80-
> Original Msg : Test Cipher
82+
> Original Txt : Test Cipher
8183

82-
> Shift-Key : 200
84+
> Shift-Key : 15
8385

84-
> Encrypted Result: Lwkl Uahzwj
86+
> Encrypted Result: Ithi Rxewtg
8587
```
8688

87-
- Therefore the final encrypted result of "Test Cipher" using a shift key of 200 is:
89+
- Therefore the final encrypted result of "Test Cipher" using a shift key of 15 is:
8890

89-
- "**`LwklfUahzwj`**".
91+
- "**`Ithi Rxewtg`**".
9092

9193
- Note that the `key` parameter is _optional_, and if not provided, a random key between 1 and 25 will be generated:
9294

@@ -95,7 +97,7 @@
9597

9698
------------------------------------
9799

98-
> Original Msg : Test Cipher
100+
> Original Txt : Test Cipher
99101

100102
> Shift-key : 19
101103

@@ -109,7 +111,7 @@
109111

110112
------------------------------------
111113

112-
> Original Msg : Test Cipher
114+
> Original Txt : Test Cipher
113115

114116
> Shift-key : 24
115117

@@ -123,7 +125,7 @@
123125

124126
------------------------------------
125127

126-
> Original Msg : Test Cipher
128+
> Original Txt : Test Cipher
127129

128130
> Shift-key : 4
129131

@@ -132,7 +134,7 @@
132134

133135
---
134136

135-
### Message Decryption
137+
### Text Decryption
136138

137139
- For decrypting text, use the **`CaesarCipher.decrypt`** class method:
138140

@@ -195,16 +197,19 @@
195197
> Decrypted Shift-Key 24 : Qbpq Zfmebo
196198

197199
> Decrypted Shift-Key 25 : Paop Yeldan
200+
```
201+
202+
- The **`CaesarCipher.decrypt`** method will return all possible shifted-key variations of the given encrypted text as a dictionary.
203+
- This is NOT printed to stdout even if the `stdout_output` parameter is set to `True`.
198204

205+
- ```python
199206
{'Ozno Xdkczm': 0, 'Nymn Wcjbyl': 1, 'Mxlm Vbiaxk': 2, 'Lwkl Uahzwj': 3, 'Kvjk Tzgyvi': 4, 'Juij Syfxuh': 5, 'Ithi Rxewtg': 6, 'Hsgh Qwdvsf': 7, 'Grfg Pvcure': 8, 'Fqef Oubtqd': 9, 'Epde Ntaspc': 10, 'Docd Mszrob': 11, 'Cnbc Lryqna': 12, 'Bmab Kqxpmz': 13, 'Alza Jpwoly': 14, 'Zkyz Iovnkx': 15, 'Yjxy Hnumjw': 16, 'Xiwx Gmtliv': 17, 'Whvw Flskhu': 18, 'Vguv Ekrjgt': 19, 'Uftu Djqifs': 20, 'Test Cipher': 21, 'Sdrs Bhogdq': 22, 'Rcqr Agnfcp': 23, 'Qbpq Zfmebo': 24, 'Paop Yeldan': 25}
200207
```
201208

202-
- The **`CaesarCipher.decrypt`** method will return all possible shifted-key variations of the given encrypted message as a dictionary.
203-
204-
- **_Generally_**, the _most legible_ key output will be the correct decrypted message, assuming the encrypted message was legible initially.
209+
- **_Generally_**, the _most legible_ key output will be the correct decrypted text (assuming the encrypted text was legible initially).
205210

206-
- Regardless, the correct output **MUST** be one of the output values due to the limitations of the algorithm being tied to the length of the alphabet 26 and the number of possible integers [0-9].
207-
- This is also the reason why the algorithm is not recommended for serious real-world cryptography use cases.
211+
- Regardless, the correct output **MUST** be one of the output values due to the limitations of the algorithm being tied to the length of the alphabet [26] and the number of possible integers [0-9].
212+
- This is also the reason why the algorithm is not recommended for serious real-world cryptography use cases, as it is rather simple to decipher Caesar-Cipher encrypted text.
208213

209214
---
210215

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
readme = pathlib.Path("readme.md").read_text()
55
reqs = pathlib.Path("requirements.txt").read_text()
66
setup(name="MyCaesarCipher",
7-
version="0.3.0",
7+
version='0.4.0',
88
description=
99
'Simple cryptographic substitution-based cipher for encoding plaintext.',
1010
url='https://github.com/schlopp96/MyCaesarCipher',
@@ -32,5 +32,6 @@
3232
],
3333
keywords=[
3434
'cryptography', 'Caesar-Cipher', 'Caesar', 'Cipher', 'encryption',
35-
'decryption', 'cryptographic', 'module', 'script', 'python'
35+
'decryption', 'cryptographic', 'module', 'script', 'encrypt',
36+
'decrypt', 'python'
3637
])

0 commit comments

Comments
 (0)