⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pkcs12example.java

📁 kmlnjlkj nlkjlkjkljl okopokipoipo oipipipo i
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.bouncycastle.jce.examples;import java.io.FileOutputStream;import java.math.BigInteger;import java.security.KeyFactory;import java.security.KeyStore;import java.security.PrivateKey;import java.security.PublicKey;import java.security.Security;import java.security.cert.Certificate;import java.security.cert.X509Certificate;import java.security.spec.RSAPrivateCrtKeySpec;import java.security.spec.RSAPublicKeySpec;import java.util.Date;import java.util.Hashtable;import java.util.Vector;import org.bouncycastle.asn1.DERBMPString;import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;import org.bouncycastle.asn1.x509.BasicConstraints;import org.bouncycastle.asn1.x509.X509Extensions;import org.bouncycastle.jce.PrincipalUtil;import org.bouncycastle.jce.X509Principal;import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier;import org.bouncycastle.jce.provider.BouncyCastleProvider;import org.bouncycastle.x509.X509V1CertificateGenerator;import org.bouncycastle.x509.X509V3CertificateGenerator;import org.bouncycastle.x509.extension.AuthorityKeyIdentifierStructure;import org.bouncycastle.x509.extension.SubjectKeyIdentifierStructure;/** * Example of how to set up a certificiate chain and a PKCS 12 store for * a private individual - obviously you'll need to generate your own keys, * and you may need to add a NetscapeCertType extension or add a key * usage extension depending on your application, but you should get the * idea! As always this is just an example... */public class PKCS12Example{    static char[]   passwd = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' };        static X509V1CertificateGenerator  v1CertGen = new X509V1CertificateGenerator();    static X509V3CertificateGenerator  v3CertGen = new X509V3CertificateGenerator();    /**     * we generate the CA's certificate     */    public static Certificate createMasterCert(        PublicKey       pubKey,        PrivateKey      privKey)        throws Exception    {        //        // signers name         //        String  issuer = "C=AU, O=The Legion of the Bouncy Castle, OU=Bouncy Primary Certificate";        //        // subjects name - the same as we are self signed.        //        String  subject = "C=AU, O=The Legion of the Bouncy Castle, OU=Bouncy Primary Certificate";        //        // create the certificate - version 1        //        v1CertGen.setSerialNumber(BigInteger.valueOf(1));        v1CertGen.setIssuerDN(new X509Principal(issuer));        v1CertGen.setNotBefore(new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30));        v1CertGen.setNotAfter(new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 30)));        v1CertGen.setSubjectDN(new X509Principal(subject));        v1CertGen.setPublicKey(pubKey);        v1CertGen.setSignatureAlgorithm("SHA1WithRSAEncryption");        X509Certificate cert = v1CertGen.generateX509Certificate(privKey);        cert.checkValidity(new Date());        cert.verify(pubKey);        PKCS12BagAttributeCarrier   bagAttr = (PKCS12BagAttributeCarrier)cert;        //        // this is actually optional - but if you want to have control        // over setting the friendly name this is the way to do it...        //        bagAttr.setBagAttribute(            PKCSObjectIdentifiers.pkcs_9_at_friendlyName,            new DERBMPString("Bouncy Primary Certificate"));        return cert;    }    /**     * we generate an intermediate certificate signed by our CA     */    public static Certificate createIntermediateCert(        PublicKey       pubKey,        PrivateKey      caPrivKey,        X509Certificate caCert)        throws Exception    {        //        // subject name table.        //        Hashtable                   attrs = new Hashtable();        Vector                      order = new Vector();        attrs.put(X509Principal.C, "AU");        attrs.put(X509Principal.O, "The Legion of the Bouncy Castle");        attrs.put(X509Principal.OU, "Bouncy Intermediate Certificate");        attrs.put(X509Principal.EmailAddress, "feedback-crypto@bouncycastle.org");        order.addElement(X509Principal.C);        order.addElement(X509Principal.O);        order.addElement(X509Principal.OU);        order.addElement(X509Principal.EmailAddress);        //        // create the certificate - version 3        //        v3CertGen.reset();        v3CertGen.setSerialNumber(BigInteger.valueOf(2));        v3CertGen.setIssuerDN(PrincipalUtil.getSubjectX509Principal(caCert));        v3CertGen.setNotBefore(new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30));        v3CertGen.setNotAfter(new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 30)));        v3CertGen.setSubjectDN(new X509Principal(order, attrs));        v3CertGen.setPublicKey(pubKey);        v3CertGen.setSignatureAlgorithm("SHA1WithRSAEncryption");        //        // extensions        //        v3CertGen.addExtension(            X509Extensions.SubjectKeyIdentifier,            false,            new SubjectKeyIdentifierStructure(pubKey));        v3CertGen.addExtension(            X509Extensions.AuthorityKeyIdentifier,            false,            new AuthorityKeyIdentifierStructure(caCert));        v3CertGen.addExtension(            X509Extensions.BasicConstraints,            true,            new BasicConstraints(0));        X509Certificate cert = v3CertGen.generateX509Certificate(caPrivKey);        cert.checkValidity(new Date());        cert.verify(caCert.getPublicKey());        PKCS12BagAttributeCarrier   bagAttr = (PKCS12BagAttributeCarrier)cert;        //        // this is actually optional - but if you want to have control        // over setting the friendly name this is the way to do it...        //        bagAttr.setBagAttribute(            PKCSObjectIdentifiers.pkcs_9_at_friendlyName,            new DERBMPString("Bouncy Intermediate Certificate"));        return cert;    }    /**     * we generate a certificate signed by our CA's intermediate certficate     */    public static Certificate createCert(        PublicKey       pubKey,        PrivateKey      caPrivKey,        PublicKey       caPubKey)        throws Exception    {        //        // signers name table.        //        Hashtable                   sAttrs = new Hashtable();        Vector                      sOrder = new Vector();        sAttrs.put(X509Principal.C, "AU");        sAttrs.put(X509Principal.O, "The Legion of the Bouncy Castle");        sAttrs.put(X509Principal.OU, "Bouncy Intermediate Certificate");        sAttrs.put(X509Principal.EmailAddress, "feedback-crypto@bouncycastle.org");        sOrder.addElement(X509Principal.C);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -