/Hide-Text-in-Image

Embed encrypted text in an image.

Primary LanguagePython

Hide-Text-In-Image

I embed an encrypted message into an image by using an image's pixel data and its exif data. The encryption scheme is simple and effective enough to hide information from the computer illiterate.

Fig 1. Hide Text in Image encryption protocol. Embedding encrypted data inside of an image of a monkey we saw at เขาใหญ่.

Encryption

  1. Read an image as a numpy array I, and perturb the first pixel's R channel by a visually indiscernible random integer -> randint(0,3).

  2. Get a secret message msg from the user. This program supports text file upload or manual entry of the secret message.

  3. Encrypt the secret message (AES128 in CBC mode with a SHA256 HMAC message authentication code) using the cryptography module.

  4. Generate a random sample S (of length = len(msg)) of the image's (x, y) coordinates. Create a hash table with each (xi, yi) in S as the key and I[0,0,0] mod 3 color channel value minus the ascii character code of msg[i] as the value. This dictionary is stored in the image's exif data, specifically the MakerNote.

  5. Add some random stuff to the hash table placed in the MakerNote such as public-key,private-key, and seed. These meaningless key-values are used to further obfuscate.

  6. Save the output image. It is critical to use a lossless image format such as png. Lossy file types can result in perturbed pixel values and information loss.

  7. Pickle the ferenet class used for encryption, and find a secure way of sending it to the reciever of the secret message. Obviously you should not send the pickle file with the image (don't send the lock and key together).

Decryption

  1. Read the image containing encrypted data. Extract the dictionary stored in the exif MakerNote. Additionally read the image's pixel data as a numpy array.

  2. Extract the encrypted message by using the dictionary stored in the MakerNote in conjunction with the image's pixel data.

  3. Use the key.pickle file to decrypt the cipher text

Dependencies

  • python 3.8
  • numpy
  • piexif
  • cryptography

Usage

(1) Decryption

py -3.8 encrypt.py --input (fname).png

*Note this assumes you have the key.pickle file in the current directory. (fname) is the name of the png file containing encrypted data.

(1) Encryption

py -3.8 encrypt.py --write --input (fname).png --output (oname).png

*(fname) is the name of the png file that you would like to embed encrypted data. (oname) is the name of the output file with encrypted data

*After you run in encryption mode, you will also generate a key.pickle file. This file is used by the recipient to decrypt the message. Technically the recipient would also need to be able to run this python file.