-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_01.py
70 lines (58 loc) · 2.57 KB
/
task_01.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import getpass
print("-------------------------------------------- Caesar Cipher Tool --------------------------------------------------")
print("")
def analyze_message(message):
num_letters = sum(c.isalpha() for c in message)
num_digits = sum(c.isdigit() for c in message)
num_special = len(message) - num_letters - num_digits
print(f"Please check whether the message you provided was correct or not using the following hints:")
print(f"\nFirst and last characters of the message: {message[:1]}***{message[-1]}")
print(f"Total number of characters entered: {len(message)}")
print(f"Number of alphabetical letters: {num_letters}")
print(f"Number of digits: {num_digits}")
if num_special > 0:
print(f"Number of special characters: {num_special}")
print("Special characters are present in the message.")
else:
print("No special characters in the message.")
def caesar_cipher(text, shift, action):
result = ""
for char in text:
if char.isalpha():
start = ord('a') if char.islower() else ord('A')
if action == 'e':
result_char = chr((ord(char) - start + shift) % 26 + start)
elif action == 'd':
result_char = chr((ord(char) - start - shift) % 26 + start)
else:
raise ValueError("Invalid action. Use 'e' to encrypt or 'd' to decrypt")
result += result_char
else:
result += char
return result
def main():
while True:
action = input("Do you want to encrypt or decrypt a message? Input 'e' for encryption, 'd' for decryption, or 'q' to quit: ").lower()
if action == 'q':
print("Exiting the program. Goodbye!")
return
elif action in ['e', 'd']:
break
else:
print("Invalid action. Please choose 'e', 'd', or 'q' to quit.")
if action == 'q':
return
# Use of getpass to make the message input invisible
message = getpass.getpass(prompt="Enter the message (invisible, type carefully): ")
if action != 'q':
analyze_message(message)
# Use of getpass to make the shift value input invisible
shift = int(getpass.getpass(prompt="Enter the shift value (invisible, type carefully): "))
if action == 'e':
result = caesar_cipher(message, shift, 'e')
print("\nEncrypted Message:", result)
elif action == 'd':
result = caesar_cipher(message, shift, 'd')
print("\nDecrypted Message:", result)
if __name__ == "__main__":
main()