This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Security and the fly likes Image Encryption and Decryption using AES Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Security
Bookmark "Image Encryption and Decryption using AES" Watch "Image Encryption and Decryption using AES" New topic
Author

Image Encryption and Decryption using AES

Kasparov Patel
Ranch Hand

Joined: Jan 23, 2012
Posts: 40
Hello,

I am trying to encrypt image considering it as byte data (i.e. without considering the image data structure). and decrypt considering the image structure (image header and image data, etc) using AES. I am able to encrypt, but unfortunatly my decrypt code is not working. Please find my AES.java and GUI.java files as follow:





I would appreciate if anyone can help me.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1035
    
  10

Kasparov Patel wrote:

I would appreciate if anyone can help me.


There is no need to go though all those contortions! AES is one of the standard encryption methods built into Java JCE/JCA.

See -
http://docs.oracle.com/javase/7/docs/technotes/guides/security/crypto/CryptoSpec.html
"Beginning Cryptography with Java" by David Hook published by Wrox.
"Java Cryptography" by Jonathan Knutsen published by O'Reilly (getting old now but still applicable to the JCE as a concept).

P.S. You seem to be padding with zeros. This cannot be right since image files are binary files which might have legitimate trailing zeros which you will strip and corrupt the image. You should use something like PKCS5 padding.
P.P.S Why are you encrypting the buffered image content and not the file content directly. Encrypt the file content and not the buffered image (they are not the same).
P.P.P.S. In your encrypt action listener you have

but you never do anything with the generated 'encryptedImage' !


Edit: Am I mising something ??? You have all that AES algorithm code but then you seem to be using the JCE to do the encryption! Why do you think you need the AES algorithm code?

Though this thread has been moved to the 'Security' section most of the problems seem to be standard Java problems and few if any to do with cryptography.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Kasparov Patel wrote:Please find my AES.java and GUI.java files as follow:

Well, all I can say is that if you can decipher anything from that pile, you're a better man than I am.
I have NEVER seen a chain of 26 method invocations in a row - and I use Builders a lot - and hope I never do again.

About the only thing I can suggest is that encrypting/decrypting an image has absolutely nothing to do with a GUI (except
maybe a simple panel to display what you decrypted to test that it works - although personally I'd just run a checksum on it).

Concentrate on the problem.

Winston

PS: Please DontWriteLongLines.
It makes your thread very hard to read (in your case, incredibly so), and it's actually bad coding practice.
I'm not even going to try and break yours up because I wouldn't know where to start, but for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments AND long (read gigantic) method calls.

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Kasparov Patel
Ranch Hand

Joined: Jan 23, 2012
Posts: 40
Hello Friends,

I am sorry for that. I have posted my both file so you can understand what I am trying to do. I am using my AES.java file to encrypt calling AES.encrypt() and decrypting using AES.decrypt().
my code is working for encryption but not decryption. I tried to figure out but not succeed. I would appreciate if you could point me where exactly I am doing mistake.

Once again I am sorry for inconvenience if any and thanks for your reply.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1035
    
  10

Kasparov Patel wrote:Hello Friends,

I am sorry for that. I have posted my both file so you can understand what I am trying to do. I am using my AES.java file to encrypt calling AES.encrypt() and decrypting using AES.decrypt().
my code is working for encryption but not decryption. I tried to figure out but not succeed. I would appreciate if you could point me where exactly I am doing mistake.

Once again I am sorry for inconvenience if any and thanks for your reply.


You do not seem to have read my response so I will summarise it :-

1) I don't see how the encryption can be working since, as I have already pointed out, you don't do anything with the ciphertext!
2) You should encrypt the file content and not the buffered image content.
3) Your approach to padding will corrupt your data. Use PKCS5 padding and not zero padding.
4) Why you are using your AES code and not that available in the JCE/JCA . Use the inbuilt AES encryption.

In your position I would separate out the encryption code from the GUI into a class that encrypts and decrypts a file. Create a test harness to test this new class outside of the GUI .





Kasparov Patel
Ranch Hand

Joined: Jan 23, 2012
Posts: 40
Hello Richard,

Thanks for your valuable suggestion. I will follow it. Thanks once again.
 
 
subject: Image Encryption and Decryption using AES
 
Similar Threads
Problem With GUI of Net Beans
java programming
iperf
Line Spacing in jTextPane
How to add JTable into a JPanel