Exemple #1
0
    def decrypt(self, data: int) -> bytes:
        data = Conversion.uint32_to_uint16_reversed_array(data)
        data = bytes(Conversion.uint16_array_to_uint8_array(data))

        blocksize = self.blocksize
        key = self.key
        rounds = self.rounds

        w = blocksize // 2
        b = blocksize // 8

        expanded_key = _expand_key(key, w, rounds)

        index = b
        chunk = data[:index]
        out = []
        while chunk:
            decrypted_chunk = _decrypt_block(chunk, expanded_key, blocksize, rounds)
            chunk = data[index : index + b]  # Read in blocksize number of bytes
            if not chunk:
                decrypted_chunk = decrypted_chunk.rstrip(b"\x00")

            index += b
            out.append(decrypted_chunk)
        result = b"".join(out)

        result = Conversion.uint8_array_to_uint16_array(result)
        result = Conversion.uint16_reversed_array_to_uint32(result)

        return result
Exemple #2
0
    def encrypt(self, data: int) -> int:
        # alteration to accept numbers instead of bytes
        data = Conversion.uint32_to_uint16_reversed_array(data)
        data = bytes(Conversion.uint16_array_to_uint8_array(data))

        blocksize = self.blocksize
        key = self.key
        rounds = self.rounds

        w = blocksize // 2
        b = blocksize // 8

        expanded_key = _expand_key(key, w, rounds)

        index = b
        chunk = data[:index]
        out = []
        while chunk:
            chunk = chunk.ljust(b, b"\x00")  # padding with 0 bytes if not large enough
            encrypted_chunk = _encrypt_block(chunk, expanded_key, blocksize, rounds)
            out.append(encrypted_chunk)

            chunk = data[index : index + b]  # Read in blocksize number of bytes
            index += b
        result = b"".join(out)

        result = Conversion.uint8_array_to_uint16_array(result)
        result = Conversion.uint16_reversed_array_to_uint32(result)

        return result