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
Danny - 27 May 2007 13:06 GMT
> I am new to java encryption and would appreciate some guidance.
>
[quoted text clipped - 32 lines]
>
> Danny
Not to worry. I just realized that toString() cannot be used to
display a byte array in any meaningful format.
Thanks anyway,
Danny