当前位置: 首页>>代码示例>>Java>>正文


Java X509v1CertificateBuilder类代码示例

本文整理汇总了Java中org.bouncycastle.cert.X509v1CertificateBuilder的典型用法代码示例。如果您正苦于以下问题:Java X509v1CertificateBuilder类的具体用法?Java X509v1CertificateBuilder怎么用?Java X509v1CertificateBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


X509v1CertificateBuilder类属于org.bouncycastle.cert包,在下文中一共展示了X509v1CertificateBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: generateCertificate

import org.bouncycastle.cert.X509v1CertificateBuilder; //导入依赖的package包/类
/**
 * Create a self-signed X.509 Certificate.
 * From http://bfo.com/blog/2011/03/08/odds_and_ends_creating_a_new_x_509_certificate.html.
 *
 * @param dn        the X.509 Distinguished Name, eg "CN=Test, L=London, C=GB"
 * @param pair      the KeyPair
 * @param days      how many days from now the Certificate is valid for
 * @param algorithm the signing algorithm, eg "SHA1withRSA"
 * @return the self-signed certificate
 * @throws CertificateException thrown if a security error or an IO error occurred.
 */
public static X509Certificate generateCertificate(String dn, KeyPair pair,
                                                  int days, String algorithm)
    throws CertificateException {

  try {
    Security.addProvider(new BouncyCastleProvider());
    AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find(algorithm);
    AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
    AsymmetricKeyParameter privateKeyAsymKeyParam = PrivateKeyFactory.createKey(pair.getPrivate().getEncoded());
    SubjectPublicKeyInfo subPubKeyInfo = SubjectPublicKeyInfo.getInstance(pair.getPublic().getEncoded());
    ContentSigner sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId).build(privateKeyAsymKeyParam);
    X500Name name = new X500Name(dn);
    Date from = new Date();
    Date to = new Date(from.getTime() + days * 86400000L);
    BigInteger sn = new BigInteger(64, new SecureRandom());

    X509v1CertificateBuilder v1CertGen = new X509v1CertificateBuilder(name, sn, from, to, name, subPubKeyInfo);
    X509CertificateHolder certificateHolder = v1CertGen.build(sigGen);
    return new JcaX509CertificateConverter().setProvider("BC").getCertificate(certificateHolder);
  } catch (CertificateException ce) {
    throw ce;
  } catch (Exception e) {
    throw new CertificateException(e);
  }
}
 
开发者ID:becketqin,项目名称:likafka-clients,代码行数:37,代码来源:TestSslUtils.java

示例2: generateCertificate

import org.bouncycastle.cert.X509v1CertificateBuilder; //导入依赖的package包/类
/**
 * Create a self-signed X.509 Certificate.
 * From http://bfo.com/blog/2011/03/08/odds_and_ends_creating_a_new_x_509_certificate.html.
 *
 * @param dn the X.509 Distinguished Name, eg "CN=Test, L=London, C=GB"
 * @param pair the KeyPair
 * @param days how many days from now the Certificate is valid for
 * @param algorithm the signing algorithm, eg "SHA1withRSA"
 * @return the self-signed certificate
 * @throws CertificateException thrown if a security error or an IO error occurred.
 */
public static X509Certificate generateCertificate(String dn, KeyPair pair,
                                                  int days, String algorithm)
    throws  CertificateException {

    try {
        Security.addProvider(new BouncyCastleProvider());
        AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find(algorithm);
        AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
        AsymmetricKeyParameter privateKeyAsymKeyParam = PrivateKeyFactory.createKey(pair.getPrivate().getEncoded());
        SubjectPublicKeyInfo subPubKeyInfo = SubjectPublicKeyInfo.getInstance(pair.getPublic().getEncoded());
        ContentSigner sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId).build(privateKeyAsymKeyParam);
        X500Name name = new X500Name(dn);
        Date from = new Date();
        Date to = new Date(from.getTime() + days * 86400000L);
        BigInteger sn = new BigInteger(64, new SecureRandom());

        X509v1CertificateBuilder v1CertGen = new X509v1CertificateBuilder(name, sn, from, to, name, subPubKeyInfo);
        X509CertificateHolder certificateHolder = v1CertGen.build(sigGen);
        return new JcaX509CertificateConverter().setProvider("BC").getCertificate(certificateHolder);
    } catch (CertificateException ce) {
        throw ce;
    } catch (Exception e) {
        throw new CertificateException(e);
    }
}
 
开发者ID:txazo,项目名称:kafka,代码行数:37,代码来源:TestSslUtils.java

示例3: makeV1Certificate

import org.bouncycastle.cert.X509v1CertificateBuilder; //导入依赖的package包/类
public static X509CertificateHolder makeV1Certificate(AsymmetricCipherKeyPair subKP, String _subDN, AsymmetricCipherKeyPair issKP, String _issDN)
    throws IOException, OperatorCreationException
{
    RSAKeyParameters lwPubKey = (RSAKeyParameters)subKP.getPublic();

    X509v1CertificateBuilder v1CertGen = new X509v1CertificateBuilder(
        new X500Name(_issDN),
        allocateSerialNumber(),
        new Date(System.currentTimeMillis()),
        new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 100)),
        new X500Name(_subDN),
        new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new RSAPublicKey(lwPubKey.getModulus(), lwPubKey.getExponent()))
    );

    AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA1WithRSAEncryption");
    AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);

    ContentSigner sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId).build((AsymmetricKeyParameter)issKP.getPrivate());


    return v1CertGen.build(sigGen);
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:23,代码来源:CMSTestUtil.java

示例4: getCertificate

import org.bouncycastle.cert.X509v1CertificateBuilder; //导入依赖的package包/类
public static X509Certificate getCertificate(Credential cred) throws CertificateException, NoSuchAlgorithmException, SignatureException, InvalidKeyException, IOException, OperatorCreationException {
       X500Name issuer = new X500Name("C=DK, O=test, OU=test");
       BigInteger serial = BigInteger.valueOf(34234);
       Date notBefore = new Date(System.currentTimeMillis() - 10000);
       Date notAfter = new Date(System.currentTimeMillis() + 100000L);
       X500Name subject = new X500Name("C=DK, O=test, OU=test");

       ByteArrayInputStream bIn = new ByteArrayInputStream(cred.getPublicKey().getEncoded());
       SubjectPublicKeyInfo publicKeyInfo = new SubjectPublicKeyInfo((ASN1Sequence)new ASN1InputStream(bIn).readObject());

       X509v1CertificateBuilder gen = new X509v1CertificateBuilder(issuer, serial, notBefore, notAfter, subject, publicKeyInfo);

       ContentSigner sigGen = new JcaContentSignerBuilder("SHA1withRSA").setProvider("BC").build(cred.getPrivateKey());
       X509CertificateHolder certificateHolder = gen.build(sigGen);

       X509Certificate x509Certificate = new JcaX509CertificateConverter().setProvider("BC").getCertificate(certificateHolder);
       return x509Certificate;
}
 
开发者ID:amagdenko,项目名称:oiosaml.java,代码行数:19,代码来源:TestHelper.java

示例5: _createCert

import org.bouncycastle.cert.X509v1CertificateBuilder; //导入依赖的package包/类
/**
 * Create a new dummy certificate based on the passed key pair
 *
 * @param kp
 *        KeyPair to use. May not be <code>null</code>.
 * @return A {@link X509Certificate} for further usage
 */
@Nonnull
private X509Certificate _createCert (@Nonnull final KeyPair kp) throws Exception
{
  final PublicKey aPublicKey = kp.getPublic ();
  final PrivateKey aPrivateKey = kp.getPrivate ();
  final ContentSigner aContentSigner = new JcaContentSignerBuilder ("SHA1withRSA").setProvider (BouncyCastleProvider.PROVIDER_NAME)
                                                                                  .build (aPrivateKey);

  // Form yesterday
  final Date aStartDate = new Date (System.currentTimeMillis () - 24 * CGlobal.MILLISECONDS_PER_HOUR);
  // For one year from now
  final Date aEndDate = new Date (System.currentTimeMillis () + 365 * 24 * CGlobal.MILLISECONDS_PER_HOUR);

  final X509v1CertificateBuilder aCertBuilder = new JcaX509v1CertificateBuilder (new X500Principal ("CN=TestIssuer"),
                                                                                 BigInteger.ONE,
                                                                                 aStartDate,
                                                                                 aEndDate,
                                                                                 new X500Principal ("CN=TestSubject"),
                                                                                 aPublicKey);
  final X509CertificateHolder aCertHolder = aCertBuilder.build (aContentSigner);
  // Convert to JCA X509Certificate
  return new JcaX509CertificateConverter ().getCertificate (aCertHolder);
}
 
开发者ID:phax,项目名称:ph-xmldsig,代码行数:31,代码来源:XMLDSigCreatorTest.java

示例6: generateV1Certificate

import org.bouncycastle.cert.X509v1CertificateBuilder; //导入依赖的package包/类
public static X509Certificate generateV1Certificate(final KeyPair keyPair,
                                                    final String subject,
                                                    final String issuer,
                                                    final int validityYears,
                                                    final String signatureAlgorithm) throws CertificateException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException, InvalidKeyException, OperatorCreationException {
    final DateTime now = DateTime.now(DateTimeZone.UTC);
    final X509v1CertificateBuilder certBuilder = new JcaX509v1CertificateBuilder(
            new X500Name(issuer),
            generateRandomBigInt(),
            now.minusDays(1).toDate(),
            now.plusYears(validityYears).toDate(),
            new X500Name(subject),
            keyPair.getPublic());
    final ContentSigner signer = new JcaContentSignerBuilder(signatureAlgorithm).setProvider("BC").build(keyPair.getPrivate());
    final X509CertificateHolder certHolder = certBuilder.build(signer);
    return new JcaX509CertificateConverter().setProvider("BC").getCertificate(certHolder);
}
 
开发者ID:apache,项目名称:cloudstack,代码行数:18,代码来源:CertUtils.java

示例7: makeV1Certificate

import org.bouncycastle.cert.X509v1CertificateBuilder; //导入依赖的package包/类
public static X509Certificate makeV1Certificate(KeyPair subKP, String _subDN, KeyPair issKP, String _issDN)
    throws GeneralSecurityException, IOException, OperatorCreationException
{

    PublicKey  subPub  = subKP.getPublic();
    PrivateKey issPriv = issKP.getPrivate();
    PublicKey  issPub  = issKP.getPublic();

    X509v1CertificateBuilder v1CertGen = new JcaX509v1CertificateBuilder(
        new X500Name(_issDN),
        allocateSerialNumber(),
        new Date(System.currentTimeMillis()),
        new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 100)),
        new X500Name(_subDN),
        subPub);

    JcaContentSignerBuilder contentSignerBuilder = makeContentSignerBuilder(issPub);

    X509Certificate _cert = new JcaX509CertificateConverter().setProvider("BC").getCertificate(v1CertGen.build(contentSignerBuilder.build(issPriv)));

    _cert.checkValidity(new Date());
    _cert.verify(issPub);

    return _cert;
}
 
开发者ID:credentials,项目名称:irma_future_id,代码行数:26,代码来源:CMSTestUtil.java

示例8: generateCertificate

import org.bouncycastle.cert.X509v1CertificateBuilder; //导入依赖的package包/类
/**
 * Create a self-signed X.509 Certificate.
 * From http://bfo.com/blog/2011/03/08/odds_and_ends_creating_a_new_x_509_certificate.html.
 *
 * @param dn the X.509 Distinguished Name, eg "CN(commonName)=Test, O(organizationName)=Org"
 * @param pair the KeyPair
 * @param days how many days from now the Certificate is valid for
 * @param algorithm the signing algorithm, eg "SHA1withRSA"
 * @return the self-signed certificate
 * @throws java.security.cert.CertificateException thrown if a security error or an IO error ocurred.
 */
public static X509Certificate generateCertificate(String dn, KeyPair pair, int days, String algorithm)
    throws CertificateException {
  try {
    Security.addProvider(new BouncyCastleProvider());
    AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find(algorithm);
    AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
    AsymmetricKeyParameter privateKeyAsymKeyParam = PrivateKeyFactory.createKey(pair.getPrivate().getEncoded());
    SubjectPublicKeyInfo subPubKeyInfo = SubjectPublicKeyInfo.getInstance(pair.getPublic().getEncoded());
    ContentSigner sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId).build(privateKeyAsymKeyParam);
    X500Name name = new X500Name(dn);
    Date from = new Date();
    Date to = new Date(from.getTime() + days * 86400000L);
    BigInteger sn = new BigInteger(64, new SecureRandom());

    X509v1CertificateBuilder v1CertGen = new X509v1CertificateBuilder(name, sn, from, to, name, subPubKeyInfo);
    X509CertificateHolder certificateHolder = v1CertGen.build(sigGen);
    return new JcaX509CertificateConverter().setProvider("BC").getCertificate(certificateHolder);
  } catch (CertificateException ce) {
    throw ce;
  } catch (Exception e) {
    throw new CertificateException(e);
  }
}
 
开发者ID:linkedin,项目名称:ambry,代码行数:35,代码来源:TestSSLUtils.java

示例9: generateTestCertificate

import org.bouncycastle.cert.X509v1CertificateBuilder; //导入依赖的package包/类
public static X509Certificate generateTestCertificate(KeyPair pair) throws CertificateException, OperatorCreationException {
    final X500NameBuilder nameBuilder = new X500NameBuilder(BCStyle.INSTANCE);
    final X500Name cn = nameBuilder.addRDN(BCStyle.CN, "Test Certificate").build();

    final byte[] encoded = pair.getPublic().getEncoded();
    final SubjectPublicKeyInfo subjectPublicKeyInfo = new SubjectPublicKeyInfo(ASN1Sequence.getInstance(encoded));

    final X509v1CertificateBuilder certBuilder = new X509v1CertificateBuilder(
            cn,
            BigInteger.valueOf(System.currentTimeMillis()),
            new Date(System.currentTimeMillis() - 10000),
            new Date(System.currentTimeMillis() + 10000),
            cn,
            subjectPublicKeyInfo
    );

    final JcaContentSignerBuilder contentSignerBuilder = new JcaContentSignerBuilder("SHA256WithRSAEncryption");
    final ContentSigner contentSigner = contentSignerBuilder.build(pair.getPrivate());
    final X509CertificateHolder certificateHolder = certBuilder.build(contentSigner);

    return new JcaX509CertificateConverter().setProvider( "BC" ).getCertificate(certificateHolder);
}
 
开发者ID:gini,项目名称:jersey-smime,代码行数:23,代码来源:KeyTools.java

示例10: generateKeyAndCertificate

import org.bouncycastle.cert.X509v1CertificateBuilder; //导入依赖的package包/类
private static Pair<PrivateKey, X509Certificate> generateKeyAndCertificate(String asymmetric, String sign, int validityYears, String dn) throws NoSuchAlgorithmException, OperatorCreationException, CertificateException {
    Preconditions.checkArgument(validityYears > 0, "validityYears <= 0");
    KeyPair keyPair = KeyPairGenerator.getInstance(asymmetric).generateKeyPair();
    Date notBefore = new Date(System.currentTimeMillis());
    Date notAfter = new Date(System.currentTimeMillis() + validityYears * 31536000000l);
    X500Name issuer = new X500Name(new X500Principal(dn).getName());
    SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded());
    X509v1CertificateBuilder builder = new X509v1CertificateBuilder(issuer, BigInteger.ONE, notBefore, notAfter, issuer, publicKeyInfo);
    ContentSigner signer = new JcaContentSignerBuilder(sign).setProvider(new BouncyCastleProvider()).build(keyPair.getPrivate());
    X509CertificateHolder holder = builder.build(signer);
    JcaX509CertificateConverter converter = new JcaX509CertificateConverter().setProvider(new BouncyCastleProvider());
    X509Certificate certificate = converter.getCertificate(holder);
    return Pair.of(keyPair.getPrivate(), certificate);
}
 
开发者ID:NBANDROIDTEAM,项目名称:NBANDROID-V2,代码行数:15,代码来源:ApkUtils.java

示例11: main

import org.bouncycastle.cert.X509v1CertificateBuilder; //导入依赖的package包/类
public static void main(String[] args) throws IOException, OperatorCreationException, NoSuchAlgorithmException {

		// ---------------------- CA Creation ----------------------
		// System.out.println("Generating Keys");
		KeyPairGenerator rsa = KeyPairGenerator.getInstance("RSA");
		rsa.initialize(1024);
		KeyPair kp = rsa.generateKeyPair();

		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.YEAR, 100);

		// System.out.println("Getting data");
		byte[] pk = kp.getPublic().getEncoded();
		SubjectPublicKeyInfo bcPk = SubjectPublicKeyInfo.getInstance(pk);

		// System.out.println("Creating cert");
		X509v1CertificateBuilder certGen = new X509v1CertificateBuilder(new X500Name("CN=CA Cert"), BigInteger.ONE,
				new Date(), cal.getTime(), new X500Name("CN=CA Cert"), bcPk);

		X509CertificateHolder certHolder = certGen
				.build(new JcaContentSignerBuilder("SHA1withRSA").build(kp.getPrivate()));

		StringBuffer s = new StringBuffer();

		s.append(X509Factory.BEGIN_CERT + "\n");
		s.append(Base64Utils.base64Encode(certHolder.getEncoded()) + "\n");
		s.append(X509Factory.END_CERT);

		saveFile(s.toString().getBytes());

		// ---------------------- ISSUER Creation ----------------------

	}
 
开发者ID:demoiselle,项目名称:signer,代码行数:34,代码来源:CreateCA.java

示例12: generateCertificate

import org.bouncycastle.cert.X509v1CertificateBuilder; //导入依赖的package包/类
public static X509Certificate generateCertificate(KeyPair keyPair) {
	try {
		ContentSigner sigGen = new JcaContentSignerBuilder(AuthenticationUtils.ALGORITHM)
				.build(keyPair.getPrivate());
		Date startDate = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
		Date endDate = new Date(System.currentTimeMillis() + 365 * 24 * 60 * 60 * 1000);
		X509v1CertificateBuilder v1CertGen = new JcaX509v1CertificateBuilder(new X500Principal("CN=User"),
				BigInteger.ONE, startDate, endDate, new X500Principal("CN=User"), keyPair.getPublic());
		X509CertificateHolder certHolder = v1CertGen.build(sigGen);
		return new JcaX509CertificateConverter().getCertificate(certHolder);
	} catch (OperatorCreationException | CertificateException ex) {
		throw new RuntimeException(ex);
	}
}
 
开发者ID:shilongdai,项目名称:LSChatServer,代码行数:15,代码来源:TestUtils.java

示例13: generateX509CertificateV1

import org.bouncycastle.cert.X509v1CertificateBuilder; //导入依赖的package包/类
@Override
public X509Certificate generateX509CertificateV1(@NonNull final X509V1CertRequest request, @NonNull final PrivateKey privateKey) {
    final ContentSigner signer = contentSigner(privateKey);
    final X509v1CertificateBuilder certBuilder = request.x509v1CertificateBuilder();
    final X509CertificateHolder certHolder = certBuilder.build(signer);
    return toX509Certificate(certHolder);
}
 
开发者ID:runrightfast,项目名称:runrightfast-vertx,代码行数:8,代码来源:CertificateServiceImpl.java

示例14: x509v1CertificateBuilder

import org.bouncycastle.cert.X509v1CertificateBuilder; //导入依赖的package包/类
public X509v1CertificateBuilder x509v1CertificateBuilder() {
    return new JcaX509v1CertificateBuilder(
            issuerPrincipal,
            serialNumber,
            Date.from(notBefore),
            Date.from(notAfter),
            subjectPrincipal,
            subjectPublicKey
    );
}
 
开发者ID:runrightfast,项目名称:runrightfast-vertx,代码行数:11,代码来源:X509V1CertRequest.java

示例15: createMasterCert

import org.bouncycastle.cert.X509v1CertificateBuilder; //导入依赖的package包/类
/**
 * we generate the CA's certificate
 */
public static X509Certificate 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
    //
    X509v1CertificateBuilder v1CertBuilder = new JcaX509v1CertificateBuilder(
        new X500Name(issuer),
        BigInteger.valueOf(1),
        new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30),
        new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 30)),
        new X500Name(subject),
        pubKey);

    X509CertificateHolder cert = v1CertBuilder.build(new JcaContentSignerBuilder("SHA1withRSA").setProvider(BC).build(privKey));

    return new JcaX509CertificateConverter().setProvider(BC).getCertificate(cert);
}
 
开发者ID:credentials,项目名称:irma_future_id,代码行数:34,代码来源:PfxPduTest.java


注:本文中的org.bouncycastle.cert.X509v1CertificateBuilder类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。