class Deszyfrator: def __init__(self): self.alfabet = 'AĄBCĆDEĘFGHIJKLŁMNŃOÓPRSŚTUWYZŹŻ' try: plik = open('tekst4.txt', 'r') self.tekst4 = plik.read() plik.close() except FileNotFoundError: print("Brak pliku tekst4.txt. Dodaj plik tekst4.txt do projektu.") self.error = True return try: plik = open('lalka.txt', 'r') self.lalka = plik.read() plik.close() except FileNotFoundError: print("Brak pliku lalka.txt. Dodaj plik lalka.txt do projektu.") self.error = True return self.stworz_kandydatow() self.ustalone_x = [False] * 32 self.ustalone_y = [False] * 32 self.przejscia = [-1] * 32 self.error = False self.generuj_permutacje() def zlicz(self, tekst): ile = [0] * 32 suma = 0 for znak in tekst.upper(): if znak in self.alfabet: poz = self.alfabet.index(znak) ile[poz] = ile[poz] + 1 suma += 1 return [i/suma for i in ile] def stworz_kandydatow(self): ile_org = self.zlicz(self.lalka) ile_szyfr = self.zlicz(self.tekst4) self.kandydaci = [] for x in range(32): pary = [] for y in range(32): pary.append((abs(ile_szyfr[x] - ile_org[y]), self.alfabet[y])) sor_par = sorted(pary, key = lambda x: x[0]) napis = '' for t in sor_par: napis += t[1] self.kandydaci.append(napis) def ustal(self, slowo_x, slowo_y): if self.error: return slowo_x = slowo_x.upper() slowo_y = slowo_y.upper() if len(slowo_x) != len(slowo_y): print('Słowa "' + slowo_x + '" i "' + slowo_y + '" są różnej długości.') self.error = True return for (litera_x, litera_y) in zip(slowo_x, slowo_y): x = self.alfabet.index(litera_x) y = self.alfabet.index(litera_y) if self.ustalone_y[y]: ktory_x = 0 for i in range(32): if self.ustalone_x[i] and self.przejscia[i] == y: ktory_x = i break if ktory_x != x: print('Nieudana próba ustalenia "' + slowo_x + '" -> "' + slowo_y + '".') print('Przejście na literę "' + litera_y + '" ustalone dwukrotnie:') print(self.alfabet[ktory_x], '->', self.alfabet[y]) print(self.alfabet[x], '->', self.alfabet[y]) self.error = True elif self.ustalone_x[x]: if self.przejscia[x] != y: print('Nieudana próba ustalenia "' + slowo_x + '" -> "' + slowo_y + '".') print('Przejście z litery "' + litera_x + '" ustalone dwukrotnie:') print(self.alfabet[x], '->', self.alfabet[self.przejscia[x]]) print(self.alfabet[x], '->', self.alfabet[y]) self.error = True else: self.przejscia[x] = y self.ustalone_x[x] = True self.ustalone_y[y] = True def generuj_permutacje(self): wykorzystane = [x for x in self.ustalone_y] for x in range(32): if not self.ustalone_x[x]: for litera_y in self.kandydaci[x]: y = self.alfabet.index(litera_y) if not wykorzystane[y]: self.przejscia[x] = y wykorzystane[y] = True break self.permutacja = '' for x in range(32): self.permutacja += self.alfabet[self.przejscia[x]] def wypisz_kandydatow(self): print('Kandydaci na deszyfrację:') for x in range(32): print(self.alfabet[x], end = ': ') print(self.kandydaci[x]) print() def odszyfruj(self): duzyA = self.alfabet malyA = self.alfabet.lower() duzaP = self.permutacja malaP = self.permutacja.lower() trans = str.maketrans(duzyA + malyA, duzaP + malaP) self.deszyfr = self.tekst4.translate(trans) def wypisz_slowo(self, slowo, czy_duze = [True] * 32): for x in slowo: if czy_duze[self.alfabet.index(x)]: print(x, end = '') else: print(x.lower(), end = '') print(' ', end = '') def wypisz_permutacje(self): print('Permutacja:') print(self.alfabet) self.wypisz_slowo(self.permutacja, self.ustalone_y) print() print() def najczestsze(self, tekst, dlugosc): ile = {} for s in tekst.upper().split(): if (len(s)) == dlugosc: same_litery = True for x in s: if not x in self.alfabet: same_litery = False if same_litery: if s in ile: ile[s] += 1 else: ile[s] = 1 lista = sorted(ile.items(), key = lambda x: x[1], reverse = True) return [x[0] for x in lista[:15]] def wypisz_jezyk(self): jez = [] for i in range(3): jez.append(self.najczestsze(self.lalka, i + 1)) print('Najczęstsze słowa w języku:') for i in range(3): for x in jez[i]: self.wypisz_slowo(x) print() print() def wypisz_najczestsze(self): print('Najczęstsze słowa w szyfrogramie i w rozszyfrowanej wiadomości') szyfr = [] deszyfr = [] for i in range(3): szyfr.append(self.najczestsze(self.tekst4, i + 1)) deszyfr.append(self.najczestsze(self.deszyfr, i + 1)) for i in range(3): for x in szyfr[i]: self.wypisz_slowo(x) print() for x in deszyfr[i]: self.wypisz_slowo(x, self.ustalone_y) print() print() def wypisz_fragment(self): print('Fragment rozszyfrowanego szyfrogramu:') print(self.deszyfr[-500:]) def sprawdz_permutacje(self): if self.error: return self.generuj_permutacje() self.odszyfruj() self.wypisz_permutacje() self.wypisz_najczestsze() self.wypisz_fragment()