Beispiel #1
0
    def _validate_key(key_ints: List[int]) -> None:
        """Validate the key."""
        if len(key_ints) == 0:
            raise EmptyKeyError()

        for num in key_ints:
            char_int_allowed(num)
Beispiel #2
0
    def decrypt(self, cipher: TextIO, key: str, text: TextIO) -> None:
        """Decrypt the cipher using the key."""
        super().decrypt(cipher, key, text)

        while True:
            cipher_hex = cipher.read(HEX_LENGTH)
            if cipher_hex == '':
                break

            cipher_int = hex_to_int(cipher_hex)
            char_int_allowed(cipher_int)

            text_int = self._process(cipher_int)

            text.write(chr(text_int))
Beispiel #3
0
    def encrypt(self, text: TextIO, key: str, cipher: TextIO) -> None:
        """Encrypt the text using the key."""
        super().encrypt(text, key, cipher)

        while True:
            text_char = text.read(1)
            if text_char == '':
                break

            text_int = ord(text_char)
            char_int_allowed(text_int)

            cipher_int = self._process(text_int)

            cipher.write(int_to_hex(cipher_int))
Beispiel #4
0
    def encrypt(self, text: TextIO, key_filename: str, cipher: TextIO) -> None:
        """Encrypt the text and save generated key to the file."""
        with open(key_filename, 'w') as key_file:
            while True:
                text_char = text.read(1)
                if text_char == '':
                    break

                text_int = ord(text_char)
                char_int_allowed(text_int)

                key_int = next(self.__key_int_gen)

                cipher_int = text_int ^ key_int

                key_file.write(int_to_hex(key_int))
                cipher.write(int_to_hex(cipher_int))
Beispiel #5
0
    def decrypt(self, cipher: TextIO, key_filename: str, text: TextIO) -> None:
        """Decrypt the cipher using the key from the file."""
        with open(key_filename, 'r') as key_file:
            while True:
                cipher_hex = cipher.read(HEX_LENGTH)
                key_hex = key_file.read(HEX_LENGTH)
                if cipher_hex == '' or key_hex == '':
                    break

                cipher_int = hex_to_int(cipher_hex)
                char_int_allowed(cipher_int)

                key_int = hex_to_int(key_hex)
                char_int_allowed(key_int)

                text_int = cipher_int ^ key_int

                text.write(chr(text_int))
Beispiel #6
0
 def _validate_input(input_ints: List[int]) -> None:
     """Validate the input."""
     for num in input_ints:
         char_int_allowed(num)