Thread: Image encryption/decryption

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    5

    Image encryption/decryption

    Hi,
    I want to encrypt an image (JPEG or BMP), my problem is how to manipulate image and recover it, is there a simple code or tutorial to help
    thanks a lot

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Are you looking at encrypting just the image data or the entire file? If you want to encrypt the entire file, then you don't have to worry about what it is. If you only want to encrypt the image data, look for an image library that can handle common image formats like png, jpeg and so on.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    Quote Originally Posted by Subsonics View Post
    Are you looking at encrypting just the image data or the entire file? If you want to encrypt the entire file, then you don't have to worry about what it is. If you only want to encrypt the image data, look for an image library that can handle common image formats like png, jpeg and so on.
    Yes I want to encrypt image data, can you suggest me a portable image library

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    The only image format where you can sensibly encrypt just the image data is a plain BMP file.
    The image isn't compressed in any way to begin with, and all possible values result in a valid RGB value.
    It's also very easy to write the code to just get the image data to begin with.

    PNG is heavily compressed, and it relies on large areas of the image having the same colour. Encryption is going to render every pixel different, which will totally stuff PNG compression.

    JPG files are going to be tricky. Sure you can parse the file to extract the encoded image blocks, but if you encrypt an image block, is the result still valid in JPG?
    Nor can you decode an image block to pixels, then encrypt it, then convert it back to a JPG image block. JPG is lossy, so it will completely destroy your ability to decrypt later on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If the only transform you want to do is encryption, I'd think any technique that works on binary data would be fine -- it does not matter if it is an image or not, unless you want the image to remain an image, but scrambled. Ie, you could encrypt "whatever.jpg" using a general technique, such that the encrypted file is useless until decrypted, or you could use a more specific technique such that the encrypted file is still a .jpg, but the image looks scrambled. Do you understand the difference?

    For the later, imagemagick does image encryption, with some limitations (namely, no lossy types such as jpeg, as Salem mentions):
    Transformations -- IM v6 Examples
    with a gnarly looking C API, lol:
    ImageMagick: MagickWand, C API for ImageMagick
    but I dunno if that means you can do the encryption with the C API. Probably. Supposedly there is a lower level C API too (referred to above).

    That route may not be the easiest. Unless you really really want to produce a scrambled version that is still an image, I'd just use a standard technique such as SHA-whatever:

    Secure Hash Algorithm - Wikipedia, the free encyclopedia

    You can google and find a bunch of libraries which implement this. Just make sure they are intended for use on both text and binary data.

    Another option: if you are only going to apply this to a specific (lossy) type with an initial header, you could figure out the format, extract the header (maybe using magickwand or libjpeg), then SHA scramble only the remaining (image) data.
    Last edited by MK27; 04-01-2012 at 07:33 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption and Decryption
    By dbz8gokugohan in forum C Programming
    Replies: 7
    Last Post: 12-02-2010, 07:40 AM
  2. encryption / decryption
    By ghatoz in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2010, 06:45 AM
  3. encryption / decryption program
    By epidemic in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2008, 06:40 AM
  4. Encryption/Decryption Help
    By coolage in forum C++ Programming
    Replies: 9
    Last Post: 04-25-2008, 01:53 AM
  5. Ask about Encryption and Decryption
    By ooosawaddee3 in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 12:55 AM

Tags for this Thread