1
0
forked from kalmar/DALGLI0

Update 'Zadanie3'

Encode jako bytearray, decode wciąż działa wybiórczo
This commit is contained in:
Klaudia Kandulska 2018-06-29 15:36:03 +00:00
parent b8a923877f
commit 7e1e1a7348

View File

@ -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()