Change user input and fix encoding issue

This commit is contained in:
Varmen8 2018-06-26 01:27:40 +02:00
parent bfaefd1740
commit f807d323a0

71
main.py
View File

@ -1,5 +1,9 @@
import sys
def encode(m):
message = format(int.from_bytes(m.encode("latin-1"), "big"), "b")
m = bytearray(m, "ascii")
message = format(int.from_bytes(m, "big"), "b")
generator = "10001000000100001"
gen_deg = len(generator)
@ -28,14 +32,51 @@ def encode(m):
while len(chunk) % 8 != 0:
chunk = "0" + chunk
return m + chr(int(chunk[:8], 2)) + chr(int(chunk[8:], 2))
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):
message = format(int.from_bytes(n.encode("latin-1"), "big"), "b")
fb = n[-7:-5]
sb = n[-2:]
msg = n[:-10]
message = ""
for ch in msg:
ch = bin(ord(ch))[2:]
while len(ch) < 8:
ch = "0" + ch
message += str(ch)
fb = bin(int(fb, 16))[2:]
while len(fb) < 8:
fb = "0" + fb
sb = bin(int(sb, 16))[2:]
while len(sb) < 8:
sb = "0" + sb
message += fb + sb
generator = "10001000000100001"
gen_deg = len(generator)
@ -69,17 +110,19 @@ def decode(n):
if __name__ == "__main__":
choice = ""
while choice != "3":
choice = input("1. Utwórz FCS\n2. Sprawdź FCS\n3. Wyjdź\n")
if choice == "1":
inp = input("Wprowadź wiadomość:\n")
print(encode(inp))
elif choice == "2":
inp = input("Wprowadź wiadomość z ramką:\n")
print(decode(inp))
if len(sys.argv) < 2:
print("Proszę wprowadzić jedną z flag:\n")
print(" -e, --encode\tZakoduj <wiadomość>")
print(" -d, --decode\tDekoduj <wiadomość_z_ramką>")
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]))