2018-06-21 23:10:19 +02:00
|
|
|
def toBinary(string):
|
|
|
|
return "".join([format(ord(char),'#010b')[2:] for char in string])
|
|
|
|
|
|
|
|
def toString(binaryString):
|
|
|
|
return "".join([chr(int(binaryString[i:i+8],2)) for i in range(0,len(binaryString),8)])
|
|
|
|
|
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-21 23:10:19 +02:00
|
|
|
def decode(message_final,poly16):
|
2018-06-20 23:29:22 +02:00
|
|
|
|
|
|
|
l_poly16 = len(poly16)
|
2018-06-21 23:10:19 +02:00
|
|
|
appended_message2 = message_final
|
2018-06-20 23:29:22 +02:00
|
|
|
remainder = divide(appended_message2, poly16)
|
2018-06-21 23:10:19 +02:00
|
|
|
#sprawdzenie kazdego bitu
|
2018-06-21 14:48:23 +02:00
|
|
|
for i in range(0, len(remainder)):
|
|
|
|
if '1' not in remainder:
|
|
|
|
print("true ")
|
2018-06-21 23:10:19 +02:00
|
|
|
elif '1' in remainder:
|
|
|
|
print("false ")
|
|
|
|
|
|
|
|
|
|
|
|
def encode(message, poly16):
|
|
|
|
#konwersja z ascii do binarnego
|
|
|
|
aa=toBinary(message)
|
|
|
|
|
|
|
|
l_poly16 = len(poly16)
|
|
|
|
|
|
|
|
# Dodaj n-1 "0" do wiadomosci
|
|
|
|
appended_message = aa + '0'*(l_poly16-1)
|
|
|
|
remainder = divide(appended_message, poly16)
|
|
|
|
|
|
|
|
message_final = aa + remainder
|
|
|
|
print("binarnie calosc : ", message_final)
|
|
|
|
#konwersja z ascii do binarnego
|
|
|
|
ab=toString(message_final)
|
|
|
|
print("w ascii : ", ab)
|
|
|
|
print("sprawdzenie : ")
|
|
|
|
decode(message_final, poly16)
|
2018-06-20 23:29:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
poly16 = "10001000000100001"
|
|
|
|
print("podaj wiadomosc do zakodowania : ")
|
|
|
|
message = str(raw_input())
|
|
|
|
encode(message, poly16)
|
2018-06-21 23:10:19 +02:00
|
|
|
|
|
|
|
|