Export DSAPrivateKeySpec : Digital Signature Algorithm « Security « Java Tutorial






import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.spec.DSAPrivateKeySpec;

public class MainClass {
  public static void main(String args[]) throws Exception {
    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);
    oos.writeObject(ks.getX());
    oos.writeObject(ks.getP());
    oos.writeObject(ks.getQ());
    oos.writeObject(ks.getG());
  }
}








36.16.Digital Signature Algorithm
36.16.1.Use DSA to Sign
36.16.2.Signed Object
36.16.3.Creating a Signature
36.16.4.Verifies the signature for the given buffer of bytes using the public key.
36.16.5.Digital Signature Algorithm Demo
36.16.6.DSA with Elliptic Curve
36.16.7.DSA Key Translator
36.16.8.Use DSAPrivateKeySpec
36.16.9.Getting the Digital Signature Algorithm (DSA) Parameters of a Key Pair
36.16.10.Create the DSA key factory from a Set of Digital Signature Algorithm (DSA) Parameters
36.16.11.Create the DSA private key from a Set of Digital Signature Algorithm (DSA) Parameters
36.16.12.Create the DSA public key from a Set of Digital Signature Algorithm (DSA) Parameters
36.16.13.Export DSAPrivateKeySpec
36.16.14.Signing a Java Object