33import random
44import string
55
6+
67class ShuffledShiftCipher :
78 """
89 Enhanced Caesar Cipher with shuffled character set for stronger encryption.
@@ -11,7 +12,7 @@ class ShuffledShiftCipher:
1112
1213 def __init__ (self , passcode : str | None = None ) -> None :
1314 """
14- Initialize cipher with optional passcode.
15+ Initialize cipher with optional passcode.
1516 Generates random passcode if none provided.
1617 """
1718 self .__passcode = passcode or self .__passcode_creator ()
@@ -41,13 +42,13 @@ def __make_key_list(self) -> list[str]:
4142 breakpoints = sorted (set (self .__passcode ))
4243 shuffled : list [str ] = [] # Explicit type annotation
4344 temp : list [str ] = [] # Explicit type annotation
44-
45+
4546 for char in key_options :
4647 temp .append (char )
4748 if char in breakpoints or char == key_options [- 1 ]:
4849 shuffled .extend (reversed (temp ))
4950 temp .clear ()
50-
51+
5152 return shuffled
5253
5354 def __make_shift_key (self ) -> int :
@@ -60,42 +61,45 @@ def encrypt(self, plaintext: str) -> str:
6061 """Encrypt plaintext using shuffled shift cipher."""
6162 encoded : list [str ] = [] # Explicit type annotation
6263 key_len = len (self .__key_list )
63-
64+
6465 for char in plaintext :
6566 pos = self .__key_list .index (char )
6667 new_pos = (pos + self .__shift_key ) % key_len
6768 encoded .append (self .__key_list [new_pos ])
68-
69+
6970 return "" .join (encoded )
7071
7172 def decrypt (self , encoded_message : str ) -> str :
7273 """Decrypt message using shuffled shift cipher."""
7374 decoded : list [str ] = [] # Explicit type annotation
7475 key_len = len (self .__key_list )
75-
76+
7677 for char in encoded_message :
7778 pos = self .__key_list .index (char )
7879 new_pos = (pos - self .__shift_key ) % key_len
7980 decoded .append (self .__key_list [new_pos ])
80-
81+
8182 return "" .join (decoded )
8283
84+
8385def test_end_to_end () -> str :
8486 """Test full encryption-decryption cycle."""
8587 msg = "Hello, this is a modified Caesar cipher"
8688 cipher = ShuffledShiftCipher ()
8789 return cipher .decrypt (cipher .encrypt (msg ))
8890
91+
8992if __name__ == "__main__" :
9093 import doctest
94+
9195 doctest .testmod ()
92-
96+
9397 # Example usage
9498 cipher = ShuffledShiftCipher ("SecurePass123" )
9599 original = "Encryption test!"
96100 encrypted = cipher .encrypt (original )
97101 decrypted = cipher .decrypt (encrypted )
98-
102+
99103 print (f"Original: { original } " )
100104 print (f"Encrypted: { encrypted } " )
101105 print (f"Decrypted: { decrypted } " )
0 commit comments