Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Generating and Saving Keys for AES Encryption.

Thread view: 
Danny - 26 May 2007 16:05 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.