noise_table = [0,2,4,8,16,32,64,128]
import random
import binascii
from crc import crc
def read_binary_data(data_file_name):
with open(data_file_name, "rb") as binary_file:
binary_data = binary_file.read()
return binary_data
def write_binary_data(destination_file_name, binary_data, conf="wb"):
with open(destination_file_name, conf) as binary_file:
binary_file.write(binary_data)
def even_calc(binary_data):
result = sum([bin(byte).count("1") for byte in binary_data]) % 2
return result
def add_noise(binary_data):
binary_data_mut = bytearray(binary_data)
# for i in range(0, len(binary_data_mut)):
# binary_data_mut[i] = binary_data_mut[i] ^ random.choice(noise_table)
binary_data_mut[random.randint(0,len(binary_data_mut))] ^ random.choice(noise_table)
return binary_data_mut
def modulo_sum(bytes,m):
return sum(bytes)%m
def binary_to_string(binary_data):
s =""
for b in binary_data:
s+= bin(b).strip("0b")
return s
# original_file = "C:\\Users\\student\\Desktop\\putty.exe"
original_file = "test.txt"
send_file = "writtendata.txt"
binary_data = read_binary_data(original_file)
s = binary_to_string(binary_data)
even = str(even_calc(binary_data))
m_sum = str(modulo_sum(binary_data,100))
crc_result = crc(binary_to_string(binary_data),'010101010101110011100101010111010')
print(f'binary data: {binary_to_string(binary_data)}')
print(f'no of even bits %2: {even}')
print(f'modulo sum: {m_sum}')
print(f'crc result: {crc_result}')
write_binary_data(send_file, binary_data)
write_binary_data(send_file, even, "a")
binary_data_2 = read_binary_data(send_file)
binary_data_edit = add_noise(binary_data)
even_calc_edit = (even_calc(binary_data_edit))
even_edit = str(even_calc(binary_data_edit))
m_sum_edit = str(modulo_sum(binary_data,100))
crc_result_edit = crc(binary_to_string(binary_data_edit),'010101010101110011100101010111010')
print('\n\n')
print(f'binary data after noise: {binary_to_string(binary_data_edit)}')
print(f'no of even bits %2: {even_edit}')
print(f'modulo sum: {m_sum_edit}')
print(f'crct result: {crc_result_edit}')