This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes AES Encryption/Decrypton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "AES Encryption/Decrypton" Watch "AES Encryption/Decrypton" New topic
Author

AES Encryption/Decrypton

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

This is the first time I have tried file encryption/decryption (I have some experience with network encryption/decryption). I tried to create a program that would save an AES key (128 bit to test) to a file. Then, a separate program would read this file and convert it back to the string. I suspect the second program has to know some kind of key of the first, but I really don't know what I am doing. Here is the encrypting program with comments at the top :

EncryptTest.java (It is 41 lines, seems to have no errors, and seems to work. It outputs a file named "psswd" with 33 seemingly random characters each time this program is ran)

DecryptTest.java (It is 35 lines, doesn't seems to work, and produces one error)


Error output from "DecryptTest" program :


Thank you,
John Price


“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

I also tried another way. I kicked the padding lines on both the Encryption and Decryption programs (cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");). Here is the new Decryption program that I tried. It gave me a new error :
DecryptTest (35 lines)

Error :
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
A few observations:

It seems that you don't save the key - the code creates a new one for each encryption and decryption. In other words, you will never be able to decrypt.

It also looks like you're running the encrypted data through the asHex method (why? you can store binary data in a file) before saving it in a file, but there's no corresponding decoding being done before decryption.

"new String(original)" - don't do this. Never use the String(byte[]) constructor without specifying the encoding; it's just asking for trouble. Conversely, never use the String.getBytes() method without specifying the encoding.
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

I tried to super simplify it by coming up with a static key. I will eventually make this into a password entered by GUI or command line, which will be different every time. Anyway, I bought the code down to less than 25 lines each. I have to add the type of encryption somewhere (128, 196, or 256), I just don't know where. Thanks for your help!




Here is what I've come to. Everything is equal, it's just not printing out the string. What do I need to convert to to see the string?

Thanks,
John Price
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Instead of CBC, use ECB. And, of course, don't print out the byte array, print out the string. (And, again, don't use "new String(byte[])".)

You can use AES-256 instead of AES-128 by using a 32 byte (= 256 bit) key instead of a 16 byte (= 128 bit) key. AES-192 presumably takes a 24 byte key.
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

Instead of CBC, use ECB. And, of course, don't print out the byte array, print out the string. (And, again, don't use "new String(byte[])".)

1. Could you please tell me how to switch from CBC to ECB?
2. I can't seem to figure out how to print out the value of that byte array. I feel like such a n00b after programming for a year, but I haven't been programming for a few months so please excuse me.

Thanks for your help,
John Price

EDIT : I am now doing getBytes("UTF-8") so that should clear up future problems. In this case, it's still printing out the byte[] location or whatever

EDIT 2 : I am such an idiot. I forgot to print out the string, not the byte array. I see what you are saying now. What should I use to convert it from byte array to string?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
1. Could you please tell me how to switch from CBC to ECB?

Search "CBC", replace by "ECB".

I forgot to print out the string, not the byte array. I see what you are saying now. What should I use to convert it from byte array to string?

? The code already performs the conversion, albeit, as I pointed out, using a constructor that you should not be using. Note also, as I said before, that you should not be using String.getBytes() - which the code does in three places. Here, too, should you specify the encoding.
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

This is what you were saying I was supposed to do (below) ? I didn't look into how to replace it much yet, but this is what I got for those of you who are really wondering how to do it :




Thank you,
John Price
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

Here is my finished product. Thank you guys so much for your help. I will implement a very modified version of this into one of my major projects I have been working on. By the way, EncryptTest could be more efficient, I just took the easy way with the nullifying of args[0]. If you are confused, look at the code below. The programs take command line input such as displayed here :


Here is a live example :


Here is EncryptTest.java and DecryptTest.java :
EncryptTest.java :

DecryptTest.java


Thanks a lot guys!
John Price
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AES Encryption/Decrypton