Java 类java.security.interfaces.DSAKeyPairGenerator 实例源码

项目:cagrid2    文件:CertRequest.java   
/** Generate a key pair

         @param type DSA or RSA
         @param size the length
         @param password the password to use to encrypted the key
         @param keyfile the keyfile to store the key in
         @param newParams generate new parameters if using DSA--by default Sun uses fixed precomputed params
         @return the keypair

         @exception NoSuchAlgorithmException if you choose a key we don't know about
         @exception NoSuchProviderException internal errors
         @exception IOException encoding errors
     */
     public static KeyPair generateKey(String type,int size,
       String password,BufferedWriter keyfile,boolean newParams) 
       throws NoSuchAlgorithmException, NoSuchProviderException, IOException {
       PrivateKey priv;

       KeyPairGenerator kg;

       if(type.equals("DSA")){
         kg=KeyPairGenerator.getInstance(type);
         DSAKeyPairGenerator dsaGen=(DSAKeyPairGenerator)kg;
         dsaGen.initialize(size,newParams,new SecureRandom());
       }
       else{
         kg=KeyPairGenerator.getInstance(type,"Cryptix");         
         kg.initialize(size);
       }
       KeyPair pair=kg.generateKeyPair();

       // We need to map the DSA keys given us by Sun into
       // our own DSA keys because Sun gives us a bogus
       // DER encoding
       if(type.equals("DSA")){
     priv=new EAYDSAPrivateKey((DSAPrivateKey)pair.getPrivate());
       }
       else {
     priv=new X509RSAPrivateKey((CryptixRSAPrivateKey)pair.getPrivate());
       }

       EAYEncryptedPrivateKey.writePrivateKey(priv,password.getBytes(),keyfile);
       return pair;
     }