Пример #1
0
    def encrypt_single_val(self, plaintext):
        """

        :param plaintext: int >= 0 / str / PohligHellmanCiphertext
        :return: PohligHellmanCiphertext
        """
        if isinstance(plaintext, str):
            plaintext = conversion.str_to_int(plaintext)
        elif isinstance(plaintext, PohligHellmanCiphertext):
            plaintext = plaintext.message
        elif not isinstance(plaintext, int):
            plaintext = conversion.str_to_int(str(plaintext))

        ciphertext = powmod(plaintext, self.exponent, self.mod_base)
        return PohligHellmanCiphertext(ciphertext)
Пример #2
0
    def encrypt(self, plaintext):
        """

        :param plaintext: int >= 0 / str / PohligHellmanCiphertext
        :return: PohligHellmanCiphertext
        """
        if type(plaintext) == str:
            plaintext = conversion.str_to_int(plaintext)
        elif type(plaintext) == PohligHellmanCiphertext:
            plaintext = plaintext.message

        ciphertext = powmod(plaintext, self.exponent, self.mod_base)
        return PohligHellmanCiphertext(ciphertext)
Пример #3
0
    def decrypt_single_val(self, ciphertext, decode_output=False):
        """
        If decode, then call int_to_str() method to decode the output plaintext
        :param ciphertext: PohligHellmanCiphertext
        :param decode_output: bool
        :return: PohligHellmanCiphertext / str
        """
        if isinstance(ciphertext, PohligHellmanCiphertext):
            ciphertext = ciphertext.message
        elif isinstance(ciphertext, str):
            ciphertext = conversion.str_to_int(ciphertext)

        if decode_output:
            return conversion.int_to_str(powmod(ciphertext, self.exponent_inverse, self.mod_base))
        else:
            return PohligHellmanCiphertext(powmod(ciphertext, self.exponent_inverse, self.mod_base))
Пример #4
0
 def record_original_id(k, v):
     if isinstance(k, str):
         restored_id = conversion.int_to_str(conversion.str_to_int(k))
     else:
         restored_id = k
     return (restored_id, k)