import java.io.*; import java.security.*; public class KeyHandler { private static String fileName = "key"; public static void main(String[] args) { // Gör publika och privata nycklar och spara dessa till fil KeyPair keyPair = makeKeyPair(); PrivateKey priKey = keyPair.getPrivate(); saveKey(priKey, args[0]); PublicKey pubKey = keyPair.getPublic(); saveKey(pubKey, args[1]); System.exit(1); } private static KeyPair makeKeyPair() { KeyPairGenerator keyGen = null; try { keyGen = KeyPairGenerator.getInstance("DSA", "SUN"); System.out.println("DONE MAKING KEY GENERATOR"); } catch(NoSuchProviderException nspe) { System.out.println("NoSuchProviderException generated"); } catch(NoSuchAlgorithmException nsae) { System.out.println("NoSuchAlgorithmException generated"); } SecureRandom random = null; try { random = SecureRandom.getInstance("SHA1PRNG", "SUN"); } catch(NoSuchProviderException nspe) { System.out.println("NoSuchProviderException generated"); } catch(NoSuchAlgorithmException nsae) { System.out.println("NoSuchAlgorithmException generated"); } keyGen.initialize(1024, random); System.out.println("DONE MAKING KEY GENERATOR INITIALISATION"); KeyPair keyPair = keyGen.generateKeyPair(); System.out.println("DONE MAKING KEY PAIR"); return keyPair; } 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; } }