Example #1
0
 def decrypt(self, img: np.ndarray, pth="static/integral_rgba"):
     r_ = img[:, :, 0]
     g_ = img[:, :, 1]
     b_ = img[:, :, 2]
     a_ = img[:, :, 3]
     print("decrypting LM ...")
     a_ = eu.decrypt(a_, eu.SECRET_KEY, 256).astype(np.uint8)
     print("LM decrypted!")
     r_LM = a_ % 2
     a_ >>= 1
     g_LM = a_ % 2
     a_ >>= 1
     b_LM = a_ % 2
     r_thread = RDHDecryptedThread(self.r, r_, r_LM, pu.path_join(pth, "r"), "r")
     g_thread = RDHDecryptedThread(self.g, g_, g_LM, pu.path_join(pth, "g"), "g")
     b_thread = RDHDecryptedThread(self.b, b_, b_LM, pu.path_join(pth, "b"), "b")
     b_thread.start()
     g_thread.start()
     r_thread.start()
     b_thread.join()
     g_thread.join()
     r_thread.join()
     # self.r.decrypt(r_, r_LM, False, pth=pth + "/r")
     # self.g.decrypt(g_, g_LM, False, pth=pth + "/g")
     # self.b.decrypt(b_, b_LM, False, pth=pth + "/b")
     r, g, b = self.r.decrypted, self.g.decrypted, self.b.decrypted
     self.decrypted = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)
     self.decrypted[:, :, 0] = b
     self.decrypted[:, :, 1] = g
     self.decrypted[:, :, 2] = r
     iu.save_img(self.decrypted, pu.path_join(pu.get_root_path(), pth, "res.png"))
Example #2
0
 def encrypt(self, img: np.ndarray, r_data=0, g_data=0, b_data=0, pth=__INTEGRAL_PATH):
     b_ = img[:, :, 0]
     g_ = img[:, :, 1]
     r_ = img[:, :, 2]
     b_thread = RDHEncryptedThread(self.b, b_, b_data, pu.path_join(pth, "b"), "b")
     g_thread = RDHEncryptedThread(self.g, g_, g_data, pu.path_join(pth, "g"), "g")
     r_thread = RDHEncryptedThread(self.r, r_, r_data, pu.path_join(pth, "r"), "r")
     b_thread.start()
     g_thread.start()
     r_thread.start()
     b_thread.join()
     g_thread.join()
     r_thread.join()
     # self.b.encrypt(b_, b_data, pth=pth + "/b")
     # self.g.encrypt(g_, g_data, pth=pth + "/g")
     # self.r.encrypt(r_, r_data, pth=pth + "/r")
     self.LM = self.b.LM.astype(np.uint8) * 4 + \
               self.g.LM.astype(np.uint8) * 2 + \
               self.r.LM.astype(np.uint8) * 1
     print("encrypting LM ...")
     self.encrypted_LM = eu.encrypt(self.LM, eu.SECRET_KEY, 256)
     print("LM encrypted!")
     self.encrypted = np.zeros((img.shape[0], img.shape[1], 4), np.uint8)
     self.encrypted[:, :, 0] = self.r.encrypted
     self.encrypted[:, :, 1] = self.g.encrypted
     self.encrypted[:, :, 2] = self.b.encrypted
     self.encrypted[:, :, 3] = self.encrypted_LM
     iu.save_img(self.encrypted, pu.path_join(pu.get_root_path(), pth, "image.png"))
Example #3
0
 def save(self, pth: str):
     """
     将LM和加密后的图片存储到路径中
     :param pth: 存储的路径
     :return:
     """
     iu.save_img(self.encrypted_I, pu.path_join(pth, "image.png"))
     iu.save_img(self.encrypted_LM, pu.path_join(pth, "LM.png"))
Example #4
0
 def save(img, name):
     """
     :param pth: 存储的路径
     :return:
     """
     root_path = pu.get_root_path()
     out = pu.path_join(root_path, pu.REC_PATH)
     pth = pu.path_join(out, name)
     iu.save_img(img, pth)
Example #5
0
def main(file_name="200px-Lenna.jpg"):
    root_path = pu.get_root_path()
    # file_name = "trees.png"
    file_path = pu.path_join(root_path, pu.INPUT_PATH, file_name)

    color_lena = iu.read_img(file_path, iu.READ_COLOR)

    r_data = 0b11111000001111100000
    g_data = 0b11111111110000000000
    b_data = 0b10101010101010101010

    length = [len(bin(r_data)), len(bin(g_data)), len(bin(b_data))]
    tplt = "{0:<" + str(length[0]) + "}\t{1:<" + str(length[1]) + "}\t{2:<" + str(length[2]) + "}"

    print(tplt.format("r_data(insert):", "g_data(insert):", "b_data(insert):"))
    print(tplt.format(bin(r_data), bin(g_data), bin(b_data)))

    r = ReversibleDataHidingRGBA()
    r.encrypt(color_lena, r_data, g_data, b_data)
    # iu.print_img(r.encrypted)

    # encrypted = iu.read_img(pu.path_join(root_path, "static", "integral_rgba", "image.png"), cv2.IMREAD_UNCHANGED)
    r.decrypt(r.encrypted)
    # iu.print_img(r.decrypted)
    print(tplt.format("r_data(extract):", "g_data(extract):", "b_data(extract):"))
    print(tplt.format(r.r_data, r.g_data, r.b_data))
    print(f"max length of hide data: {r.max_length}")
    print(f"image size: {color_lena.shape}")
    print("file save in /static/integral_rgba!")
Example #6
0
def main(file_name):
    a = ReversibleDataHiding()
    root_path = pu.get_root_path()
    file_path = pu.path_join(root_path, pu.INPUT_PATH, file_name)

    gray_lena = iu.read_img(file_path, iu.READ_GRAY)
    # changed_gray_lena = gray_lena.copy()
    # changed_gray_lena[100, 100] = 0

    # a.encrypt(gray_lena, 0b11111000001111100000)
    # a.decrypt()

    # print("1")
    print("encrypting!")
    print("insert data: 0b11111000001111100000")
    a.encrypt(gray_lena, 0b11111000001111100000)
    # print("2")
    print("decrypting!")
    a.decrypt()
    # print("3")
    # a.encrypt(changed_gray_lena, 0b11111000001111100000, pth="static/test/changed")
    # print("4")
    # a.decrypt(pth="static/test/changed")
    print("extract data: ", end="")
    print(a.r.data)
    print(f"hide data max length: {a.e.max_length()}")
    print(f"image size: {gray_lena.shape}")
    # print(a.r.data)
    print("file save in /static/integral!")
Example #7
0
class ReversibleDataHiding:
    __INTEGRAL_PATH = pu.path_join("static", "integral")

    def __init__(self):
        self.r = None
        self.data_length = 0
        self.LM = None
        self.encrypted = None
        self.e = None
        self.decrypted = None
        self.root_path = pu.get_root_path()

    def encrypt(self, img, hide_data: int = 0, pth=__INTEGRAL_PATH):
        """

        Args:
            img: 要加密的图片
            hide_data: 想要隐藏的数据
            pth: 保存的相对路径

        Returns:

        """
        self.data_length = hide_data.bit_length()
        e = Encryptor(img)
        e.decomposition()
        e.predict()
        e.recomposition()
        e.data_hider(hide_data)
        e.encryption()
        e.save(pu.path_join(self.root_path, pth))
        self.LM = e.LM
        self.encrypted = e.encrypted_I
        self.e = e

    def decrypt(self, img=None, LM=None, LM_encrypted=True, pth=__INTEGRAL_PATH):
        """

        Args:
            img: 需要解密的图像
            LM: 保存有LM图像
            LM_encrypted: LM是否需要解密
            pth: 保存的路径

        Returns:

        """
        if img is None:
            img = iu.read_img(pu.path_join(self.root_path, pth, "image.png"))
        if LM is None:
            LM = iu.read_img(pu.path_join(self.root_path, pth, "LM.png"))
        r = Receiver(img, LM, LM_encrypted)
        r.decryption()
        r.data_extraction(self.data_length)
        r.recomposition()
        iu.save_img(r.res_img, pu.path_join(self.root_path, pth, "res.png"))
        self.decrypted = r.res_img
        self.r = r
Example #8
0
    def decrypt(self, img=None, LM=None, LM_encrypted=True, pth=__INTEGRAL_PATH):
        """

        Args:
            img: 需要解密的图像
            LM: 保存有LM图像
            LM_encrypted: LM是否需要解密
            pth: 保存的路径

        Returns:

        """
        if img is None:
            img = iu.read_img(pu.path_join(self.root_path, pth, "image.png"))
        if LM is None:
            LM = iu.read_img(pu.path_join(self.root_path, pth, "LM.png"))
        r = Receiver(img, LM, LM_encrypted)
        r.decryption()
        r.data_extraction(self.data_length)
        r.recomposition()
        iu.save_img(r.res_img, pu.path_join(self.root_path, pth, "res.png"))
        self.decrypted = r.res_img
        self.r = r
Example #9
0
def __test(file):
    e = Encryptor(file, Encryptor.predict_method1)
    e.decomposition()
    e.predict()
    e.recomposition()
    print(e.max_length())
    e.data_hider(0b11000100000110001111111011100001000001100011111110111000010000011000111111101110000100000110001111111011)
    e.encryption()
    root_path = pu.get_root_path()
    out = pu.path_join(root_path, pu.OUTPUT_PATH)
    e.save(out)
    # lm = dec.decryptioner(e.encrypted_LM, dec.secretKey, 256)
    # diff = e.LM.astype(np.int) - lm.astype(np.int)
    # i = dec.decryptioner(e.encrypted_I, dec.secretKey, 256)
    # diff_I = i - e.res_img
    # print(np.sum(abs(diff)))
    # print(np.sum(abs(diff_I)))
    # iu.print_imgs(e.ans.astype(np.uint8), e.encrypted_LM.astype(np.uint8))
    pass
Example #10
0
    def encrypt(self, img, hide_data: int = 0, pth=__INTEGRAL_PATH):
        """

        Args:
            img: 要加密的图片
            hide_data: 想要隐藏的数据
            pth: 保存的相对路径

        Returns:

        """
        self.data_length = hide_data.bit_length()
        e = Encryptor(img)
        e.decomposition()
        e.predict()
        e.recomposition()
        e.data_hider(hide_data)
        e.encryption()
        e.save(pu.path_join(self.root_path, pth))
        self.LM = e.LM
        self.encrypted = e.encrypted_I
        self.e = e
Example #11
0
class ReversibleDataHidingRGBA:

    __INTEGRAL_PATH = pu.path_join("static", "integral_rgba")

    b = ReversibleDataHiding()
    g = ReversibleDataHiding()
    r = ReversibleDataHiding()

    def __init__(self):
        self.LM = None

    @property
    def max_length(self):
        return self.r.e.max_length() + \
               self.g.e.max_length() + \
               self.b.e.max_length()

    @property
    def r_data(self):
        return self.r.r.data

    @property
    def g_data(self):
        return self.g.r.data

    @property
    def b_data(self):
        return self.b.r.data

    def encrypt(self, img: np.ndarray, r_data=0, g_data=0, b_data=0, pth=__INTEGRAL_PATH):
        b_ = img[:, :, 0]
        g_ = img[:, :, 1]
        r_ = img[:, :, 2]
        b_thread = RDHEncryptedThread(self.b, b_, b_data, pu.path_join(pth, "b"), "b")
        g_thread = RDHEncryptedThread(self.g, g_, g_data, pu.path_join(pth, "g"), "g")
        r_thread = RDHEncryptedThread(self.r, r_, r_data, pu.path_join(pth, "r"), "r")
        b_thread.start()
        g_thread.start()
        r_thread.start()
        b_thread.join()
        g_thread.join()
        r_thread.join()
        # self.b.encrypt(b_, b_data, pth=pth + "/b")
        # self.g.encrypt(g_, g_data, pth=pth + "/g")
        # self.r.encrypt(r_, r_data, pth=pth + "/r")
        self.LM = self.b.LM.astype(np.uint8) * 4 + \
                  self.g.LM.astype(np.uint8) * 2 + \
                  self.r.LM.astype(np.uint8) * 1
        print("encrypting LM ...")
        self.encrypted_LM = eu.encrypt(self.LM, eu.SECRET_KEY, 256)
        print("LM encrypted!")
        self.encrypted = np.zeros((img.shape[0], img.shape[1], 4), np.uint8)
        self.encrypted[:, :, 0] = self.r.encrypted
        self.encrypted[:, :, 1] = self.g.encrypted
        self.encrypted[:, :, 2] = self.b.encrypted
        self.encrypted[:, :, 3] = self.encrypted_LM
        iu.save_img(self.encrypted, pu.path_join(pu.get_root_path(), pth, "image.png"))

    def decrypt(self, img: np.ndarray, pth="static/integral_rgba"):
        r_ = img[:, :, 0]
        g_ = img[:, :, 1]
        b_ = img[:, :, 2]
        a_ = img[:, :, 3]
        print("decrypting LM ...")
        a_ = eu.decrypt(a_, eu.SECRET_KEY, 256).astype(np.uint8)
        print("LM decrypted!")
        r_LM = a_ % 2
        a_ >>= 1
        g_LM = a_ % 2
        a_ >>= 1
        b_LM = a_ % 2
        r_thread = RDHDecryptedThread(self.r, r_, r_LM, pu.path_join(pth, "r"), "r")
        g_thread = RDHDecryptedThread(self.g, g_, g_LM, pu.path_join(pth, "g"), "g")
        b_thread = RDHDecryptedThread(self.b, b_, b_LM, pu.path_join(pth, "b"), "b")
        b_thread.start()
        g_thread.start()
        r_thread.start()
        b_thread.join()
        g_thread.join()
        r_thread.join()
        # self.r.decrypt(r_, r_LM, False, pth=pth + "/r")
        # self.g.decrypt(g_, g_LM, False, pth=pth + "/g")
        # self.b.decrypt(b_, b_LM, False, pth=pth + "/b")
        r, g, b = self.r.decrypted, self.g.decrypted, self.b.decrypted
        self.decrypted = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)
        self.decrypted[:, :, 0] = b
        self.decrypted[:, :, 1] = g
        self.decrypted[:, :, 2] = r
        iu.save_img(self.decrypted, pu.path_join(pu.get_root_path(), pth, "res.png"))
Example #12
0
        out = pu.path_join(root_path, pu.REC_PATH)
        pth = pu.path_join(out, name)
        iu.save_img(img, pth)

    @staticmethod
    def composition(img0, img1, img2, img3):
        a = np.hstack((img0, img1))
        b = np.hstack((img2, img3))
        all = np.vstack((a, b))
        return all


def __test(img, LM):
    e = Receiver(img, LM)
    e.decryption()
    e.data_extraction(104)
    e.recomposition()
    Receiver.save(e.res_img, 'res.png')


if __name__ == '__main__':
    root_path = pu.get_root_path()
    file1_name = "image.png"
    file1_path = pu.path_join(root_path, pu.OUTPUT_PATH, file1_name)
    file2_name = "LM.png"
    file2_path = pu.path_join(root_path, pu.OUTPUT_PATH, file2_name)

    encrypted_img = iu.read_img(file1_path, iu.READ_GRAY)
    LM = iu.read_img(file2_path, iu.READ_GRAY)
    __test(encrypted_img, LM)
Example #13
0
    for i in range(0, 4):
        P = permutation(P, init_matixs[i], M, N)
        P = row_diffusion(P, init_matixs[i], F, M, N)
        P = column_diffusion(P, init_matixs[i], F, M, N)
    return P


def decrypt(P, S, F=256):
    """

    Args:
        P: 要解密的矩阵
        S: 密钥
        F: pixel值

    Returns: 解密后的数据

    """
    return dec.decryptioner(P, S, F)


if __name__ == '__main__':
    root_path = pu.get_root_path()
    file_path = pu.path_join(root_path, pu.INPUT_PATH, '200px-Lenna.jpg')
    P = iu.read_img(file_path, iu.READ_GRAY)
    encrypted_img = encrypt(P, SECRET_KEY, 256).astype(np.uint8)
    iu.print_img(encrypted_img, "encrypted_lena")
    anti_encrypted_img = dec.decryptioner(encrypted_img, SECRET_KEY,
                                          256).astype(np.uint8)
    iu.print_img(anti_encrypted_img, "anti-encrypted_lena")
Example #14
0
    r1 = img1[:, :, 2]
    b2 = img2[:, :, 0]
    g2 = img2[:, :, 1]
    r2 = img2[:, :, 2]
    r = np.array([0, 0, 0, 0, 0])
    g = np.array([0, 0, 0, 0, 0])
    b = np.array([0, 0, 0, 0, 0])
    r[0], r[1] = npcr_uaci_analysis(r1, r2)
    g[0], g[1] = npcr_uaci_analysis(g1, g2)
    b[0], b[1] = npcr_uaci_analysis(b1, b2)
    r[2] = ent_analysis(r2)
    g[2] = ent_analysis(g2)
    b[2] = ent_analysis(b2)
    r[3], r[4] = cor_analysis(r1, r2)
    g[3], g[4] = cor_analysis(g1, g2)
    b[3], b[4] = cor_analysis(b1, b2)
    return np.mean(r + g + b)


if __name__ == "__main__":
    root_path = pu.get_root_path()
    img1 = iu.read_img(
        pu.path_join(root_path, "static/test/unchanged/image.png"),
        iu.READ_GRAY)
    img2 = iu.read_img(
        pu.path_join(root_path, "static/test/changed/image.png"), iu.READ_GRAY)

    print(npcr_uaci_analysis(img1, img2))
    print(ent_analysis(img2))
    print(cor_analysis(img1, img2))
Example #15
0
    e.data_hider(0b11000100000110001111111011100001000001100011111110111000010000011000111111101110000100000110001111111011)
    e.encryption()
    root_path = pu.get_root_path()
    out = pu.path_join(root_path, pu.OUTPUT_PATH)
    e.save(out)
    # lm = dec.decryptioner(e.encrypted_LM, dec.secretKey, 256)
    # diff = e.LM.astype(np.int) - lm.astype(np.int)
    # i = dec.decryptioner(e.encrypted_I, dec.secretKey, 256)
    # diff_I = i - e.res_img
    # print(np.sum(abs(diff)))
    # print(np.sum(abs(diff_I)))
    # iu.print_imgs(e.ans.astype(np.uint8), e.encrypted_LM.astype(np.uint8))
    pass


if __name__ == '__main__':
    root_path = pu.get_root_path()
    file_name = "200px-Lenna.jpg"
    file_path = pu.path_join(root_path, pu.INPUT_PATH, file_name)

    gray_lena = iu.read_img(file_path, iu.READ_GRAY)
    #
    # e1 = Encryptor(gray_lena, Encryptor.predict_method1)
    # e1.decomposition()
    # e1.predict()
    #
    # print(e1.error())

    # iu.print_imgs(e1.recomposition())
    __test(gray_lena)
Example #16
0
    g1 = img1[:, :, 1]
    r1 = img1[:, :, 2]
    b2 = img2[:, :, 0]
    g2 = img2[:, :, 1]
    r2 = img2[:, :, 2]
    r = np.array([0, 0, 0])
    g = np.array([0, 0, 0])
    b = np.array([0, 0, 0])
    r[0] = ssim(r1, r2)
    r[1] = mse(r1, r2)
    r[2] = psnr(r1, r2)
    g[0] = ssim(g1, g2)
    g[1] = mse(g1, g2)
    g[2] = psnr(g1, g2)
    b[0] = ssim(b1, b2)
    b[1] = mse(b1, b2)
    b[2] = psnr(b1, b2)
    return np.mean(r + g + b)


if __name__ == "__main__":
    root_path = pu.get_root_path()
    img1 = iu.read_img(pu.path_join(root_path, "static/input/timg.jpeg"),
                       iu.READ_GRAY)
    img2 = iu.read_img(
        pu.path_join(root_path, "static/test/unchanged/res.png"), iu.READ_GRAY)

    print(ssim(img1, img2))
    print(mse(img1, img2))
    print(psnr(img1, img2))
Example #17
0
import src.util.image_util as iu
import src.util.path_util as pu

import numpy as np

if __name__ == '__main__':
    root_path = pu.get_root_path()
    src_black_img = iu.read_img(
        pu.path_join(root_path, pu.INPUT_PATH, "200px-Lenna.jpg"),
        iu.READ_GRAY)
    res_black_img = iu.read_img(
        pu.path_join(root_path, "static", "integral", "res.png"), iu.READ_GRAY)

    src_color_img = iu.read_img(
        pu.path_join(root_path, pu.INPUT_PATH, "trees.png"), iu.READ_COLOR)
    res_color_img = iu.read_img(
        pu.path_join(root_path, "static", "integral_rgba", "res.png"),
        iu.READ_COLOR)

    diff_color_img = abs(
        src_color_img.astype(np.int) - res_color_img.astype(np.int)).astype(
            np.uint8)
    diff_black_img = abs(
        src_black_img.astype(np.int) - res_black_img.astype(np.int)).astype(
            np.uint8)

    iu.print_imgs(diff_black_img, diff_color_img)

    diff_color_path = pu.path_join(root_path, "static", "difference",
                                   "color_difference.png")
    diff_black_path = pu.path_join(root_path, "static", "difference",