I am trying to use the CertAndKeyGen in java I tried to import sun.security.x509.CertAndKeyGen.ALL sun package libs showing error

Viewed 273

When I am trying to access the module sun.security.tools.keytool.CertAndKeyGen in Java It showing the error It showing in the hint its showing the import sun.security.tools.keytool cannot be resolved

I know its been removed in the JDK, so I tried to use sun.security.tools.keytool.CertAndKeyGen but all sun giving me the same error cannot be resolved or accessible. I have also tried adding the jar

import java.security.KeyStore;     //importing keystore works fine
import java.security.cert.X509Certificate;   //worksfine
import sun.security.x509.CertAndKeyGen;              //showing the error
import sun.security.x509.X500Name;                    //showing the error

@SuppressWarnings("restriction")
public class X509CertificateGenerator {
    
    private final static int KEYLENGTH = 1024;//2*"512 should be enough"... ;-) 
    private final static int VALIDITYINDAYS = 1095; //3 years adding validating days


    public static void generateKeyFor(KeyStore keystore, String cn,String adminMail,String pwd) throws Exception{

        CertAndKeyGen cakg = new CertAndKeyGen("DSA", "SHAwithDSA");
        cakg.generate(KEYLENGTH);
       
        X500Name name = new X500Name(
                cn, //common name of a person, e.g. "Vivette Davis"
                System.getProperty("user.name"),//organizationUnit - small organization name, e.g. "Purchasing"
                "GreasySpoon",//organizationName - large organization name, e.g. "Onizuka, Inc."
                "Somewhere over",//localityName - locality (city) name, e.g. "Palo Alto"
                "Internet",//stateName - state name, e.g. "California"
                System.getProperty("user.country") //country - two letter country code, e.g. "CH"
                );
        
        X509Certificate certificate = cakg.getSelfCertificate(name,VALIDITYINDAYS*86400);
        certificate.checkValidity();
    
        //Add certificate to keystore
        keystore.setCertificateEntry("gs-ssl", certificate);
        java.security.cert.Certificate[] certs = {certificate};
        keystore.setKeyEntry("gs-ssl", cakg.getPrivateKey(), pwd.toCharArray(),certs);    //setting 
    }
    
}
0 Answers
Related