Krishna Kalubandi
Krishna Kalubandi

Reputation: 542

(2 n) Visual Cryptography using Python

I am trying to implement the simple algorithm mentioned on wikipedia to create a (2 n) secret sharing images using python.

There is a simple algorithm for binary (black and white) visual cryptography that creates 2 encrypted images from an original unencrypted image. The algorithm is as follows: First create an image of random pixels the same size and shape as the original image. Next, create a second image the same size and shape as the first, but where a pixel of the original image is the same as the corresponding pixel in the first encrypted image, set the same pixel of the second encrypted image to the opposite color. Where a pixel of the original image is different than the corresponding pixel in the first encrypted image, set the same pixel of the second encrypted image to the same color as the corresponding pixel of the first encrypted image. The two apparently random images can now be combined using an exclusive-or (XOR) to re-create the original image.

This is my code for generating share1 and share2

    share1 = Image.new("1", size, "white")
    share1pix = share1.load()
    for i in range(len(self.key)):
        for j in range(255):
            x = randint(0,1)
            if x == 0:
                share1pix[j, i] = 0
    share2 = Image.new("1", size)
    share2pix = share2.load()
    for i in range(len(self.key)):
        for j in range(255):
            if pix[j, i] == share1pix[j, i]:
                if pix[j, i] == 255:
                    share2pix[j, i] = 0
                else:
                    share2pix[j, i] = 255
            else:
                share2pix[j, i] = share1pix[j, i]

where pix is the original image array. However, I am getting the output as

original image

original image

share1

share1

share2

share2

As you can see, share2.png is not random. It is just the reverse of original.png. Where am I going wrong? Please help. TIA.

Upvotes: 0

Views: 1813

Answers (1)

alpha1554
alpha1554

Reputation: 605

You have a little error in your algorithm. The idea is that for each pixel of pix, share1 and share2, we have the following relation :

share2 = pix XOR share1

This translates into :

if pix[j, i] == share1[j, i]:
  share2[j, i] = 0
else:
  share2[j, i] = 255

Upvotes: 1

Related Questions