x509 - Java generate secure certificate -
how in java generate analogical output of command?
openssl req -x509 -nodes -days 365 -newkey rsa:4096 -keyout server.key -out cert.pem
i next
keypairgenerator gen = keypairgenerator.getinstance(keyalgo); securerandom random = new securerandom(); gen.initialize(keylength, random); keypair = gen.genkeypair(); publickey pubk = keypair.getpublic(); privatekey prvk = keypair.getprivate();
but in result received length public , private keys.
here example:
date validitybegindate = new date(system.currenttimemillis()); date validityenddate = new date(system.currenttimemillis() + 365 * 24 * 60 * 60 * 1000); //1 year keypairgenerator keypairgenerator = keypairgenerator.getinstance("rsa", "bc"); keypairgenerator.initialize(4096, new securerandom()); keypair keypair = keypairgenerator.generatekeypair(); x509v1certificategenerator certgen = new x509v1certificategenerator(); x500principal dnname = new x500principal("cn=john doe"); certgen.setserialnumber(biginteger.valueof(system.currenttimemillis())); certgen.setsubjectdn(dnname); certgen.setissuerdn(dnname); certgen.setnotbefore(validitybegindate); certgen.setnotafter(validityenddate); certgen.setpublickey(keypair.getpublic()); certgen.setsignaturealgorithm("sha256withrsaencryption"); x509certificate cert = certgen.generate(keypair.getprivate(), "bc");
use pemwriter
output pem-file.
Comments
Post a Comment