2018-06-25 19:53:53 +02:00
|
|
|
from sys import argv
|
2018-06-29 17:36:03 +02:00
|
|
|
import sys
|
|
|
|
import binascii
|
2018-06-29 18:16:51 +02:00
|
|
|
import base64
|
2018-06-21 23:10:19 +02:00
|
|
|
|
2018-06-25 19:53:53 +02:00
|
|
|
def toBinary(string):
|
|
|
|
return "".join([format(ord(char),'#010b')[2:] for char in string])
|
|
|
|
|
2018-06-27 23:30:58 +02:00
|
|
|
def binnahex(var):
|
|
|
|
all = {"0000": "0",
|
|
|
|
"0001": "1",
|
|
|
|
"0010": "2",
|
|
|
|
"0011": "3",
|
|
|
|
"0100": "4",
|
|
|
|
"0101": "5",
|
|
|
|
"0110": "6",
|
|
|
|
"0111": "7",
|
|
|
|
"1000": "8",
|
|
|
|
"1001": "9",
|
|
|
|
"1010": "A",
|
|
|
|
"1011": "B",
|
|
|
|
"1100": "C",
|
|
|
|
"1101": "D",
|
|
|
|
"1110": "E",
|
|
|
|
"1111": "F"
|
|
|
|
}
|
|
|
|
i = 0
|
|
|
|
output = ""
|
|
|
|
|
|
|
|
while (len(var) % 4 != 0):
|
|
|
|
var = "0" + var
|
|
|
|
|
|
|
|
while (i < len(var)):
|
|
|
|
output = output + all[var[i:i + 4]]
|
|
|
|
i = i + 4
|
|
|
|
|
|
|
|
output = output.lstrip("0")
|
|
|
|
output = "0" if len(output) == 0 else output
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
2018-06-20 23:29:22 +02:00
|
|
|
#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
|
|
|
|
|
2018-06-29 18:16:51 +02:00
|
|
|
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")
|
|
|
|
|
|
|
|
|
2018-06-21 23:10:19 +02:00
|
|
|
def encode(message, poly16):
|
2018-06-29 17:36:03 +02:00
|
|
|
|
|
|
|
a=message
|
|
|
|
|
|
|
|
message = bytearray(message, 'ascii')
|
|
|
|
message = format(int.from_bytes(message, "big"), "b")
|
2018-06-21 23:10:19 +02:00
|
|
|
l_poly16 = len(poly16)
|
2018-06-29 17:36:03 +02:00
|
|
|
message = message + '0'*(l_poly16-1)
|
|
|
|
while len(message) % 8 != 0:
|
|
|
|
message = "0" + message
|
|
|
|
obroc = list(message)
|
2018-06-27 23:30:58 +02:00
|
|
|
for i in range(l_poly16 - 1):
|
|
|
|
if obroc[i] == "0":
|
|
|
|
obroc[i] = "1"
|
|
|
|
elif obroc[i] == '1':
|
|
|
|
obroc[i] = "0"
|
|
|
|
|
2018-06-29 17:36:03 +02:00
|
|
|
message = "".join(obroc)
|
|
|
|
remainder = divide(message, poly16)
|
2018-06-27 23:30:58 +02:00
|
|
|
ab=binnahex(remainder)
|
2018-06-29 18:16:51 +02:00
|
|
|
d=str(base64.b16decode(ab))[2:-1]
|
|
|
|
message_final = a + d
|
2018-06-29 17:36:03 +02:00
|
|
|
message_final = bytearray(message_final, 'ascii')
|
2018-06-29 18:16:51 +02:00
|
|
|
d = bytearray(d, 'ascii')
|
2018-06-27 23:30:58 +02:00
|
|
|
print("calosc : ", message_final)
|
2018-06-29 18:26:06 +02:00
|
|
|
print("crc hex : ", ab)
|
|
|
|
print("crc ascii : ", d)
|
2018-06-20 23:29:22 +02:00
|
|
|
|
|
|
|
poly16 = "10001000000100001"
|
2018-06-21 23:10:19 +02:00
|
|
|
|
2018-06-25 19:53:53 +02:00
|
|
|
def main():
|
|
|
|
global message
|
|
|
|
message = list(argv[1])
|
|
|
|
flag = argv[2]
|
|
|
|
if flag == 'encode':
|
2018-06-29 17:36:03 +02:00
|
|
|
encode(sys.argv[1], poly16)
|
2018-06-28 01:07:44 +02:00
|
|
|
elif flag == 'decode':
|
|
|
|
crc = argv[3]
|
2018-06-29 17:36:03 +02:00
|
|
|
decode(sys.argv[1], poly16,sys.argv[3])
|
2018-06-25 19:53:53 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
2018-06-29 18:27:46 +02:00
|
|
|
|