Rozwiązanie zadania "CRC" #24
131
main.py
Normal file
131
main.py
Normal file
@ -0,0 +1,131 @@
|
||||
import sys
|
||||
import platform
|
||||
|
||||
def encode(m):
|
||||
m = bytearray(m, "ascii")
|
||||
message = format(int.from_bytes(m, "big"), "b")
|
||||
generator = "10001000000100001"
|
||||
gen_deg = len(generator)
|
||||
|
||||
message += "0" * (gen_deg - 1)
|
||||
while len(message) % 8 != 0:
|
||||
message = "0" + message
|
||||
|
||||
swapped = list(message)
|
||||
for i in range(gen_deg - 1):
|
||||
if swapped[i] == "0":
|
||||
swapped[i] = "1"
|
||||
else:
|
||||
swapped[i] = "0"
|
||||
|
||||
message = "".join(swapped)
|
||||
|
||||
while True:
|
||||
|
||||
if message[0] == "0":
|
||||
message = message[1:]
|
||||
else:
|
||||
chunk = message[:gen_deg]
|
||||
|
||||
if len(chunk) < gen_deg:
|
||||
|
||||
while len(chunk) % 8 != 0:
|
||||
chunk = "0" + chunk
|
||||
|
||||
fb = str(hex(int(chunk[:8], 2)))[2:]
|
||||
|
||||
if len(fb) == 1:
|
||||
fb = "0" + fb
|
||||
|
||||
sb = str(hex(int(chunk[8:], 2)))[2:]
|
||||
|
||||
if len(sb) == 1:
|
||||
sb = "0" + sb
|
||||
|
||||
m += b"\\x" + bytes(fb, "ascii")
|
||||
m += b"\\x" + bytes(sb, "ascii")
|
||||
return m
|
||||
|
||||
remainder = format(int(chunk, 2) ^ int(generator, 2), "b")
|
||||
message = remainder + message[gen_deg:]
|
||||
|
||||
|
||||
def decode(n):
|
||||
|
||||
if platform.system() == "Windows":
|
||||
n = n.split("\\\\x")
|
||||
else:
|
||||
n = n.split("\\x")
|
||||
|
||||
msg = n[0]
|
||||
message = ""
|
||||
|
||||
for ch in msg:
|
||||
|
||||
ch = bin(ord(ch))[2:]
|
||||
|
||||
while len(ch) < 8:
|
||||
ch = "0" + ch
|
||||
|
||||
message += str(ch)
|
||||
|
||||
fb = bin(int(n[1], 16))[2:]
|
||||
|
||||
while len(fb) < 8:
|
||||
fb = "0" + fb
|
||||
|
||||
sb = bin(int(n[2], 16))[2:]
|
||||
|
||||
while len(sb) < 8:
|
||||
sb = "0" + sb
|
||||
|
||||
message += fb + sb
|
||||
generator = "10001000000100001"
|
||||
gen_deg = len(generator)
|
||||
|
||||
while len(message) % 8 != 0:
|
||||
message = "0" + message
|
||||
|
||||
swapped = list(message)
|
||||
for i in range(gen_deg - 1):
|
||||
if swapped[i] == "0":
|
||||
swapped[i] = "1"
|
||||
else:
|
||||
swapped[i] = "0"
|
||||
|
||||
message = "".join(swapped)
|
||||
|
||||
while True:
|
||||
|
||||
if '1' not in message:
|
||||
return True
|
||||
|
||||
if message[0] == "0":
|
||||
message = message[1:]
|
||||
else:
|
||||
chunk = message[:gen_deg]
|
||||
|
||||
if len(chunk) < gen_deg:
|
||||
return False
|
||||
|
||||
remainder = format(int(chunk, 2) ^ int(generator, 2), "b")
|
||||
message = remainder + message[gen_deg:]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
if len(sys.argv) < 2:
|
||||
print("Proszę wprowadzić jedną z flag:\n")
|
||||
print(" -e, --encode\tZakoduj <wiadomość>")
|
||||
print(" -d, --decode\tDekoduj <wiadomość_z_ramką>\n")
|
||||
print("Przykład:")
|
||||
print("python main.py -e abc")
|
||||
print("python main.py -d abc\\\\x51\\\\x4a")
|
||||
else:
|
||||
if sys.argv[1] == "-e" or sys.argv[1] == "--encode":
|
||||
print(encode(sys.argv[2]))
|
||||
elif sys.argv[1] == "-d" or sys.argv[1] == "--decode":
|
||||
print(decode(sys.argv[2]))
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user