I am writing a program in python (myself) to encrypt images. I don't want or expect it to be state of the art encryption and safe at the end but i have a problem. The result encrypted image still has a recognizable trace of the original image. I know its because of they way i am encrypting it.... so what my program does it loads an image as an array of integer values for all pixel colors (RGB) and then i convert an ASCII password into numbers from 0-94 (type-able characters) and then use a saved randomly generated dictionary to map these numbers randomly to numbers from 0-255 so that i would avoid colors possibly staying to similar. Then i use this number to shift the pixel values with formula old_pixel_value + hashed_password_character mod 256. The problem is though that if there is a big white-space or a patch of uniform color there are these repeating stripes and then you can kind of still recognize the shape ill put a picture below. So how do i change or what do i add so that the encrypted image seems more random???

(the first one is the non encrypted the second is after encryption) Not encrypted image output image after encryption


1 Answer 1


What you've done is this:-


It's called electronic code book style encryption. For block cipher encryption read your own pixel transformation algorithm. And for plain text, read original pixel values. Can you see the how the pattern repeats? That's the length of your hashed password, and what's called the block width. You've used it over and over again encrypting a block width's amount of pixels. Because the background is constant white, it encrypts to the same value over and over. And as the block width straddles the bike, it kinda still shows up against the constantly repeated background.

What you need to do it this:-


It's called cipher block chaining and creates a relationship between sequential blocks. You'll have to adjust your algorithm so that one block's data is somehow propagated to the next one. The traditional way (as illustrated) is to simply xor it. Now every subsequent block will be affected by the previous block and you'll get a much more random pattern.

You don't need the initialisation vector thing at the moment. You can address that later when your chaining algorithm works. It's highly recommended though to stop relay attacks and leak encrypted information when you repeatedly transmit the same information (the cipher text will be identical each time suggesting the same information was sent).

Note. It's also traditional to use a fairly complicated set of xor and bit shift operations to encrypt your information /image. These would be inside the block cipher encryption box. They create an avalanche effect so very small changes create massive ones. You use just a shift operator. I'm not 100% confident that a simple shift will not still show the bike's outline somewhat. Anyone?

  • $\begingroup$ So i kind of followed your advice and fed the encrypted pixels into the password array so that it keeps changing is dependant on the previous. And it works the bike image now looks completely random. I tried encrypting a huge blank image and eventually a pattern appeared but thats not the problem since normal images dont have that much uniform color. But now the problem is with the decryption because i am simply feeding the new pixels as the password if i have the same password length after the first few pixels the image is perfectly identical... now what did i do wrong? $\endgroup$
    – Philogy
    Commented Jul 31, 2017 at 14:03
  • $\begingroup$ new pixels as the password is wrong. The password is still the password -> your colour shifting encryption algorithm has to be reversible. AES is reversible. Look at the decryption architecture in @SEJPM 's comment to you, and figure out how to reverse your shift algorithm. $\endgroup$
    – Paul Uszak
    Commented Aug 1, 2017 at 0:21

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