-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathcipher.py
More file actions
52 lines (47 loc) · 1.58 KB
/
cipher.py
File metadata and controls
52 lines (47 loc) · 1.58 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
45
46
47
48
49
50
51
52
"""
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 Encryption Part
"""
def cipher(imput_string, shift_key):
"""
Implementation of Crypto Technique.
Params: input_string (required), shift_key (required)
Returns: encrypted_string
:type imput_string: str
:type shift_key: int
"""
# Initialise str to store the encrypted message
encrypted_string = ""
for text in imput_string:
"""
There are 3 possibilities
- Lower Case
- Upper Case
- Blank Space
"""
if text == " ":
# For Blank Space, encrypted as it is
encrypted_string += text
elif text.isupper():
# For Upper Case
encrypted_string = encrypted_string + chr(
(ord(text) + shift_key - 65) % 26 + 65
)
else:
# For Lower Case
encrypted_string = encrypted_string + chr(
(ord(text) + shift_key - 97) % 26 + 97
)
return encrypted_string
if __name__ == "__main__":
"""
Function Calling
"""
imput_string = input("Enter the text to be encrypted: ")
shift = int(input("Enter the shift key: "))
print("Text before Encryption: ", imput_string)
print("Shift Key: ", shift)
print("Encrypted text: ", cipher(imput_string, shift))