views:

412

answers:

3

For encryption I use something like this:

SecretKey aesKey = KeyGenerator.getInstance("AES").generateKey();
StringEncrypter aesEncrypt = new StringEncrypter(aesKey, aesKey.getAlgorithm());
String aesEncrypted= aesEncrypt.encrypt(StringContent);

If I print out aesKey I get: "javax.crypto.spec.SecretKeySpec@1708d".

So for encryption I would like to ask user for key but dont know how and what format should it be. My plan was something like this:

SecretKey aesKey = javax.crypto.spec.SecretKeySpec@1708d;
StringEncrypter aesEncrypt = new StringEncrypter(aesKey, aesKey.getAlgorithm());
String aesDecrypt = aesEncrypt.decrypt(aesEncrypted);

But seems its not working. Is there some easy way to print out the key after encryption to console so user can save it(or remember it) and then Use for Decryption ?

Whole code is here:http://stackoverflow.com/questions/1918542/symmetric-key-encryption-in-java-reading-from-file-problem So Im sorry for posting again but Im not sure If the code is even readable(I'm newbie).

+2  A: 

I've stored keys in java keystore files. Here's an article that may help you out

http://www.informit.com/articles/article.aspx?p=170967&seqNum=3

BillMan
A: 

Just for reference, the output you're seeing is the result of the default toString method and the funny number on the end is a hash code. See here. Hash codes are by design not reversible, and toString is not necessarily guaranteed to give you enough information to reconstruct the original object (although it does for certain classes).

Dan
A: 
erickson
Thanks for explanation. But How can I call the key instead of genenerating one(KeyGenerator.getInstance("AES").generateKey(); )? I can now print out the key with getEncoded(), user will write it down. But how I call the key back ?
dave91