0

I need to decrypt a png file. I can't open/view the image because it is encrypted. When I run the file command on the image in the command line, it says that it's a 'data' type.

I know the image is encrypted using XOR (as in the case of one-time pad), with a secret key which I do not know of.

I only have the image file and no other information. How should I go about finding out the secret key?

Gabor Lengyel
  • 14,129
  • 4
  • 32
  • 59
SugarDrink
  • 21
  • 3
  • Brew tons of coffee and try each combination (this is called "brute force") and that's why encryption should be unbreakable :-) – Michael Fehr Feb 11 '21 at 00:36

2 Answers2

2

You would also have to know whether the one-time pad is as long as the original image or not. If the pad is shorter then it will be repeated until the end of the plaintext. If it is 1 up to 7 bytes long then it is really easy, because the first 8 bytes of the PNG file format are known: \x89PNG\r\n\x1a\n.

Calculate key = ciphertext[0] ^ '\x89'. If key ^ ciphertext[1] == 'P' then you have your key. Otherwise you need to check key ^ ciphertext[i] == knownHeader[i] for i in 2 to 8 to see if you have the beginning of the key. Depending on the i where you found the match, then you know how long the pad is. Afterwards you can calculate the remaining key bytes.

The only remaining thing is to use the whole key to decrypt the whole file and check whether it is something sensible.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • 2
    99 times out of a hundred (assuming `IHDR` is the first chunk after the PNG header, and the image is less than 65,536 pixels wide), you can actually assume the first 18 bytes are `89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00`. The last eight bytes should start with `IEND`, which should be useful for confirming the key length. – r3mainer Feb 10 '21 at 19:09
1

XORing a random bitstream will result in a practically random output, because each bit changes with a probability of 0.5 (see Shannon, 1949). If you have neither the key nor the image, you have no way to recover the image, or the key itself.

If this is some kind of a challenge, you can try and use the properties of XOR to your advantage. If for example there is another image XORed with the same key, and you have that image plaintext and encrypted, you can obviously find the key by XORing the unencrypted image to the encrypted one. Or you can for example try guessing at keys that are for some reason obvious in your context.

Also one time pad has all the great features if the key is truly random, and has the same length as the input. Your key may for example be shorter and repeated.

One thing you can exploit is the fact that an image file has a known format, so quite a few bits of the key you can find out if you know what format it should be. At the beginning there is the magic number to identify the file format, and then depending on the actual image type some fields have few or just one potential value. Let's fill those in in your key being constructed and see if there is a pattern. But again, if the key is truly random, never reused, and as long as the image, it is not possible to recover the image file.

Gabor Lengyel
  • 14,129
  • 4
  • 32
  • 59