Generating and Saving Keys for AES Encryption.

Discussion in 'Java' started by Danny, May 26, 2007.

  1. Danny

    Danny Guest

    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, May 26, 2007
    #1
    1. Advertising

  2. Danny

    Danny Guest

    On May 26, 11:05 am, Danny <> 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(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
     
    Danny, May 27, 2007
    #2
    1. Advertising

  3. Danny

    GodSpeed

    Joined:
    Dec 26, 2010
    Messages:
    1
    favor

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

     
    GodSpeed, Dec 26, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lars J.
    Replies:
    0
    Views:
    488
    Lars J.
    Jul 31, 2005
  2. Chris Curvey

    modes for AES encryption?

    Chris Curvey, Mar 24, 2005, in forum: Python
    Replies:
    1
    Views:
    388
    Chris Curvey
    Mar 29, 2005
  3. Tobiah
    Replies:
    3
    Views:
    476
    Paul Rubin
    Jan 23, 2007
  4. Anth
    Replies:
    6
    Views:
    250
    Brian Candler
    Sep 2, 2009
  5. Nate Wiger

    New AES gem available -- fast-aes

    Nate Wiger, Jan 20, 2010, in forum: Ruby
    Replies:
    3
    Views:
    230
    Pål Bergström
    Jul 1, 2010
Loading...

Share This Page