001package edu.pdx.cs410J.security;
002
003import java.io.*;
004import java.security.*;
005
006/**
007 * This program generates a pair of DSA keys in files named
008 * <code>public.key</code> and <code>private.key</code>
009 */
010public class GenerateKeys {
011  public static void main(String[] args) {
012    KeyPairGenerator gen = null;
013
014    try {
015      gen = KeyPairGenerator.getInstance("DSA");
016    } catch (NoSuchAlgorithmException ex) {
017      ex.printStackTrace(System.err);
018    }
019
020    gen.initialize(1024);
021    KeyPair keys = gen.generateKeyPair();
022
023    PrivateKey privateKey = keys.getPrivate();
024    PublicKey publicKey = keys.getPublic();
025
026    try {
027      FileOutputStream fos = new FileOutputStream ("private.key");
028      fos.write(privateKey.getEncoded());
029
030      fos = new FileOutputStream ("public.key");
031      fos.write(publicKey.getEncoded());
032
033    } catch (IOException ex) {
034      ex.printStackTrace(System.err);
035      System.exit(1);
036    }
037  }
038}