forked from kalmar/DALGLI0
Update 'Zadanie3'
Encode jako bytearray, decode wciąż działa wybiórczo
This commit is contained in:
parent
b8a923877f
commit
7e1e1a7348
36
Zadanie3
36
Zadanie3
@ -1,4 +1,6 @@
|
|||||||
from sys import argv
|
from sys import argv
|
||||||
|
import sys
|
||||||
|
import binascii
|
||||||
|
|
||||||
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])
|
||||||
@ -36,9 +38,6 @@ def binnahex(var):
|
|||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
def toString(binaryString):
|
|
||||||
return "".join([chr(int(binaryString[i:i+8],2)) for i in range(0,len(binaryString),8)])
|
|
||||||
|
|
||||||
#XOR
|
#XOR
|
||||||
def xor(a, b):
|
def xor(a, b):
|
||||||
|
|
||||||
@ -85,7 +84,6 @@ def divide(message, poly16):
|
|||||||
check = temp
|
check = temp
|
||||||
return check
|
return check
|
||||||
|
|
||||||
|
|
||||||
def decode(message_final,poly16,crc):
|
def decode(message_final,poly16,crc):
|
||||||
l_poly16 = len(poly16)
|
l_poly16 = len(poly16)
|
||||||
appended_message2 = toBinary(message_final)
|
appended_message2 = toBinary(message_final)
|
||||||
@ -113,33 +111,33 @@ def decode(message_final,poly16,crc):
|
|||||||
elif suma > 0:
|
elif suma > 0:
|
||||||
print("false")
|
print("false")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def encode(message, poly16):
|
def encode(message, poly16):
|
||||||
#konwersja z ascii do binarnego
|
|
||||||
aa=toBinary(message)
|
|
||||||
a=toString(aa)
|
|
||||||
l_poly16 = len(poly16)
|
|
||||||
|
|
||||||
obroc = list(aa)
|
a=message
|
||||||
|
|
||||||
|
message = bytearray(message, 'ascii')
|
||||||
|
message = format(int.from_bytes(message, "big"), "b")
|
||||||
|
l_poly16 = len(poly16)
|
||||||
|
message = message + '0'*(l_poly16-1)
|
||||||
|
while len(message) % 8 != 0:
|
||||||
|
message = "0" + message
|
||||||
|
obroc = list(message)
|
||||||
for i in range(l_poly16 - 1):
|
for i in range(l_poly16 - 1):
|
||||||
if obroc[i] == "0":
|
if obroc[i] == "0":
|
||||||
obroc[i] = "1"
|
obroc[i] = "1"
|
||||||
elif obroc[i] == '1':
|
elif obroc[i] == '1':
|
||||||
obroc[i] = "0"
|
obroc[i] = "0"
|
||||||
|
|
||||||
aa = "".join(obroc)
|
message = "".join(obroc)
|
||||||
|
remainder = divide(message, poly16)
|
||||||
# Dodajemy n-1 "0" do wiadomosci
|
|
||||||
appended_message = aa + '0'*(l_poly16-1)
|
|
||||||
remainder = divide(appended_message, poly16)
|
|
||||||
ab=binnahex(remainder)
|
ab=binnahex(remainder)
|
||||||
message_final = a + ab
|
message_final = a + ab
|
||||||
|
message_final = bytearray(message_final, 'ascii')
|
||||||
|
ab = bytearray(ab, 'ascii')
|
||||||
print("calosc : ", message_final)
|
print("calosc : ", message_final)
|
||||||
print("crc : ", ab)
|
print("crc : ", ab)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
poly16 = "10001000000100001"
|
poly16 = "10001000000100001"
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@ -147,9 +145,9 @@ def main():
|
|||||||
message = list(argv[1])
|
message = list(argv[1])
|
||||||
flag = argv[2]
|
flag = argv[2]
|
||||||
if flag == 'encode':
|
if flag == 'encode':
|
||||||
encode(message, poly16)
|
encode(sys.argv[1], poly16)
|
||||||
elif flag == 'decode':
|
elif flag == 'decode':
|
||||||
crc = argv[3]
|
crc = argv[3]
|
||||||
decode(message, poly16,crc)
|
decode(sys.argv[1], poly16,sys.argv[3])
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user