Created
November 21, 2018 12:32
-
-
Save Samir55/18d26a3a4f98e0b8bb9320a9415c0432 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ast | |
def encode(input_file, output_file): | |
payloads = [x.rstrip() for x in open(input_file)] | |
# Append char count | |
payloads_with_char_count = [] | |
for pl in payloads: | |
payload_with_char_count = str(len(pl) + 2) + pl | |
payloads_with_char_count.append(payload_with_char_count) | |
# Convert to binary | |
payloads_bin = [] | |
for pl in payloads_with_char_count: | |
payloads_bin.append(ascii_to_binary(pl)) | |
# Calculate parity | |
final_payloads = [] | |
for pl in payloads_bin: | |
pl.append(calc_even_parity(pl)) | |
final_payloads.append(pl) | |
print(final_payloads) | |
with open(output_file, "w") as file: | |
for i in final_payloads: | |
st = "" | |
for j in i: | |
st += ("[\'" + j + "\'], ") | |
st = '[' + st[:-2] + ']' | |
file.write(st + '\n') | |
def calc_even_parity(chars_list): | |
res = int('0b' + chars_list[0], 2) | |
i = 0 | |
# apply xoring | |
for c in chars_list: | |
i += 1 | |
if i == 1: | |
continue | |
else: | |
res ^= int('0b' + c, 2) | |
bin_str = bin(res)[2:] | |
bin_str = '0' * (8 - len(bin_str)) + bin_str | |
return bin_str | |
def ascii_to_binary(str): | |
bin_list = [] | |
for c in str: | |
bin_str = bin(int.from_bytes(c.encode(), 'big'))[2:] | |
bin_str = '0' * (8 - len(bin_str)) + bin_str | |
bin_list.append(bin_str) | |
return bin_list | |
def decode(input_file, output_file): | |
# Decoding. | |
file = open(output_file, "w") | |
for line in open(input_file): | |
# Get the frame | |
pay_load = ast.literal_eval(line) | |
# Get the number of characters as ascii int and in absolute. | |
n = int('0b' + pay_load[0][0], 2) | |
count = n - ord('0') | |
# Validate the number of bytes is the same. | |
if (count is not len(pay_load)): | |
print("ERROR, BYTE COUNT MISMATCH") | |
continue | |
# Get the parity. | |
parity = 0 | |
pay_load_bin = [] | |
for i in range(0 , count - 1): | |
# print (int('0b' + pay_load[i][0], 2), pay_load[i], i) | |
parity = parity ^ int('0b' + pay_load[i][0], 2) | |
# print(parity) | |
pay_load_bin.append(int('0b' + pay_load[i][0], 2)) | |
if int ('0b' + pay_load[count - 1][0], 2) is not parity: | |
print ("ERROR, PARITY MISMATCH") | |
file.write("ERROR, PARITY MISMATCH") | |
else : | |
pay_load_string = '' | |
for i in range(1, len(pay_load_bin)): | |
pay_load_string += pay_load_bin[i].to_bytes((n.bit_length() + 7) // 8, 'big').decode() | |
file.write(pay_load_string + '\n') | |
print (pay_load_string) | |
file.close() | |
encode("payloads.txt", "encode_outputfile.txt") | |
decode("encode_outputfile.txt", "decode_outputfile.txt") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment