Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m Building a project and i m stuck with this part... i want to encrypt the pixels of a bitmap image using DES and then display this new image....and then bring the original image back from this image...

Thank You.
Posted
Comments
Bernhard Hiller 14-Apr-14 4:16am    
"display this new image" - I do not understand that. When the data are encrypted, it does not make any sense to display them as if they were not encrypted!

Then problem you have is that you cannot encrypt the entire image file. Doing so would create an invalid image format. What you want to do really doesn't have any purpose at all and is a bit difficult.

Encrypting the image data is NOT mean that the number of bytes you get out of the encryption algorithm is going to be the same as the number of bytes that went in. This means your image will be a different size. You'll have to create a new image file with different size parameters and include the encrypted data. What are those new parameters going to be? There's no way to know and you can get a stream of bytes that doesn't divide nicely anywhere near the original image size.

Now, the second problem is decrypting the image. Now that you have a new image, you've lost the original size data for the source image. How are you going to recreate the image metadata since it no longer exists in the encrypted data?

Whoops.

Just encrypt the entire image file and don't even think of trying to display it. Nobody does this - for a reason.
 
Share this answer
 
Comments
Bernhard Hiller 14-Apr-14 8:53am    
Well, if he used a 32bit key (hm...) for encrypting one Red-Green-Blue-Alpha value after each other, it could work; in case of a bitmap with even width and height, he could use blocks of 4 pixels with a 128bit key. But also I think it's quite useless.
Dave Kreskowiak 14-Apr-14 9:35am    
Not exactly a perfect solution as 128-bit keys aren't exactly the most secure things in the world and the image data must be a multiple of larger keys in order to work.

Then there's salting for a proper scrambling, which changes the data length... ;)
Member 10672244 14-Apr-14 10:56am    
Bernhard and Dave, thanks for your concern.....what i m trying to say is that..i choose a bitmap image and then after encryption it should look like this.." http://www.lunatim.com/oddsends/cryptocoded.gif"...this is what i meant by saying display this new image...

Thank You.
Dave Kreskowiak 14-Apr-14 11:06am    
Yeah and that's what we're been talking about. There are things that must be taken into account. The first of which is that you must use an encryption algorithm that returns the same number of bytes as the image data in the original image file. You can NOT encrypt the entire file as the metadata for the image will be destroyed and make the image impossible to render.

The second is that you must carefully pick your key size. Bernhard said it, the best key size to use for a 4-byte-per-pixel image is 128-bits because that puts your key on pixel boundaries. If you use a larger key size, the data length must be a multiple of the key size or you will end up with extra data on the end of the encrypted data, possibly invalidating your image.

Then, there's the metadata issue I already talked about.
Because of the block size constraints, better use AES / Rijndael. Assuming an even (i.e. can be divided by 2) width and height of your original image, you can use following (slow!) mechanism:
- create a new bitmap of same size as the original bitmap
- iterate through all pixels of the original bitmap in blocks of 4 pixels
- put their red/green/blue/alpha data into an array of 16 bytes (those values are bytes)
- encrypt the 16byte array
- add 4 pixels to the new bitmap based on the encrypted array
Since the algorithm is symmetric, decryption works exactly the same way.
 
Share this answer
 
Comments
Member 10672244 15-Apr-14 11:13am    
can you provide me with a working code here....if possible...

Thank You.
Bernhard Hiller 16-Apr-14 2:51am    
C'mon, that's your project, isn't it? Start with the information you were provided. If you get stuck somewhere, ask a specific question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900