-
Notifications
You must be signed in to change notification settings - Fork 0
/
morse_without_separator.py
99 lines (77 loc) · 2.67 KB
/
morse_without_separator.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
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
99
morse_dict = { 'A':'.-', 'B':'-...',
'C':'-.-.', 'D':'-..', 'E':'.',
'F':'..-.', 'G':'--.', 'H':'....',
'I':'..', 'J':'.---', 'K':'-.-',
'L':'.-..', 'M':'--', 'N':'-.',
'O':'---', 'P':'.--.', 'Q':'--.-',
'R':'.-.', 'S':'...', 'T':'-',
'U':'..-', 'V':'...-', 'W':'.--',
'X':'-..-', 'Y':'-.--', 'Z':'--..',
'Å':'.--.-', 'Ä':'.-.-', 'Ö':'---.',
',':'--..--', '.':'.-.-.-', '?':'..--..' }
morse_dict2 = { '.-':'A', '-...':'B',
'-.-.':'C', '-..':'D', '.':'E',
'..-.':'F', '--.':'G', '....':'H',
'..':'I', '.---':'J', '-.-':'K',
'.-..':'L', '--':'M', '-.':'N',
'---':'O', '.--.':'P', '--.-':'Q',
'.-.':'R', '...':'S', '-':'T',
'..-':'U', '...-':'V', '.--':'W',
'-..-':'X', '-.--':'Y', '--..':'Z',
'.--.-':'Å', '.-.-':'Ä', '---.':'Ö',
'--..--':',', '.-.-.-':'.', '..--..':'?'}
morse_list = [
'...----.-.-.-..-.-....---..-.-..-.',
'......---....-.--..-.-.----...-...-...-',
'..-...-......-------.-.-....-......-',
'-----.-.....-.....-...-...----.-...-...',
'-....-....-.-.-.-.-.....-...-...-...-.---.-.-.-.-..-',
'......-..-.--...-.-.-.-.-',
'--...--...-....-....-...--.--...--.',
]
cleartext = ''
morse_string = '' #enter string (for encryptor function)
decrypted = ''
with open("barnvisor.txt", "r", encoding="utf-8") as input:
lines = input.readlines()
def encryptor():
for line in lines:
line = line.strip().upper()
#print(line)
enc_string = ""
for char in line:
if char in morse_dict:
enc_string += morse_dict[char]
if enc_string in morse_list:
print(enc_string, line)
def decryptor(morse, decrypted):
if morse[:1] in morse_dict.values():
morse_char = morse[:1]
dec_char = morse_dict2[morse_char]
decrypted += dec_char
morse = morse[1:]
decryptor(morse, decrypted)
if morse[:2] in morse_dict.values():
morse_char = morse[:2]
dec_char = morse_dict2[morse_char]
decrypted += dec_char
morse = morse[2:]
decryptor(morse, decrypted)
if morse[:3] in morse_dict.values():
morse_char = morse[:3]
dec_char = morse_dict2[morse_char]
decrypted += dec_char
morse = morse[3:]
decryptor(morse, decrypted)
if morse[:4] in morse_dict.values():
morse_char = morse[:4]
dec_char = morse_dict2[morse_char]
decrypted += dec_char
morse = morse[4:]
decryptor(morse, decrypted)
if len(morse) == 0:
f = open("output.txt", "a")
f.write(decrypted + "\n")
f.close()
#decryptor(morse_string, decrypted)
encryptor()