Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 50 additions & 28 deletions Caesar_Cipher/Caesar_cipher.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,67 @@
class main:
def __init__(self,key:dict) -> None:
class CaesarCipher:
"""
A class used to encrypt and decrypt strings using a substitution cipher key dictionary.
"""
def __init__(self, key: dict) -> None:
"""
Initializes the cipher with a key mapping dictionary.

:param key: dict mapping input characters to their encrypted output characters.
"""
self.key = key

def get_input(self) -> None:
"""
Prompts the user for alphabetic text input and converts it to lowercase.
"""
while True:
blank_string = str(input("Enter string to decrypt: "))
blank_string = str(input("Enter string to encrypt/decrypt: "))
if blank_string.isalpha():
blank_string = blank_string.lower()
self.blank_string = blank_string
self.blank_string = blank_string.lower()
break
else:
print("Input is not valid")
print("Input is not valid. Please enter alphabetic characters only.")
continue

def encrypt_string(self) -> str:
"""
Encrypts self.blank_string using the instance key dictionary.

:return: Encrypted string
"""
output = ""
for c in self.blank_string:
for k,v in self.key.items():
if k == c:
output += v
else:
continue
self.decrypted_string = output
return(output)
if c in self.key:
output += self.key[c]
self.encrypted_string = output
return output

def decrypt_string(self, string: str) -> str:
def decrypt_string(self, string: str = "") -> str:
"""
Decrypts a given string (or self.blank_string) using the inverse key dictionary.

:param string: Encrypted input string to decode.
:return: Decrypted original string
"""
output = ""
string = string.lower()
string = string.strip()
if string == "":
return(self.blank_string)
else:
for c in string:
for k,v in self.key.items():
if v == c:
output += k
target_string = string.lower().strip() if string else self.blank_string

return(output)
# Create inverse lookup map (v -> k)
inverse_key = {v: k for k, v in self.key.items()}
for c in target_string:
if c in inverse_key:
output += inverse_key[c]

return output

if __name__ == "__main__":
key ={"a": "d", "b": "e", "c": "f", "d": "g", "e": "h", "f": "i", "g": "j", "h": "k", "i": "l", "j": "m", "k": "n", "l": "o", "m": "p", "n": "q", "o": "r", "p": "s", "q": "t", "r": "u", "s": "v", "t": "w", "u": "x", "v": "y", "w": "z", "x": "a", "y": "b", "z": "c"}
main = main(key=key)
main.get_input()
print(main.encrypt_string())
key = {
"a": "d", "b": "e", "c": "f", "d": "g", "e": "h", "f": "i", "g": "j",
"h": "k", "i": "l", "j": "m", "k": "n", "l": "o", "m": "p", "n": "q",
"o": "r", "p": "s", "q": "t", "r": "u", "s": "v", "t": "w", "u": "x",
"v": "y", "w": "z", "x": "a", "y": "b", "z": "c"
}
cipher = CaesarCipher(key=key)
cipher.get_input()
encrypted = cipher.encrypt_string()
print(f"Encrypted Output: {encrypted}")
Empty file.
Binary file not shown.