From 7e1e1a73489df4762090095a7d18f95f99a040c4 Mon Sep 17 00:00:00 2001 From: Klaudia Kandulska Date: Fri, 29 Jun 2018 15:36:03 +0000 Subject: [PATCH] Update 'Zadanie3' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Encode jako bytearray, decode wciąż działa wybiórczo --- Zadanie3 | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/Zadanie3 b/Zadanie3 index dc531f5..1be6210 100644 --- a/Zadanie3 +++ b/Zadanie3 @@ -1,4 +1,6 @@ from sys import argv +import sys +import binascii def toBinary(string): return "".join([format(ord(char),'#010b')[2:] for char in string]) @@ -36,9 +38,6 @@ def binnahex(var): return output -def toString(binaryString): - return "".join([chr(int(binaryString[i:i+8],2)) for i in range(0,len(binaryString),8)]) - #XOR def xor(a, b): @@ -85,7 +84,6 @@ def divide(message, poly16): check = temp return check - def decode(message_final,poly16,crc): l_poly16 = len(poly16) appended_message2 = toBinary(message_final) @@ -113,31 +111,31 @@ def decode(message_final,poly16,crc): elif suma > 0: print("false") - - def encode(message, poly16): - #konwersja z ascii do binarnego - aa=toBinary(message) - a=toString(aa) + + a=message + + message = bytearray(message, 'ascii') + message = format(int.from_bytes(message, "big"), "b") l_poly16 = len(poly16) - - obroc = list(aa) + message = message + '0'*(l_poly16-1) + while len(message) % 8 != 0: + message = "0" + message + obroc = list(message) for i in range(l_poly16 - 1): if obroc[i] == "0": obroc[i] = "1" elif obroc[i] == '1': obroc[i] = "0" - aa = "".join(obroc) - - # Dodajemy n-1 "0" do wiadomosci - appended_message = aa + '0'*(l_poly16-1) - remainder = divide(appended_message, poly16) + message = "".join(obroc) + remainder = divide(message, poly16) ab=binnahex(remainder) message_final = a + ab + message_final = bytearray(message_final, 'ascii') + ab = bytearray(ab, 'ascii') print("calosc : ", message_final) - print("crc : ", ab) - + print("crc : ", ab) poly16 = "10001000000100001" @@ -147,9 +145,9 @@ def main(): message = list(argv[1]) flag = argv[2] if flag == 'encode': - encode(message, poly16) + encode(sys.argv[1], poly16) elif flag == 'decode': crc = argv[3] - decode(message, poly16,crc) + decode(sys.argv[1], poly16,sys.argv[3]) if __name__ == '__main__': main()