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
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
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.
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.
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