-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cesar.py
53 lines (42 loc) · 1.63 KB
/
Cesar.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
# Description: This file contains the functions to decode a message encoded with a Cesar algorithm with an unknown key
import matplotlib.pyplot as plt
from common import open_file, save, frequency_analysis
import logging
def decode(message, key):
"""
Decodes a message using the Caesar cipher algorithm.
Args:
message (str): The message to be decoded.
key (int): The key used for decoding.
Returns:
str: The decoded message.
"""
newmessage = ""
for letter in message:
newmessage += chr(ord(letter) + key)
return newmessage
def Cesar(message):
"""
Decodes a message encrypted using the Caesar cipher with an unknown key.
Parameters:
message (str): The message to be decrypted.
Returns:
tuple: A tuple containing the decoded message and the key used for decoding.
"""
key = frequency_analysis(message)
return decode(message, key), key
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format='%(levelname)s-%(asctime)s : %(message)s', datefmt='%H:%M:%S')
# decode the messages and save them
path=r'.\Messages\Encoded_messages\message_2.txt'
message = open_file(path)
decoded_message,key = Cesar(message)
logging.info(f"{decoded_message[:100]} \n[...]\n {decoded_message[-100:]} \n")
logging.info(f"Key= {key}")
save(decoded_message)
path=r'.\Messages\Encoded_messages\message_3.txt'
message = open_file(path)
decoded_message,key = Cesar(message)
logging.info(f"{decoded_message[:100]} \n[...]\n {decoded_message[-100:]} \n")
logging.info(f"Key= {key}")
save(decoded_message)