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
66 changes: 32 additions & 34 deletions Caesar_Cipher/Caesar_cipher.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
class main:
def __init__(self,key:dict) -> None:
class Cipher:
def __init__(self, key: dict) -> None:
self.key = key

def get_input(self) -> None:
self.rev_key = {v: k for k, v in key.items()} # reverse mapping

def get_input(self) -> None:
while True:
blank_string = str(input("Enter string to decrypt: "))
if blank_string.isalpha():
blank_string = blank_string.lower()
self.blank_string = blank_string
text = input("Enter string to encrypt: ").strip()
if text.isalpha():
self.blank_string = text.lower()
break
else:
print("Input is not valid")
continue

print("Input is not valid! Only alphabets allowed.")

def encrypt_string(self) -> str:
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)

output += self.key.get(c, c)
return output

def decrypt_string(self, string: str) -> str:
output = ""
string = string.lower()
string = string.strip()
string = string.strip().lower()
if string == "":
return(self.blank_string)
else:
for c in string:
for k,v in self.key.items():
if v == c:
output += k

return(output)
return self.blank_string
output = ""
for c in string:
output += self.rev_key.get(c, 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())
# Fixed: Caesar cipher with shift of 3 (properly wrapping around)
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 = Cipher(key)
cipher.get_input()
encrypted = cipher.encrypt_string()
print("Encrypted:", encrypted)
print("Decrypted:", cipher.decrypt_string(encrypted))
6 changes: 5 additions & 1 deletion Caesar_Cipher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ The script was created with Python3 and the built-in functions in it
<!--Remove the below lines and add yours -->
```bash
python3 caesarcypher.py
Enter string to decrypt: hello
Enter string to encrypt: hello
khoor

Enter string to decrypt: khoor
hello

```
## 📺 Demo
<p align="center">
Expand Down
Binary file not shown.