From c8a2112082bc399f298410fb8d706f6f637c1d6b Mon Sep 17 00:00:00 2001 From: Klaudia Kandulska Date: Wed, 20 Jun 2018 21:29:22 +0000 Subject: [PATCH] Zadanie3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Wersja 1, dla wielomianów przedstawionych binarnie, bez ascii --- Zadanie3 | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Zadanie3 diff --git a/Zadanie3 b/Zadanie3 new file mode 100644 index 0000000..3569a86 --- /dev/null +++ b/Zadanie3 @@ -0,0 +1,77 @@ +#XOR +def xor(a, b): + + # init + result = [] + + # porownuje znaki + for i in range(1, len(b)): + if a[i] == b[i]: + result.append('0') + else: + result.append('1') + + return ''.join(result) + + +# dzielenie modulo 2 +def divide(message, poly16): + + # Ilosc bitow dla operacji XOR 'na raz' + step = len(poly16) + + # podzial krok po kroku + temp = message[0 : step] + + while step < len(message): + + if temp[0] == '1': + + temp = xor(poly16, temp) + message[step] + + else: + # jezeli 1 bit po lewej == 0 + temp = xor('0'*step, temp) + message[step] + + # kolejny krok + step += 1 + + if temp[0] == '1': + temp = xor(poly16, temp) + else: + temp = xor('0'*step, temp) + + check = temp + return check + + +def encode(message, poly16): + + l_poly16 = len(poly16) + + # Dodajemy n-1 "0" do wiadomosci + appended_message = message + '0'*(l_poly16-1) + remainder = divide(appended_message, poly16) + + message_final = message + remainder + print("calosc : ", + message_final) + + + +def decode(message2, poly16): + + l_poly16 = len(poly16) + appended_message2 = message2 + remainder = divide(appended_message2, poly16) + print("reszta : ", remainder) + + + +poly16 = "10001000000100001" +print("podaj wiadomosc do zakodowania : ") +message = str(raw_input()) +encode(message, poly16) +print("podaj wiadomosc do odkodowania : ") +message2 = str(raw_input()) +decode(message2, poly16)