def preprocess(self):
        # Preprocess text
        text = self.text.lower()
        text = general.sanitize(text)
        self.text = [(ord(i) - const.LETTER) for i in text]

        # Preprocess key
        key = self.key.lower()
        key = general.sanitize(key)
        self.key = [(ord(i) - const.LETTER) for i in key]
示例#2
0
    def preprocess(self):
        # Preprocess text
        text = self.text.lower()
        text = general.sanitize(text)
        text = general.replace_char(text, 'j', 'i')
        self.bigrams = create_bigram(text)

        # Preprocess key
        key = self.key.lower()
        key = general.sanitize(key)
        key += const.ALPHABET
        key = general.remove_duplicate_char(key)
        key = general.remove_char(key, 'j')
        self.key = general.create_square(key, 5)
 def preprocess(self):
     # Preprocess text
     # Done in encrypt and decrypt function for special purpose
             
     # Preprocess key
     key = self.key.lower()
     key = general.sanitize(key)
     self.key = [(ord(i)-const.LETTER) for i in key]
示例#4
0
    def preprocess(self):
        # preprocess key
        try:
            key = json.loads(self.key)['key']
        except KeyError as err:
            raise Exception(f'required key {err}', 400)
        except Exception as err:
            raise Exception(err.args, 400)

        key_length = len(key)
        if not (is_square(key_length)):
            raise Exception("length of key is not perfect square", 400)

        key_sqrt = sqrt(key_length)
        self.key = general.create_square(key, key_sqrt)

        # preprocess text
        text = self.text.lower()
        text = general.sanitize(text)
        self.ngrams = create_n_gram(self.text, key_sqrt)
    def encrypt(self):
        chiper_text = ''
        
        #Preprocess text
        text = self.text.lower()
        text = general.sanitize(text)
        self.text = [(ord(i)-const.LETTER) for i in text]

        #Encrypt phase
        vig = Vigenere(self.text,self.key).encrypt()

        ord_vig = [(ord(i)-const.LETTER) for i in vig]
        matrices = split_len(ord_vig,const.TRANSPOSEKEY)
        transpose = np.transpose(matrices)

        for row in transpose:
            for col in row:
                if(col != const.SENTINEL):                
                    chiper_text += general.order_to_char(col)
                else:
                    chiper_text += chr(col)

        return chiper_text
示例#6
0
    def preprocess(self):
        # Preprocess text
        text = self.text.lower()
        text = general.sanitize(text)
        self.text = text

        # Preprocess key
        try:
            key = json.loads(self.key)
        except:
            raise Exception("can't decode json key", 400)

        try:
            self.m = int(key['m'])
            self.b = int(key['b'])
        except ValueError as err:
            raise Exception("key is not integer", 400)
        except KeyError as err:
            raise Exception(f'required key {err}', 400)

        self.n = 26
        if math.gcd(self.m, self.n) != 1:
            raise Exception(f"{self.m} and {self.n} are not relatively prime",
                            400)