import java.io.*; import javax.crypto.*; import javax.crypto.spec.*; import java.security.*; import java.security.spec.*; //import com.sun.crypto.provider.SunJCE; //import com.dstc.security.provider.DSTC; public class KeyHandler { static final String algorithm = "Blowfish"; //static final int keyLength = 448; static final int keyLength = 32; public static void main(String[] args) { //Security.addProvider(new SunJCE()); //Security.addProvider(new DSTC()); // Gör hemlig nyckel och spara den till fil SecretKey secretKey = makeSecretKey(); saveKey(secretKey, args[0]); System.exit(1); } private static SecretKey makeSecretKey() { SecretKey secretKey = null; try { // Gör nyckel-generator KeyGenerator keyGen = KeyGenerator.getInstance(algorithm); System.out.println("DONE MAKING KEY GENERATOR"); keyGen.init(keyLength); System.out.println("DONE MAKING KEY GENERATOR INITIALISATION"); // Gör hemlig nyckel secretKey = keyGen.generateKey(); System.out.println("DONE MAKING KEY"); } catch(NoSuchAlgorithmException nsae) { System.out.println("NoSuchAlgorithmException generated"); } return secretKey; } public static void saveKey(Key key, String fileName) { try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName)); oos.writeObject(key); oos.close(); System.out.println("DONE SAVING KEY"); } catch (IOException ioe) { System.out.println("IOException generated"); } } public static Key loadKey(String fileName) { Key key = null; try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName)); key = (Key) ois.readObject(); ois.close(); System.out.println("DONE LOADING KEY"); } catch (Exception ioe) { System.out.println("IOException generated"); } return key; } }