• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

recreating AES key from txt file

 
Ranch Hand
Posts: 40
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need a help from you. I am implementing AES algorithem. I need to encrypt some files and move them to server, there i need to decrypt them. Problem i am facing is with Key. I need to move the same key to the server. I am planning to write key on the file and read the key at server side. I am unable to recreate the same key from a text file, please help me.

Code i am using is:

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

FileOutputStream fos = new FileOutputStream("c:\\encrypt\\AESKey.txt");
fos.write(key.toString().getBytes());

Please help me recreating the same key from the AESKey.txt file.
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's unlikely that "key.toString()" will do anything useful. (Actually, after just reading the javadocs, I KNOW it doesn't do anything useful :-) Try key.getEncoded() instead.
[ September 17, 2008: Message edited by: Ulf Dittmer ]
 
Bala Raju Mandala
Ranch Hand
Posts: 40
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Key has been write to file using following code:
================================================
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);// 128 bits
SecretKey key =kgen.generateKey();
byte [] data = new byte[key.getEncoded().length];
FileOutputStream fos = new FileOutputStream("c:\\encrypt\\AESKey.txt");
fos.write(key.getEncoded());

Key can be read from file by using following code:
==================================================
byte []keybyte = new byte[16];
FileInputStream fin = new FileInputStream("C:\\encrypt\\AESKey.txt");
fin.read(keybyte);
SecretKey skey = new SecretKeySpec(keybyte, 0, 16, "AES");

Simpler one only, i got confused at key size.

Thank you Ulf for your reply.
[ September 18, 2008: Message edited by: Bala Raju Mandala ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic