try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
kpg.initialize(512, new SecureRandom());
KeyPair kp = kpg.generateKeyPair();
Class spec = Class.forName("java.security.spec.DSAPrivateKeySpec");
KeyFactory kf = KeyFactory.getInstance("DSA");
DSAPrivateKeySpec ks = (DSAPrivateKeySpec) kf.getKeySpec(kp.getPrivate(), spec);
FileOutputStream fos = new FileOutputStream("exportedKey");
ObjectOutputStream oos = new ObjectOutputStream(fos);
System.out.println("provider " + kf.getProvider());
oos.writeObject(ks.getX());
oos.writeObject(ks.getP());
oos.writeObject(ks.getQ());
oos.writeObject(ks.getG());
} catch (Exception e) {
e.printStackTrace();
}