Skip to content

Commit 31a2b3c

Browse files
authored
Update shuffled_shift_cipher.py
1 parent 9de4e33 commit 31a2b3c

1 file changed

Lines changed: 13 additions & 17 deletions

File tree

ciphers/shuffled_shift_cipher.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import random
44
import string
55

6-
76
class ShuffledShiftCipher:
87
"""
98
Enhanced Caesar Cipher with shuffled character set for stronger encryption.
@@ -12,7 +11,7 @@ class ShuffledShiftCipher:
1211

1312
def __init__(self, passcode: str | None = None) -> None:
1413
"""
15-
Initialize cipher with optional passcode.
14+
Initialize cipher with optional passcode.
1615
Generates random passcode if none provided.
1716
"""
1817
self.__passcode = passcode or self.__passcode_creator()
@@ -40,15 +39,15 @@ def __make_key_list(self) -> list[str]:
4039
# Get printable characters except rare whitespace
4140
key_options = string.printable.strip("\r\x0b\x0c")
4241
breakpoints = sorted(set(self.__passcode))
43-
shuffled = []
44-
temp = []
45-
42+
shuffled: list[str] = [] # Explicit type annotation
43+
temp: list[str] = [] # Explicit type annotation
44+
4645
for char in key_options:
4746
temp.append(char)
4847
if char in breakpoints or char == key_options[-1]:
4948
shuffled.extend(reversed(temp))
5049
temp.clear()
51-
50+
5251
return shuffled
5352

5453
def __make_shift_key(self) -> int:
@@ -59,47 +58,44 @@ def __make_shift_key(self) -> int:
5958

6059
def encrypt(self, plaintext: str) -> str:
6160
"""Encrypt plaintext using shuffled shift cipher."""
62-
encoded = []
61+
encoded: list[str] = [] # Explicit type annotation
6362
key_len = len(self.__key_list)
64-
63+
6564
for char in plaintext:
6665
pos = self.__key_list.index(char)
6766
new_pos = (pos + self.__shift_key) % key_len
6867
encoded.append(self.__key_list[new_pos])
69-
68+
7069
return "".join(encoded)
7170

7271
def decrypt(self, encoded_message: str) -> str:
7372
"""Decrypt message using shuffled shift cipher."""
74-
decoded = []
73+
decoded: list[str] = [] # Explicit type annotation
7574
key_len = len(self.__key_list)
76-
75+
7776
for char in encoded_message:
7877
pos = self.__key_list.index(char)
7978
new_pos = (pos - self.__shift_key) % key_len
8079
decoded.append(self.__key_list[new_pos])
81-
80+
8281
return "".join(decoded)
8382

84-
8583
def test_end_to_end() -> str:
8684
"""Test full encryption-decryption cycle."""
8785
msg = "Hello, this is a modified Caesar cipher"
8886
cipher = ShuffledShiftCipher()
8987
return cipher.decrypt(cipher.encrypt(msg))
9088

91-
9289
if __name__ == "__main__":
9390
import doctest
94-
9591
doctest.testmod()
96-
92+
9793
# Example usage
9894
cipher = ShuffledShiftCipher("SecurePass123")
9995
original = "Encryption test!"
10096
encrypted = cipher.encrypt(original)
10197
decrypted = cipher.decrypt(encrypted)
102-
98+
10399
print(f"Original: {original}")
104100
print(f"Encrypted: {encrypted}")
105101
print(f"Decrypted: {decrypted}")

0 commit comments

Comments
 (0)