• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

AES Encryption/Decrypton

 
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :
 
Saloon Keeper
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
today's feeble attempt to support the empire
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic