Ejemplo n.º 1
0
    def run(self):

        while self.is_running:

            if self.num == 0:  # emission

                data = str(
                    raw_input("\n(chat) > "))  # sanitize function needed
                # will enter in conflict with ANANSI raw_input ?

                if data == "EXIT":
                    self.is_running = False
                    print "\n(chat) > BYE"
                else:
                    data = shamirlib.send_message(data, SHAMIR_K, SHAMIR_N,
                                                  SHAMIR_MLR, SHAMIR_GFIELD)

                    for x in data:
                        for y in x:
                            self.craftbuffer.put(y)

            if self.num == 1:  # reception

                if len(
                        self.shamirbuffer
                ) == self.shamirsize:  # if enough bits for a shamir message
                    re = shamirlib.receive_element(
                        map(str, self.shamirbuffer), SHAMIR_MLR,
                        SHAMIR_N)  # extract letter_ref, x and y from data
                    self.shamir_rtable[re[0]][re[1]] = re[
                        2]  # assigning value y to point x for letter letter_ref

                    if re[0] == self.shamir_lastref:  # still the same letter
                        if len(
                                self.shamir_rtable[re[0]]
                        ) > SHAMIR_K:  # do we have enough to try to reconstruct the original data ?
                            temp = shamirlib.reconstruction(rtable[re[0]])
                            tvalue = shamirlib.modulo_fractions(
                                temp[0], temp[1], SHAMIR_GFIELD)

                            if tvalue == self.shamir_endingchar:  # this is the end, my only friend, the end
                                msg = [
                                    x for x in self.shamirmessage
                                    if x != self.shamir_endingchar
                                ]  # there should be no endingchar in shamirmessage, paranoid much ?
                                if len(msg) > 0:
                                    print "\nMSG : " + shamirlib.post_receiving_routine(
                                        msg, SHAMIR_GFIELD,
                                        self.shamir_defaultval)

                                # re-init (optimisation ?)
                                self.shamirbuffer = []
                                self.shamir_rtable = {
                                    k: {}
                                    for k in range(SHAMIR_MLR)
                                }
                                self.shamir_lastref = 0
                                self.shamirmessage = []
                                self.shamir_tvalue = SHAMIR_GFIELD

                            else:
                                if tvalue != self.shamir_tvalue:
                                    self.shamir_tvalue = tvalue  # think of a better correction algorithm using previous values/K in N combinations

                    else:  # oh my, we are looking at a new letter !
                        self.shamirmessage += [
                            self.shamir_tvalue
                        ]  # we add the reconstructed value to the message
                        self.shamir_rtable[self.shamir_lastref] = {}
                        self.shamir_tvalue = SHAMIR_GFIELD
                        self.shamir_lastref = re[0]

                    self.shamirbuffer = [
                    ]  # empty the buffer after using the shamirsize bits

                else:  # get bits from craftbuffer to shamirbuffer
                    if not self.craftbuffer.empty():
                        self.shamirbuffer.append(self.craftbuffer.get())
        return
Ejemplo n.º 2
0
    def run(self):

        while self.is_running:
            
            if self.num == 0: # emission
                
                data = str(raw_input("\n(chat) > ")) # sanitize function needed
                # will enter in conflict with ANANSI raw_input ?
                
                if data == "EXIT":
                    self.is_running = False
                    print "\n(chat) > BYE"
                else:
                    data = shamirlib.send_message(data,
                                                  SHAMIR_K,
                                                  SHAMIR_N,
                                                  SHAMIR_MLR,
                                                  SHAMIR_GFIELD)
                    
                    for x in data:
                        for y in x:
                            self.craftbuffer.put(y)
                            
            if self.num == 1: # reception
                
                if len(self.shamirbuffer) == self.shamirsize: # if enough bits for a shamir message
                    re = shamirlib.receive_element(map(str,self.shamirbuffer),SHAMIR_MLR,SHAMIR_N) # extract letter_ref, x and y from data
                    self.shamir_rtable[re[0]][re[1]] = re[2] # assigning value y to point x for letter letter_ref

                    if re[0] == self.shamir_lastref: # still the same letter
                        if len(self.shamir_rtable[re[0]])>SHAMIR_K: # do we have enough to try to reconstruct the original data ?
                            temp = shamirlib.reconstruction(rtable[re[0]])
                            tvalue = shamirlib.modulo_fractions(temp[0],temp[1],SHAMIR_GFIELD)
                            
                            if tvalue == self.shamir_endingchar: # this is the end, my only friend, the end
                                msg = [x for x in self.shamirmessage if x != self.shamir_endingchar] # there should be no endingchar in shamirmessage, paranoid much ?
                                if len(msg)>0:
                                    print "\nMSG : "+shamirlib.post_receiving_routine(msg,SHAMIR_GFIELD,self.shamir_defaultval)

                                # re-init (optimisation ?)
                                self.shamirbuffer = []
                                self.shamir_rtable = {k:{} for k in range(SHAMIR_MLR)}
                                self.shamir_lastref = 0
                                self.shamirmessage = []
                                self.shamir_tvalue = SHAMIR_GFIELD
                                
                            else:
                                if tvalue != self.shamir_tvalue:
                                    self.shamir_tvalue = tvalue # think of a better correction algorithm using previous values/K in N combinations
                                    
                    else: # oh my, we are looking at a new letter !
                        self.shamirmessage += [self.shamir_tvalue] # we add the reconstructed value to the message
                        self.shamir_rtable[self.shamir_lastref] = {}
                        self.shamir_tvalue = SHAMIR_GFIELD
                        self.shamir_lastref = re[0]

                    self.shamirbuffer = [] # empty the buffer after using the shamirsize bits
                    
                else: # get bits from craftbuffer to shamirbuffer
                    if not self.craftbuffer.empty():
                        self.shamirbuffer.append(self.craftbuffer.get())
        return
Ejemplo n.º 3
0
    def run(self):
        while self.is_running:

            if self.num == 0:  # emission
                filepath = os.path.join(
                    os.path.dirname(
                        os.path.abspath(inspect.getfile(
                            inspect.currentframe()))), "files", SRC_FILEPATH,
                    self.filename)

                with open(filepath) as f:
                    data = f.read()
                f.close()

                bdata = list()
                if SHAMIR_ON:
                    bdata = shamirlib.send_message(data, SHAMIR_K, SHAMIR_N,
                                                   SHAMIR_MAX_LETTER_REF,
                                                   SHAMIR_GFIELD)
                else:
                    bdata = [list(bin(ord(t))[2:].zfill(8)) for t in data]

                for x in data:
                    for y in x:
                        self.craftbuffer.put(y)

            if self.num == 1:  # reception
                filepath = os.path.join(
                    os.path.dirname(
                        os.path.abspath(inspect.getfile(
                            inspect.currentframe()))), "files", DST_FILEPATH,
                    self.filename)
                newdata = ""

                if SHAMIR_ON:
                    if len(
                            self.shamirbuffer
                    ) == self.shamirsize:  # if enough bits for a shamir message
                        re = shamirlib.receive_element(
                            map(str, self.shamirbuffer), SHAMIR_MLR,
                            SHAMIR_N)  # extract letter_ref, x and y from data
                        self.shamir_rtable[re[0]][re[1]] = re[
                            2]  # assigning value y to point x for letter letter_ref

                        if re[0] == self.shamir_lastref:  # still the same letter
                            if len(
                                    self.shamir_rtable[re[0]]
                            ) > SHAMIR_K:  # do we have enough to try to reconstruct the original data ?
                                temp = shamirlib.reconstruction(rtable[re[0]])
                                tvalue = shamirlib.modulo_fractions(
                                    temp[0], temp[1], SHAMIR_GFIELD)

                                if tvalue == self.shamir_endingchar:  # this is the end, my only friend, the end
                                    msg = [
                                        x for x in self.shamirmessage
                                        if x != self.shamir_endingchar
                                    ]  # there should be no endingchar in shamirmessage, paranoid much ?
                                    if len(msg) > 0:
                                        newdata = shamirlib.post_receiving_routine(
                                            msg, SHAMIR_GFIELD,
                                            self.shamir_defaultval)

                                # re-init (optimisation ?)
                                    self.shamirbuffer = []
                                    self.shamir_rtable = {
                                        k: {}
                                        for k in range(SHAMIR_MLR)
                                    }
                                    self.shamir_lastref = 0
                                    self.shamirmessage = []
                                    self.shamir_tvalue = SHAMIR_GFIELD

                                else:
                                    if tvalue != self.shamir_tvalue:
                                        self.shamir_tvalue = tvalue  # think of a better correction algorithm using previous values/K in N combinations

                        else:  # oh my, we are looking at a new letter !
                            self.shamirmessage += [
                                self.shamir_tvalue
                            ]  # we add the reconstructed value to the message
                            self.shamir_rtable[self.shamir_lastref] = {}
                            self.shamir_tvalue = SHAMIR_GFIELD
                            self.shamir_lastref = re[0]

                        self.shamirbuffer = [
                        ]  # empty the buffer after using the shamirsize bits

                    else:  # get bits from craftbuffer to shamirbuffer
                        if not self.craftbuffer.empty():
                            self.shamirbuffer.append(self.craftbuffer.get())
                else:
                    while not self.craftbuffer.empty():
                        tdata = ""
                        tdata += str(self.craftbuffer.get())
                        tdata = tdata + '0' * (8 - (len(newdata) % 8))
                        for k in range(len(tdata) / 8):
                            newdata += chr(int(tdata[k * 8:(k + 1) * 8], 2))

                with open(filepath, 'w') as f:
                    f.write(newdata)
                f.close()

        return
Ejemplo n.º 4
0
    def run(self):
        while self.is_running:
            
            if self.num == 0: # emission
                filepath = os.path.join(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))),"files",SRC_FILEPATH,self.filename)

                with open(filepath) as f:
                    data = f.read()
                f.close()
                    
                bdata = list()
                if SHAMIR_ON:
                    bdata = shamirlib.send_message(data,SHAMIR_K,SHAMIR_N,SHAMIR_MAX_LETTER_REF,SHAMIR_GFIELD)
                else:
                    bdata = [list(bin(ord(t))[2:].zfill(8)) for t in data]

                for x in data:
                        for y in x:
                            self.craftbuffer.put(y)
                            
            if self.num == 1: # reception
                filepath = os.path.join(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))),"files",DST_FILEPATH,self.filename)
                newdata = ""

                if SHAMIR_ON:
                    if len(self.shamirbuffer) == self.shamirsize: # if enough bits for a shamir message
                        re = shamirlib.receive_element(map(str,self.shamirbuffer),SHAMIR_MLR,SHAMIR_N) # extract letter_ref, x and y from data
                        self.shamir_rtable[re[0]][re[1]] = re[2] # assigning value y to point x for letter letter_ref

                        if re[0] == self.shamir_lastref: # still the same letter
                            if len(self.shamir_rtable[re[0]])>SHAMIR_K: # do we have enough to try to reconstruct the original data ?
                                temp = shamirlib.reconstruction(rtable[re[0]])
                                tvalue = shamirlib.modulo_fractions(temp[0],temp[1],SHAMIR_GFIELD)
                            
                                if tvalue == self.shamir_endingchar: # this is the end, my only friend, the end
                                    msg = [x for x in self.shamirmessage if x != self.shamir_endingchar] # there should be no endingchar in shamirmessage, paranoid much ?
                                    if len(msg)>0:
                                        newdata = shamirlib.post_receiving_routine(msg,SHAMIR_GFIELD,self.shamir_defaultval)

                                # re-init (optimisation ?)
                                    self.shamirbuffer = []
                                    self.shamir_rtable = {k:{} for k in range(SHAMIR_MLR)}
                                    self.shamir_lastref = 0
                                    self.shamirmessage = []
                                    self.shamir_tvalue = SHAMIR_GFIELD
                                
                                else:
                                    if tvalue != self.shamir_tvalue:
                                        self.shamir_tvalue = tvalue # think of a better correction algorithm using previous values/K in N combinations
                                    
                        else: # oh my, we are looking at a new letter !
                            self.shamirmessage += [self.shamir_tvalue] # we add the reconstructed value to the message
                            self.shamir_rtable[self.shamir_lastref] = {}
                            self.shamir_tvalue = SHAMIR_GFIELD
                            self.shamir_lastref = re[0]

                        self.shamirbuffer = [] # empty the buffer after using the shamirsize bits
                    
                    else: # get bits from craftbuffer to shamirbuffer
                        if not self.craftbuffer.empty():
                            self.shamirbuffer.append(self.craftbuffer.get())
                else:
                    while not self.craftbuffer.empty():
                        tdata = ""
                        tdata += str(self.craftbuffer.get())
                        tdata = tdata+'0'*(8-(len(newdata)%8))
                        for k in range(len(tdata)/8):
                            newdata += chr(int(tdata[k*8:(k+1)*8],2))

                with open(filepath,'w') as f:
                    f.write(newdata)
                f.close()
                
        return