1 Replies - 1460 Views - Last Post: 09 October 2012 - 04:09 PM Rate Topic: -----

#1 supratroopa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-October 12

User-input seed for AES encryption in Java

Posted 08 October 2012 - 08:08 PM

So I've been coding this program that encrypts a file and then decrypts it, and I want the pseudo-random key generator to take in user input as seed, so that the key can be created from it. Note that I want the key to be string-dependent (i.e.: if I enter the seed "hello" several times, it will encrypt the file with the same key every time), because eventually I will be splitting the encrypt & decrypt functions into two files.

This is my first try, based on SecureRandom. There is more code, but only main is relevant:

protected static final String ALGORITHM = "AES";

public static void main(String args[]) {

    String stringKey = args[1];
    byte[] seedArray = stringKey.getBytes();
    SecureRandom sRandom = new SecureRandom(seedArray);
    byte[] keyArray = new byte[16];
    SecretKey sKey = new SecretKeySpec(keyArray, ALGORITHM);

    try
    {   

        Encrypter2 encrypter = new Encrypter2(sKey);

        FileInputStream efis = new FileInputStream(args[0]);
        FileOutputStream efos = new FileOutputStream("Encrypted");
        encrypter.encrypt(efis, efos);

        FileInputStream dfis = new FileInputStream("Encrypted");
        FileOutputStream dfos = new FileOutputStream("Decrypted.txt");
        encrypter.decrypt(dfis, dfos);

    } catch (FileNotFoundException e1) {
        System.out.println("File not found.");
        e1.printStackTrace();
    } catch (Exception e) {
        System.out.println(e);
    }
}



Now this will create a unique key for a string input in Java 1.7, but for some reason randomizes in Java 1.6. Is there another method of generating a user-seeded key that is

...dependent on the string the user inputs? Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: User-input seed for AES encryption in Java

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,650
  • Joined: 20-September 08

Re: User-input seed for AES encryption in Java

Posted 09 October 2012 - 04:09 PM

Try it like this. Change algo as needed

	SecureRandom r = SecureRandom.getInstance("SHA1PRNG", "SUN");
	r.setSeed(seed);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1