-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathdecipher.py
More file actions
44 lines (39 loc) · 1.42 KB
/
decipher.py
File metadata and controls
44 lines (39 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
A Python Script to implement Caesar Cipher. The technique is really basic.
# It shifts every character by a certain number (Shift Key)
# This number is secret and only the sender, receiver knows it.
# Using Such a Key, the message can be easily decoded as well.
# This Script Focuses on the Decoding Part only.
"""
def decipher(encrypt_string, shift_key):
"""
Implementation of DeCipher Technique.
Params: encrypt_string (required), shift_key (required)
Returns: decrypted_string
"""
# Initialise str to store the decrypted message
decrypted_string = ""
for text in encrypt_string:
if text == " ":
# For Blank Space, encrypted as it is
decrypted_string += text
elif text.isupper():
# For Upper Case
decrypted_string = decrypted_string + chr(
(ord(text) - shift_key - 65) % 26 + 65
)
else:
# For Lower Case
decrypted_string = decrypted_string + chr(
(ord(text) - shift_key - 97) % 26 + 97
)
return decrypted_string
if __name__ == "__main__":
"""
Function Calling
"""
encrypted_string = input("Enter the text to be decrypted: ")
shift = int(input("Enter the shift key: "))
print("Text before Decryption: ", encrypted_string)
print("Shift Key: ", shift)
print("Decrypted text: ", decipher(encrypted_string, shift))