forked from kalmar/DALGLI0
Zadanie3
Wersja 1, dla wielomianów przedstawionych binarnie, bez ascii
This commit is contained in:
parent
a96da65f41
commit
c8a2112082
77
Zadanie3
Normal file
77
Zadanie3
Normal file
@ -0,0 +1,77 @@
|
||||
#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
|
||||
|
||||
|
||||
def encode(message, poly16):
|
||||
|
||||
l_poly16 = len(poly16)
|
||||
|
||||
# Dodajemy n-1 "0" do wiadomosci
|
||||
appended_message = message + '0'*(l_poly16-1)
|
||||
remainder = divide(appended_message, poly16)
|
||||
|
||||
message_final = message + remainder
|
||||
print("calosc : ",
|
||||
message_final)
|
||||
|
||||
|
||||
|
||||
def decode(message2, poly16):
|
||||
|
||||
l_poly16 = len(poly16)
|
||||
appended_message2 = message2
|
||||
remainder = divide(appended_message2, poly16)
|
||||
print("reszta : ", remainder)
|
||||
|
||||
|
||||
|
||||
poly16 = "10001000000100001"
|
||||
print("podaj wiadomosc do zakodowania : ")
|
||||
message = str(raw_input())
|
||||
encode(message, poly16)
|
||||
print("podaj wiadomosc do odkodowania : ")
|
||||
message2 = str(raw_input())
|
||||
decode(message2, poly16)
|
Loading…
Reference in New Issue
Block a user