Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Generating and Saving Keys for AES Encryption.

Reply
Thread Tools

Generating and Saving Keys for AES Encryption.

 
 
Danny
Guest
Posts: n/a
 
      05-26-2007
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(12;

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

 
Reply With Quote
 
 
 
 
Danny
Guest
Posts: n/a
 
      05-27-2007
On May 26, 11:05 am, Danny <(E-Mail Removed)> wrote:
> 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(12;
>
> 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


 
Reply With Quote
 
 
 
 
GodSpeed GodSpeed is offline
Junior Member
Join Date: Dec 2010
Posts: 1
 
      12-26-2010
Can you send me code to save and retreive key please?

Quote:
Originally Posted by Danny
On May 26, 11:05 am, Danny <(E-Mail Removed)> wrote:[color=blue]

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

Thanks anyway,

Danny
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
New AES gem available -- fast-aes Nate Wiger Ruby 3 07-01-2010 04:12 PM
WPA AES & WPA2 AES max Wireless Networking 3 02-14-2007 03:14 PM
How to link foreign keys & primary keys using python? Harry George Python 9 06-13-2006 06:29 AM
serial keys/validation keys sandeep Kanwal C++ 1 10-29-2004 06:36 PM
keyboard keys replacing mouse keys? larry Computer Support 8 09-14-2003 07:32 PM



Advertisments