This repository was archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage_decorder.py
More file actions
98 lines (87 loc) · 3.42 KB
/
Message_decorder.py
File metadata and controls
98 lines (87 loc) · 3.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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from pycipher import Playfair
#Welcome message
print("Welcome to my decoder. \n" + "It decodes coded messages in Caesar cipher, Morse code, Public-key cryptography and Playfair cipher. \n" + "If you wish to use Caesar cipher input Caesar as code type, Morse for Morse code, Keys for Public-key cryptography and Playfair for Playfair cipher. \n" + "If you are unsure about what each type of code is/does or are just interested in coded messages input Info as the code type and I will give you a link to a fun page explaining it!")
#initial values set ups
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbers = "1234567890"
#Morse dictionary settup
morse_dictionary = {".-":"A","-...":"B","-.-.":"C"}
morse_dictionary["-.."] = "D"
morse_dictionary["."] = "E"
morse_dictionary["..-."] = "F"
morse_dictionary["--."] = "G"
morse_dictionary["...."] = "H"
morse_dictionary[".."] = "I"
morse_dictionary[".---"] = "J"
morse_dictionary["-.-"] = "K"
morse_dictionary[".-.."] = "L"
morse_dictionary["--"] = "M"
morse_dictionary["-."] = "N"
morse_dictionary["---"] = "O"
morse_dictionary[".--."] = "P"
morse_dictionary["--.-"] = "Q"
morse_dictionary[".-."] = "R"
morse_dictionary["..."] = "S"
morse_dictionary["-"] = "T"
morse_dictionary["..-"] = "U"
morse_dictionary["...-"] = "V"
morse_dictionary[".--"] = "W"
morse_dictionary["-..-"] = "X"
morse_dictionary["-.--"] = "Y"
morse_dictionary["--.."] = "Z"
punctuation = ".,?'! "
print("Type of code used:")
code_type = input("type_of_code \n").title()
decoded_message = ""
#Info
if code_type == "Info":
print("https://www.enkivillage.org/types-of-codes.html")
decoded_message += "None"
#Caesar cipher decoding
elif code_type == "Caesar":
print("Input the offset by which the message in coded within the alphabet: \n")
x = int(input("code letter shift"))
print("Input coded message:")
message = input("Coded message \n")
for letter in message:
if not letter in punctuation:
letter_value = letters.find(letter)
decoded_message += letters[(letter_value + x) % len(letters)]
else:
decoded_message += letter
#Morse
elif code_type == "Morse":
print("Input coded message:")
message = input("Coded message \n")
for sign in message.split(" "):
if sign in morse_dictionary:
decoded_message += morse_dictionary[sign]
else:
decoded_message += " "
#Public-key cryptography
elif code_type == "Keys" :
print("Input public_key: " )
public_key= input("Public key \n")
while len(public_key) != 6:
if len(public_key) > 6:
print ("Error! Public key's are 6 characters long!")
else:
print ("Error! Public key's are 6 characters long!")
public_key= input("Public key \n")
print("Input private key:" )
private_key= input("Private key \n")
while len(private_key) != 2:
if len(private_key) > 2:
print ("Error! Public key's are 2 characters long!")
else:
print ("Error! Public key's are 2 characters long!")
private_key= input("Private key \n")
decoded_message += str(int(private_key) * int(public_key))
#Playfair Cipher
#check out kanpurpython.wordpress.com/2017/08/22/playfair-cipher-using-python/
elif code_type == "Playfair":
print("Input coded message:")
message = input("Coded message \n")
decoded_message += Playfair("cdabefhgkijlmnoprqstuvzwxy").decipher(message)
print("Decoded message: \n" + decoded_message.title())
print("You welcome.")