Пример #1
0
    def test_not_authentic_image(self):
        from dhdicom.hidding.mixed import EPRHindingAndAuthentication
        import pydicom

        filename = os.path.join(self.base, 'images/2.dcm')
        image = DicomImage(filename)
        ds = pydicom.read_file(filename)

        dimesions = ds.Rows, ds.Columns

        registers = ['PatientName', 'PatientID']
        recipe = os.path.join(self.base, 'recipes/confidential')
        epr = EPRData(registers, recipe)
        hider = EPRHindingAndAuthentication('nuevaclave')

        handler = DHDicomHandler(data_handler=epr, hider_handler=hider)
        new_image = handler.process(image)

        # Image Tampering
        x = dimesions[0] - 1
        y = dimesions[1] - 1
        pixels = new_image.read()
        pixels[0][0] += 1
        pixels[x][y] += 1
        new_image.write(pixels)

        # Despues del procesamiento los pixeles se modificaron
        authentic, blocks_tampered = handler.authenticate(new_image)
        self.assertFalse(authentic)
        # image size: 255x255, block size:32x32, block num: 255
        np.testing.assert_array_equal(blocks_tampered, np.array([0, 255]))
Пример #2
0
 def Procesar_imagen(self):
     ouput = QInputDialog.getText(self,
                                  u"Hiding EPR",
                                  u"Enter a password",
                                  text="")
     clave = ouput[0]
     hider = EPRHindingAndAuthentication(clave)
     handler = DHDicomHandler(data_handler=self.epr, hider_handler=hider)
     image = handler.process(self.original_image)
     self.draw_image(self.watermarked_canvas, image)
     self.load_epr_data(self.epr.read(image))
     self.load_epr_hidden(handler.get_epr(image))
     self.watermarked_image = image
Пример #3
0
    def test_authentic_image(self):
        from dhdicom.hidding.mixed import EPRHindingAndAuthentication

        filename = os.path.join(self.base, 'images/2.dcm')
        image = DicomImage(filename)

        registers = ['PatientName', 'PatientID']
        recipe = os.path.join(self.base, 'recipes/confidential')
        epr = EPRData(registers, recipe)
        hider = EPRHindingAndAuthentication('nuevaclave')

        handler = DHDicomHandler(data_handler=epr, hider_handler=hider)
        new_image = handler.process(image)

        authentic, *l = handler.authenticate(new_image)
        self.assertTrue(authentic)
        # No hay bloques modificados
        self.assertFalse(l)
Пример #4
0
    def analizar(self):
        output = QInputDialog.getText(self,
                                      u"Hiding EPR",
                                      u"Enter a password",
                                      text="")
        clave = output[0]
        hider = EPRHindingAndAuthentication(clave)
        self.draw_image(self.watermarked_canvas, self.original_image)

        handler = DHDicomHandler(data_handler=self.epr, hider_handler=hider)

        # Tamper dectection
        authentic, *l = handler.authenticate(self.original_image)

        if not authentic:
            block_manager = BlocksImage(self.original_image.read(), 32, 32)
            total_blocks = block_manager.max_num_blocks()
            modified_blocks = l[0]
            if modified_blocks:
                self.draw_tamper_regions(modified_blocks)
            image_modification = len(modified_blocks) / total_blocks

            if image_modification > 0.9:
                message = "Image is not authentic or its " \
                    "authenticity could not to be verified. " \
                    "View tamper region in right image. " \
                    "Make sure the password is correct."
            else:
                message = "Image is not authentic." \
                    "View tamper region in right image."
        else:
            message = "Image is authentic."

        # Extraccion del EPR
        try:
            data = handler.get_epr(self.original_image)
            self.load_epr_hidden(data)
        except json.JSONDecodeError:
            self.hiddenEPRTable.clearContents()

        QMessageBox.information(self, "Image authentication", message)
Пример #5
0
    def test_get_message(self):
        from dhdicom.hidding.mixed import EPRHindingAndAuthentication
        import pydicom

        filename = os.path.join(self.base, 'images/2.dcm')
        image = DicomImage(filename)
        ds = pydicom.read_file(filename)

        registers = ['PatientName', 'PatientID']
        patient_name = ds.PatientName
        patient_id = ds.PatientID

        recipe = os.path.join(self.base, 'recipes/confidential')
        epr = EPRData(registers, recipe)
        data = epr.read(image)
        hider = EPRHindingAndAuthentication('nuevaclave')

        handler = DHDicomHandler(data_handler=epr, hider_handler=hider)
        new_image = handler.process(image)

        self.assertEqual(data, handler.get_epr(new_image))
Пример #6
0
    def __init__(self, parent=None):
        # inicializar el padre
        super(VentanaPrincipal, self).__init__(parent)
        # configurar la interfaz
        self.setupUi(self)

        self.original_image = None
        self.watermarked_image = None
        self.hiddenEPRTable.setHorizontalHeaderLabels(["Register", "Value"])
        self.eprTable.setHorizontalHeaderLabels(["Register", "Value"])
        self.actionAbrir.triggered.connect(self.Cargar_imagen)
        self.actionSalvar.triggered.connect(self.Salvar_imagen)
        self.actionCropping.triggered.connect(self.crop_image)
        self.btn_procesar.clicked.connect(self.Procesar_imagen)
        self.btnExtractAuthenticate.clicked.connect(self.analizar)
        self.init_canvas()

        # Registros EPR
        base = os.path.dirname(os.path.dirname(__file__))
        self.epr = EPRData(settings.EPR_TO_HIDE, settings.RECIPE_FILE)
        self.hider = EPRHindingAndAuthentication('nuevaclave')
Пример #7
0
class PropertiesTest(unittest.TestCase):

    def setUp(self):
        self.hider = EPRHindingAndAuthentication('clavemaestra')

    def test_data_authentic(self):
        data = np.random.uniform(
            low=0, high=65537, size=(128, 128)).astype(int)
        msg = "Anier Soria Lorente"
        watermarked = self.hider.process(data, msg)
        authentic, *l = self.hider.authenticate(watermarked)
        self.assertTrue(authentic)
        self.assertFalse(l)

    def test_data_authentic_with_negative_values(self):
        data = np.random.uniform(
            low=-65537, high=65537, size=(128, 128)).astype(int)
        msg = "Anier Soria Lorente"
        watermarked = self.hider.process(data, msg)
        authentic, *l = self.hider.authenticate(watermarked)
        self.assertTrue(authentic)
        self.assertFalse(l)

    def test_data_not_authentic(self):
        data = np.random.uniform(
            low=0, high=65537, size=(128, 128)).astype(int)
        msg = "Anier Soria Lorente"
        watermarked = self.hider.process(data, msg)
        watermarked[0][0] += 1
        watermarked[127][127] += 1
        authentic, *l = self.hider.authenticate(watermarked)
        self.assertFalse(authentic)
        self.assertListEqual(l[0], [0, 15])

    def test_data_not_authentic_with_negative_values(self):
        data = np.random.uniform(
            low=-65537, high=65537, size=(128, 128)).astype(int)
        msg = "Anier Soria Lorente"
        watermarked = self.hider.process(data, msg)
        watermarked[0][0] += 1
        watermarked[127][127] += 1
        authentic, *l = self.hider.authenticate(watermarked)
        self.assertFalse(authentic)
        self.assertListEqual(l[0], [0, 15])

    def test_get_msg(self):
        import os

        data = np.random.uniform(
            low=0, high=65537, size=(128, 128)).astype(int)
        base = os.path.dirname(__file__)
        file = open(os.path.join(base, 'messages/Message_01.txt'), "r")
        msg = file.read()
        file.close()
        watermarked = self.hider.process(data, msg)
        msg_extracted = self.hider.get_msg(watermarked)

        self.assertEqual(msg, msg_extracted)

    def test_get_msg_with_negative_values(self):
        import os

        data = np.random.uniform(
            low=-65537, high=65537, size=(128, 128)).astype(int)
        base = os.path.dirname(__file__)
        file = open(os.path.join(base, 'messages/Message_01.txt'), "r")
        msg = file.read()
        file.close()
        watermarked = self.hider.process(data, msg)
        msg_extracted = self.hider.get_msg(watermarked)

        self.assertEqual(msg, msg_extracted)

    def test_msg_greather_than_capacity(self):
        import os
        from dhdicom.exceptions import ExceededCapacity

        data = np.random.uniform(
            low=0, high=65537, size=(128, 128)).astype(int)
        base = os.path.dirname(__file__)
        filepath = os.path.join(base, 'messages/Message_00.txt')
        file = open(filepath, "r", encoding="utf-8")
        msg = file.read()
        file.close()
        with self.assertRaises(ExceededCapacity):
            self.hider.process(data, msg)

    def test_msg_greather_than_capacity_with_negative_values(self):
        import os
        from dhdicom.exceptions import ExceededCapacity

        data = np.random.uniform(
            low=-65537, high=65537, size=(128, 128)).astype(int)
        base = os.path.dirname(__file__)
        filepath = os.path.join(base, 'messages/Message_00.txt')
        file = open(filepath, "r", encoding="utf-8")
        msg = file.read()
        file.close()
        with self.assertRaises(ExceededCapacity):
            self.hider.process(data, msg)
Пример #8
0
 def setUp(self):
     self.hider = EPRHindingAndAuthentication('clavemaestra')
Пример #9
0
def main():
    # Keys
    key = "Aslorent7N;fpr-y5"

    base = os.path.dirname(__file__)

    # Directory
    dir = "tests/dicom"
    if not os.path.exists(dir): os.makedirs(dir)

    try:
        root = Tk()
        filename = filedialog.askopenfilenames(
            parent=root, initialdir=dir, title='Please select a directory')
        # Load cover image (array)
        cover = DicomImage(filename[0])
    except Exception:
        raise ValueError("The image files were not loaded")

    root.destroy()

    # Creating matrix
    M_Metrics = []

    # Creating directory
    dir_dat_file = os.path.join("dhdicom", "static", "Results")
    if not os.path.exists(dir_dat_file): os.makedirs(dir_dat_file)
    dir_dat_file = os.path.join(dir_dat_file, filename[0].split("/")[-2])
    dir_dat_file = "%s_Results.dat" % dir_dat_file

    # Instance
    metr = Metrics()

    # Building stego images. Generating results
    for i in range(len(filename)):
        row = []
        # Generating keys
        key = utils.sha256_to_key_bin(key)
        image = DicomImage(filename[i])
        registers = ['PatientName', 'PatientID']
        recipe = os.path.join(base, 'recipes/confidential')
        epr = EPRData(registers, recipe)
        hider = EPRHindingAndAuthentication(key)
        handler = DHDicomHandler(data_handler=epr, hider_handler=hider)
        new_image = handler.process(image)
        cover_array = image.read()
        watermarked_array = new_image.read()
        # Experimental analysis
        row.append(metr.psnr(cover_array, watermarked_array))
        row.append(metr.uiqi(cover_array, watermarked_array))
        row.append(metr.image_fid(cover_array, watermarked_array))
        print(" ")
        print("Experimental analysis")
        print(" ")
        print("PSNR: ", row[0])
        print(" ")
        print("UIQI: ", row[1])
        print(" ")
        print("IF: ", row[2])
        print(" ")
        # Creating cols
        M_Metrics.append(row)
        # Saving results
        np.savetxt(dir_dat_file, M_Metrics, fmt="%.9e")

    # Saving results
    np.savetxt(dir_dat_file, M_Metrics, fmt="%.9e")