def correct_bchsgb(testhex): pdf = decodefunctions.hextobin(testhex)[:204] bchsgb = (decodefunctions.hextobin(testhex))[204:] binary_data_pdf = newdata = pdf.zfill(204) correctedbch = bchsgb BCH_POLYNOMIAL = 285 BCH_BITS = 6 bitflips = 0 bch = bchlibcaronoff.BCH(BCH_BITS, m=8, prim_poly=BCH_POLYNOMIAL) max_data_len = bch.n // 8 - (bch.ecc_bits + 7) // 8 + 1 data = bytearray(bch1correct.bitstring_to_bytes(binary_data_pdf)) rebuildpdf = '' for e in range(len(data)): segment = decodefunctions.dec2bin(data[e]).zfill(8) rebuildpdf = rebuildpdf + segment rebuildpdf = rebuildpdf.zfill(204) ecc = bch.encode(data) bchstring = '' for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) bchstring = bchstring + binchar ecc_provided = bytearray(bch1correct.bitstring_to_bytes(bchsgb)) packet = data + ecc_provided if ecc != ecc_provided: data, ecc = packet[:-bch.ecc_bytes], packet[-bch.ecc_bytes:] bch.data_len = max_data_len nerr = bch.decode(data, ecc) bitflips = nerr bch.correct(data, ecc) newdata = decodefunctions.dec2bin(data[0]) for e in data[1:]: binchar = decodefunctions.dec2bin(e).zfill(8) newdata = newdata + binchar newdata = newdata.zfill(204) correctedbch = '' for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) correctedbch = correctedbch + binchar newhex = decodefunctions.bin2hex(newdata + correctedbch) else: bitflips = 0 newdata = pdf correctedbch = bchsgb newhex = decodefunctions.bin2hex(pdf + bchsgb) return (bitflips, newdata, correctedbch, newhex)
def make_full(hexinput): pdf1 = decodefunctions.hextobin(hexinput) b = pdf1[:204] + '0' * 48 print(b, len(b)) ecc = calcBCH(b, 0, 202, 250) newhex = decodefunctions.bin2hex(pdf1[:204] + ecc) print(newhex) print(ecc, decodefunctions.bin2hex(ecc)) BCH_POLYNOMIAL = 285 # 285 BCH_BITS = 6 bitflips = 0 bch = bchlib.BCH(BCH_POLYNOMIAL, BCH_BITS) max_data_len = bch.n // 8 - (bch.ecc_bits + 7) // 8 data = bytearray(bch1correct.bitstring_to_bytes(pdf1[:204])) necc = bch.encode(data) bchstring = '' for e in necc: binchar = decodefunctions.dec2bin(e).zfill(8) bchstring = bchstring + binchar print(bchstring, decodefunctions.bin2hex(bchstring)) newhex = decodefunctions.bin2hex(pdf1[:204] + bchstring) return newhex
def samplepdf2(f, r): for n in range(r): pdf2 = randombinary(26).zfill(26) bch2 = Fcn.calcbch('0' * 107 + pdf2, '1010100111001', 107, 133, 145) + '0000' bch = bchlib.BCH(67, 2) data = bytearray(bch1correct.bitstring_to_bytes(pdf2)) ecc = bch.encode(data) bchstring = (Fcn.dec2bin(ecc[0]).zfill(8) + Fcn.dec2bin(ecc[1]).zfill(8))[:12] + '0000' e = random.sample(range(0, 38), 3) scramble = list(pdf2 + bch2) for i in e: scramble[i] = str(int(not int(scramble[i]))) corrupt = ''.join(scramble) ecc_provided = bytearray(bch1correct.bitstring_to_bytes(corrupt[26:])) data_provided = bytearray(bch1correct.bitstring_to_bytes(corrupt[:26])) ecc_c = bch.encode(data_provided) print(ecc_c == ecc_provided, ecc, ecc_provided) packet = bytearray(bch1correct.bitstring_to_bytes( corrupt[:26])) + bytearray( bch1correct.bitstring_to_bytes(corrupt[26:])) data, ecc = packet[:-bch.ecc_bytes], packet[-bch.ecc_bytes:] bitflips = bch.decode_inplace(data, ecc) newdata = Fcn.dec2bin(data[0]).zfill(2) for e in data[1:]: binchar = Fcn.dec2bin(e).zfill(8) newdata = newdata + binchar correctedbch2 = '' # decodefunctions.dec2bin(ecc[0]) for e in ecc: binchar = Fcn.dec2bin(e).zfill(8) correctedbch2 = correctedbch2 + binchar correctedbch2 = correctedbch2[:-4] bch2 = bch2[:-4] bchstring = bchstring[:-4] f.writelines([ '{},{},{},{},{},{},{},{}'.format( pdf2 + bch2, bch2 == bchstring, corrupt, len(pdf2 + bch2), corrupt == pdf2 + bch2, newdata + correctedbch2, newdata + correctedbch2 == pdf2 + bch2, bitflips), '\n' ])
def pdf2_to_bch2(pdf2, bch2): binary_data_pdf2 = newdata = pdf2.zfill(26) correctedbch2 = bch2 bch2 = bch2 + '0000' BCH_POLYNOMIAL = 67 BCH_BITS = 2 bitflips = 0 #bch = bchlib.BCH(BCH_POLYNOMIAL, BCH_BITS) bch = bchlibcaronoff.BCH(2, prim_poly=67) max_data_len = bch.n // 8 - (bch.ecc_bits + 7) // 8 data = bytearray(bch1correct.bitstring_to_bytes(binary_data_pdf2)) rebuildpdf2 = '' for e in range(len(data)): segment = decodefunctions.dec2bin(data[e]).zfill(8) rebuildpdf2 = rebuildpdf2 + segment ecc = bch.encode(data) # create array of ecc provide by bch ecc_provided = bytearray(bch1correct.bitstring_to_bytes(bch2)) packet = data + ecc_provided # # if ecc != ecc_provided: data, ecc = packet[:-bch.ecc_bytes], packet[-bch.ecc_bytes:] # #correct nerr = bch.decode(data, ecc) bitflips = nerr #bitflips = bch.decode_inplace(data,ecc) bch.correct(data, ecc) packet = data + ecc newdata = decodefunctions.dec2bin(data[0]) #.zfill(2) for e in data[1:]: binchar = decodefunctions.dec2bin(e).zfill(8) newdata = newdata + binchar # correctedbch2 = '' #decodefunctions.dec2bin(ecc[0]) for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) correctedbch2 = correctedbch2 + binchar return (bitflips, newdata.zfill(26), correctedbch2[:-4])
def pdf2_to_bch2(pdf2, bch2): #valid pdf1 binary_data_pdf2 = newdata = pdf2 correctedbch2 = bch2 bch2 = bch2 + '0000' #print('valid pdf1',len(binary_data_pdf1),binary_data_pdf1) BCH_POLYNOMIAL = 67 #137 BCH_BITS = 2 bitflips = 0 bch = bchlib.BCH(BCH_POLYNOMIAL, BCH_BITS) data = bytearray(bch1correct.bitstring_to_bytes(binary_data_pdf2)) rebuildpdf2 = '' for e in range(len(data)): segment = decodefunctions.dec2bin(data[e]).zfill(8) rebuildpdf2 = rebuildpdf2 + segment #print(e, data[e],segment) #print(binary_data_pdf2) #print(rebuildpdf2,len(rebuildpdf2),binary_data_pdf2==rebuildpdf2) ecc = bch.encode(data) #print('computed ecc bytes:',len(ecc)) bchstring = '' for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) #print(e, binchar) bchstring = bchstring + binchar #print(bchstring) #print(bch2) # create array of ecc provide by bch ecc_provided = bytearray(bch1correct.bitstring_to_bytes(bch2)) packet = data + ecc_provided bchstr2 = '' #print(len(data),len(ecc),len(packet)) #print('ecc included:',ecc_provided,len(ecc_provided),type(ecc_provided)) #print('ecc calc:', ecc,len(ecc),type(ecc)) # # # if ecc != ecc_provided: data, ecc = packet[:-bch.ecc_bytes], packet[-bch.ecc_bytes:] # #correct bitflips = bch.decode_inplace(data, ecc) #print('bitflips: %d' % (bitflips)) newdata = decodefunctions.dec2bin(data[0]) for e in data[1:]: binchar = decodefunctions.dec2bin(e).zfill(8) # #print(e, binchar) newdata = newdata + binchar # correctedbch2 = '' #decodefunctions.dec2bin(ecc[0]) for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) # #print(e, binchar) correctedbch2 = correctedbch2 + binchar if (len(correctedbch2)) > 12: correctedbch2 = correctedbch2[:12] return (bitflips, newdata.zfill(26), correctedbch2)
def msgbits_to_bch(msgbits,bchsgb): print(len(msgbits)) binary_data_msgbits = newdata = msgbits.zfill(207) #.zfill(204) #Msg size is 202 correctedbch=bchsgb BCH_POLYNOMIAL = 463 BCH_BITS = 6 bitflips=0 #109,113,127,131,137,139,149,151,157,163,167,173,179, 181, 191, 193, 197, 199, 211, 223, 227, 229 #233, 239 , 241, 251, 257, 263, 269, 271, 277, 281 for p in [229,233,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389, 397,401, 409, 419, 421, 431,433,439,443,449,457, 461,463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541,547,557,563,569,571,577,587,594,599,601,607]: # 233, 239 , 241, 251, 257, 263, 269, 271, 277, 281, # 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, # 353, 359, 367, 373 , 379, 383 , 389, 397 , 401, 409, # 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, # 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, # 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, # 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, # 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, # 739, 743, 751, 757, 761, 769, 773, 787, 797, 809]: try: bch = bchlib.BCH(p, BCH_BITS) data = bytearray(bch1correct.bitstring_to_bytes(binary_data_msgbits)) #data = data[1:] rebuildmsg='' for e in range(len(data)): segment=Fcn.dec2bin(data[e]).zfill(8) rebuildmsg=rebuildmsg+segment #print(e, data[e],segment) #print(binary_data_msgbits,len(binary_data_msgbits)) #print(rebuildmsg,len(rebuildmsg),binary_data_msgbits==rebuildmsg) ecc = bch.encode(data) print('computed ecc bytes:',len(ecc)) ecc_provided = bytearray(bch1correct.bitstring_to_bytes(bchsgb)) bchstring = ecc_provided[0] print(ecc) print(ecc_provided) for e in ecc_provided: binchar = Fcn.dec2bin(e).zfill(8) print(e, int(e), binchar) #bchstring = bchstring + binchar for e in ecc: #binchar = Fcn.dec2bin(e).zfill(8) print(e, int(e) , binchar) #bchstring = bchstring + binchar #print(bchstring) #print(bchsgb) print(p) except RuntimeError: pass # create array of ecc provide by bch ecc_provided = bytearray(bch1correct.bitstring_to_bytes(bchsgb)) packet=data + ecc_provided print(len(data),len(ecc),len(packet)) print('ecc included:',ecc_provided,len(ecc_provided),type(ecc_provided)) print('ecc calc:', ecc,len(ecc),type(ecc)) # # # if ecc!=ecc_provided: data, ecc = packet[:-bch.ecc_bytes], packet[-bch.ecc_bytes:] # #correct bitflips = bch.decode_inplace(data,ecc) print('bitflips: %d' % (bitflips)) newdata=Fcn.dec2bin(data[0]) for e in data[1:]: binchar = Fcn.dec2bin(e).zfill(8) # #print(e, binchar) newdata = newdata + binchar # correctedbch = '' #decodefunctions.dec2bin(ecc[0]) for e in ecc: binchar = Fcn.dec2bin(e).zfill(8) # #print(e, binchar) correctedbch = correctedbch + binchar return (bitflips,newdata,correctedbch)
def pdf_to_bchsgb(pdf, bchsgb): binary_data_pdf = newdata = pdf.zfill(204) print(len(binary_data_pdf)) correctedbch = bchsgb bchsgb = bchsgb BCH_POLYNOMIAL = 285 #285 BCH_BITS = 6 bitflips = 0 bch = bchlib.BCH(BCH_POLYNOMIAL, BCH_BITS) max_data_len = bch.n // 8 - (bch.ecc_bits + 7) // 8 data = bytearray(bch1correct.bitstring_to_bytes(binary_data_pdf)) print('len in bytes of data', len(data)) print('m------------:', bch.m) print('ecc_bits: {} ecc_bytes: {} '.format(bch.ecc_bits, bch.ecc_bytes)) print('m:', bch.m) print('n: {} bytes: {}'.format(bch.n, bch.n // 8)) print('max data length:', max_data_len) print('t: {}'.format(bch.t)) print('length of data in bytes:', len(data)) rebuildpdf = '' print('---Per Byte' + '-' * 40) for e in range(len(data)): segment = decodefunctions.dec2bin(data[e]).zfill(8) rebuildpdf = rebuildpdf + segment print(e, data[e], segment) print('-' * 50) print('pdf as input', len(pdf), pdf) print('bch as input', len(bchsgb), bchsgb) print('-' * 50) rebuildpdf = rebuildpdf.zfill(202) print(rebuildpdf, len(rebuildpdf)) print(binary_data_pdf == rebuildpdf) print('inputed data len to encode bch: ', len(data)) ecc = bch.encode(data) print('computed ecc bytes:', len(ecc)) bchstring = '' for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) print(e, binchar) bchstring = bchstring + binchar print(bchsgb) print(bchstring) print(bchsgb == bchstring) print(decodefunctions.bin2hex(rebuildpdf + bchstring)) # create array of ecc provide by bch ecc_provided = bytearray(bch1correct.bitstring_to_bytes(bchsgb)) print(ecc) print('ecc included:', ecc_provided, len(ecc_provided), type(ecc_provided)) print('ecc calc:', ecc, len(ecc), type(ecc)) print('provided', bchsgb, len(bchsgb), decodefunctions.bin2hex(bchsgb)) print('calculated', bchstring, len(bchstring), decodefunctions.bin2hex(bchstring)) packet = data + ecc_provided print('ecc equiv', ecc == ecc_provided) if ecc != ecc_provided: data, ecc = packet[1:-bch.ecc_bytes], packet[-bch.ecc_bytes:] print('cropped len data', len(data)) #data, ecc = packet[:-6], packet[-6:] print(data, ecc) bitflips = bch.decode_inplace(data, ecc) print('bitflips: %d' % (bitflips)) newdata = pdf[:4] #decodefunctions.dec2bin(byte_one) for e in data: binchar = decodefunctions.dec2bin(e).zfill(8) newdata = newdata + binchar # correctedbch = '' for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) correctedbch = correctedbch + binchar return (bitflips, newdata.zfill(204), correctedbch[:48])