1
0
forked from kalmar/DALGLI0

Update 'Zadanie3'

This commit is contained in:
Klaudia Kandulska 2018-06-29 16:16:51 +00:00
parent 066de73659
commit c624158b50

View File

@ -1,6 +1,7 @@
from sys import argv from sys import argv
import sys import sys
import binascii import binascii
import base64
def toBinary(string): def toBinary(string):
return "".join([format(ord(char),'#010b')[2:] for char in string]) return "".join([format(ord(char),'#010b')[2:] for char in string])
@ -101,7 +102,8 @@ def decode(message,poly16,crc):
obroc[i] = "0" obroc[i] = "0"
msg = "".join(obroc) msg = "".join(obroc)
print(msg)
print(crc2)
remainder = divide(msg, poly16) remainder = divide(msg, poly16)
#Poprawione sprawdzanie #Poprawione sprawdzanie
suma = 0 suma = 0
@ -114,6 +116,36 @@ def decode(message,poly16,crc):
elif suma > 0: elif suma > 0:
print("false") print("false")
def decode(message,poly16,crc):
l_poly16 = len(poly16)
message = bytearray(message, 'ascii')
message = format(int.from_bytes(message, "big"), "b")
dec = int(crc, 16)
crc2=bin(dec)[2:]
msg=message+crc2
while len(msg) % 8 != 0:
msg = "0" + msg
obroc = list(msg)
for i in range(l_poly16 - 1):
if obroc[i] == "0":
obroc[i] = "1"
elif obroc[i] == '1':
obroc[i] = "0"
msg = "".join(obroc)
remainder = divide(msg, poly16)
#Poprawione sprawdzanie
suma = 0
for i in range(0, len(remainder)):
if '1' in remainder:
suma = suma+1
if suma == 0:
print("true ")
elif suma > 0:
print("false")
def encode(message, poly16): def encode(message, poly16):
a=message a=message
@ -134,11 +166,12 @@ def encode(message, poly16):
message = "".join(obroc) message = "".join(obroc)
remainder = divide(message, poly16) remainder = divide(message, poly16)
ab=binnahex(remainder) ab=binnahex(remainder)
message_final = a + ab d=str(base64.b16decode(ab))[2:-1]
message_final = a + d
message_final = bytearray(message_final, 'ascii') message_final = bytearray(message_final, 'ascii')
ab = bytearray(ab, 'ascii') d = bytearray(d, 'ascii')
print("calosc : ", message_final) print("calosc : ", message_final)
print("crc : ", ab) print("crc : ", d)
poly16 = "10001000000100001" poly16 = "10001000000100001"