Generating and Saving Keys for AES Encryption.

D

Danny

I am new to java encryption and would appreciate some guidance.

Consider the following piece of a java program:

KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);

SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
System.out.println("raw skey: " + raw.toString());

SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

byte[] encrypted =
cipher.doFinal("This is just an example".getBytes());
System.out.println("encrypted string: " + asHex(encrypted));

I want to save the encryption key (to a file or database) for later
decryption. My understanding is that the generated key is generated
using SecureRandom. The "raw skey" string remains the same for
successive runs of the above code; it does not seem random. However,
the "encrypted string" does change, implying that a different key was
generated.

Questions:

1. Is a new key generated each run? If so, why does "raw skey" not
change?
2. After satisfying 1 above, is "SecretKeySpec skeySpec" what I save
for later decryption?

Thanks for your help,

Danny
 
D

Danny

I am new to java encryption and would appreciate some guidance.

Consider the following piece of a java program:

KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);

SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
System.out.println("raw skey: " + raw.toString());

SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

byte[] encrypted =
cipher.doFinal("This is just an example".getBytes());
System.out.println("encrypted string: " + asHex(encrypted));

I want to save the encryption key (to a file or database) for later
decryption. My understanding is that the generated key is generated
using SecureRandom. The "raw skey" string remains the same for
successive runs of the above code; it does not seem random. However,
the "encrypted string" does change, implying that a different key was
generated.

Questions:

1. Is a new key generated each run? If so, why does "raw skey" not
change?
2. After satisfying 1 above, is "SecretKeySpec skeySpec" what I save
for later decryption?

Thanks for your help,

Danny

Not to worry. I just realized that toString() cannot be used to
display a byte array in any meaningful format.

Thanks anyway,

Danny
 
Joined
Dec 26, 2010
Messages
1
Reaction score
0
favor

Can you send me code to save and retreive key please?

Danny said:
On May 26, 11:05 am, Danny <[email protected]> wrote:

Not to worry. I just realized that toString() cannot be used to
display a byte array in any meaningful format.

Thanks anyway,

Danny
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top