I would like to be able to "code" images such as the one seen here on Wikipedia.

Is this generally produced with software, or does this require complex programming? Ideally, I would love to be able to batch encrypt images based on settings I pre-defined.

  • 1
    $\begingroup$ high level: 1) Create an image you want to encrypt. 2) Create a random file of the same size (bitmaps will look better) 3) Create the bitwise XOR of the image from 1) and 2) 4) Output the random image and the result of step 3) Recovery: Bitwise XOR the outputs to recover the original picture $\endgroup$
    – SEJPM
    Commented Aug 22, 2015 at 20:46
  • 2
    $\begingroup$ Actually, I believe that the combination function used in the example is AND, not XOR. That is, the pixel in the final image is white only if the pixels in both the original images is white. That said, it turns out the generation process still (mostly) works (!) $\endgroup$
    – poncho
    Commented Aug 22, 2015 at 23:13

1 Answer 1


Actually, it's fairly simple:

  • Generate an image with random pixelation; that is, each pixel is either black or white with probability 1/2. We'll call this image 1.

  • Exclusive-or image 1 with the target to generate image 2; so that a pixel image 2 will be white if the pixels in image 1 and the target are the same (both with or both black), and black if they differ.

Now, you combine the two images by overlaying them; what this effectively does is an AND function, in the sense that a pixel in the combined image will be white only if both image 1 and image 2 are white.

Now, if we have an area of pixels in the target image which are black, then when we combine those images, that area will be black as well. That's because, of any pixel in that area, either the pixel in image 1 will be black, or the pixel in image 2 will be black.

In contrast, if we have an area of pixels in the target image which are white, then when we combine those images, that area will be gray (likely consist of half white and half black pixels). Any pixels that happen to be black in image 1 will, of course, be black; any pixels that happen to be white in image 1 will also be white in image 2; as image 1 was random, this results in a gray area.

  • $\begingroup$ Thank you very much, @Poncho, for your detailed answer. Unfortunately, I read it three times, and I have no clue of how to do any of the things you listed. "Generate an image with random pixelation"...? Sure, but how? Etc. $\endgroup$ Commented Aug 23, 2015 at 7:03
  • $\begingroup$ @fabriced, that part is going to be mostly off topic here, see crypto.stackexchange.com/help/on-topic $\endgroup$
    – otus
    Commented Aug 23, 2015 at 11:07
  • $\begingroup$ Using common open source tools: "OpenSSL rand" to generate width x height x 3 bytes to a file KEY.RAW. use imagemagick to convert this raw file to KEY.PNG. Again using imagemagick, apply an XOR filter combining KEY.PNG image with your original PLAIN.PNG image, producing a new CIPHER.PNG image. KEY.PNG and CIPHER.PNG appear to be just noise, but can be combined with the imagemagick XOR filter to recreate a pixel-perfect copy of PLAIN.PNG. $\endgroup$
    – rmalayter
    Commented Aug 26, 2015 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.