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


Java PEMWriter.close方法代码示例

本文整理汇总了Java中org.bouncycastle.openssl.PEMWriter.close方法的典型用法代码示例。如果您正苦于以下问题:Java PEMWriter.close方法的具体用法?Java PEMWriter.close怎么用?Java PEMWriter.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.bouncycastle.openssl.PEMWriter的用法示例。


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

示例1: saveKeyPairAndCertificateToFile

import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
protected void saveKeyPairAndCertificateToFile() throws Exception {
    if(localPrivateKeyFile==null){
      LOGGER.info("not saving private key nor certificate");
      return;
    }
    //Encode in PEM format, the format prefered by openssl
//    if(false){
//      PEMWriter pemWriter=new PEMWriter(new FileWriter(localPrivateKeyFile));
//      pemWriter.writeObject(localPrivateECKey);
//      pemWriter.close();
//    }
//    else{
      String keyText = "-----BEGIN EC PRIVATE KEY-----\n" +
          Base64.encode(Unpooled.wrappedBuffer(localPrivateECKey.getEncoded()), true).toString(CharsetUtil.US_ASCII) +
          "\n-----END EC PRIVATE KEY-----\n";
      Files.write(keyText, localPrivateKeyFile, CharsetUtil.US_ASCII);

      Files.write(localId.toString(), new File(localPrivateKeyFile.getParentFile(), "localPublic.hash"), CharsetUtil.US_ASCII);
//    }

    PEMWriter certificateWriter=new PEMWriter(new FileWriter(localCertificateFile));
    certificateWriter.writeObject(cert);
    certificateWriter.close();
    LOGGER.info("Saved to "+localCertificateFile.getAbsolutePath());
  }
 
开发者ID:pmarches,项目名称:peercentrum-core,代码行数:26,代码来源:NodeIdentity.java

示例2: encryptedTest

import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
private void encryptedTest(PrivateKey key, ASN1ObjectIdentifier algorithm)
    throws NoSuchProviderException, NoSuchAlgorithmException, IOException
{
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    PEMWriter pWrt = new PEMWriter(new OutputStreamWriter(bOut), "BC");
    PKCS8Generator pkcs8 = new PKCS8Generator(key, algorithm, "BC");

    pkcs8.setPassword("hello".toCharArray());
    
    pWrt.writeObject(pkcs8);

    pWrt.close();

    PEMReader pRd = new PEMReader(new InputStreamReader(new ByteArrayInputStream(bOut.toByteArray())), new PasswordFinder()
    {
        public char[] getPassword()
        {
            return "hello".toCharArray();
        }
    });

    PrivateKey rdKey = (PrivateKey)pRd.readObject();

    assertEquals(key, rdKey);
}
 
开发者ID:credentials,项目名称:irma_future_id,代码行数:26,代码来源:AllTests.java

示例3: getPEMStringFromRSAKeyPair

import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
/**
 * Converts RSA private key to PEM string.
 * 
 * @param rsaKeyPair
 * 
 * @return PEM string
 */
public static String getPEMStringFromRSAKeyPair(RSAKeyPair rsaKeyPair) {
    StringWriter pemStrWriter = new StringWriter();
    PEMWriter pemWriter = new PEMWriter(pemStrWriter);
    try {
        KeyPair keyPair = new KeyPair(rsaKeyPair.getPublic(), rsaKeyPair.getPrivate());
        //pemWriter.writeObject(keyPair);
        pemWriter.writeObject(keyPair.getPrivate());
        //pemWriter.flush();
        pemWriter.close();

    } catch (IOException e) {
        log.warning("Caught exception:" + e.getMessage());
        return "";
    }

    return pemStrWriter.toString();
}
 
开发者ID:sirvaliance,项目名称:netlib,代码行数:25,代码来源:Encryption.java

示例4: writeCertificate

import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
private void writeCertificate(Certificate... certificates)
        throws IOException {
    final PEMWriter writer = new PEMWriter(new FileWriter(destfile));
    for (final Certificate c : certificates) {
        writer.writeObject(c);
    }
    writer.close();
}
 
开发者ID:servicecatalog,项目名称:oscm,代码行数:9,代码来源:SignTask.java

示例5: toPem

import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
private static String toPem(X509Certificate certificate) throws IOException {
	StringWriter stringWriter = new StringWriter();
	PEMWriter pemWriter = new PEMWriter(stringWriter, BouncyCastleProvider.PROVIDER_NAME);
	pemWriter.writeObject(certificate);
	pemWriter.close();
	return stringWriter.toString();
}
 
开发者ID:e-Contract,项目名称:eid-applet,代码行数:8,代码来源:JSONServlet.java

示例6: keyPairToString

import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
private String keyPairToString(KeyPair keyPair) {
	StringWriter stringWriter = new StringWriter();
	PEMWriter pemWriter = new PEMWriter(stringWriter);
	try {
		pemWriter.writeObject(keyPair);
		pemWriter.flush();
		pemWriter.close();
	} catch (IOException e) {
		throw new RuntimeException("Unexpected IOException: "
				+ e.getMessage(), e);
	}
	return stringWriter.getBuffer().toString();
}
 
开发者ID:litesolutions,项目名称:scm-ssh-plugin,代码行数:14,代码来源:ScmKeyPairProvider.java

示例7: getInPemFormat

import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
private static String getInPemFormat(PrivateKey privateKey)
        throws IOException {
  final StringWriter stringWriter = new StringWriter();
  final PEMWriter pemWriter = new PEMWriter(stringWriter);
  pemWriter.writeObject(privateKey);
  pemWriter.flush();
  pemWriter.close();
  return stringWriter.toString();
}
 
开发者ID:jenkinsci,项目名称:google-oauth-plugin,代码行数:10,代码来源:JsonServiceAccountConfigTestUtil.java

示例8: getPemBytes

import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
public byte[] getPemBytes(final Object o) throws IOException {
    LOG.debug(String.format("getPemBytes(%s)", o));
    ByteArrayOutputStream pemByteOut = new ByteArrayOutputStream();
    PEMWriter pemOut = new PEMWriter(new OutputStreamWriter(pemByteOut));
    pemOut.writeObject(o);
    pemOut.close();
    return pemByteOut.toByteArray();
}
 
开发者ID:barnyard,项目名称:pi,代码行数:9,代码来源:SecurityUtils.java

示例9: doWriteReadTest

import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
private void doWriteReadTest(
    PrivateKey  akp,
    String      provider)
    throws IOException
{
    StringWriter sw = new StringWriter();
    PEMWriter pw = new PEMWriter(sw, provider);

    pw.writeObject(akp);
    pw.close();

    String data = sw.toString();

    PEMReader pr = new PEMReader(new StringReader(data));

    Object o = pr.readObject();

    if (o == null || !(o instanceof KeyPair))
    {
        fail("Didn't find OpenSSL key");
    }

    KeyPair kp = (KeyPair) o;
    PrivateKey privKey = kp.getPrivate();

    if (!akp.equals(privKey))
    {
        fail("Failed to read back test");
    }
}
 
开发者ID:credentials,项目名称:irma_future_id,代码行数:31,代码来源:WriterTest.java

示例10: encryptedTestNew

import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
private void encryptedTestNew(PrivateKey key, ASN1ObjectIdentifier algorithm)
    throws NoSuchProviderException, NoSuchAlgorithmException, IOException, OperatorCreationException
{
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    PEMWriter pWrt = new PEMWriter(new OutputStreamWriter(bOut), "BC");

    JceOpenSSLPKCS8EncryptorBuilder encryptorBuilder = new JceOpenSSLPKCS8EncryptorBuilder(algorithm);

    encryptorBuilder.setProvider("BC");
    encryptorBuilder.setPasssword("hello".toCharArray());

    PKCS8Generator pkcs8 = new JcaPKCS8Generator(key, encryptorBuilder.build());

    pWrt.writeObject(pkcs8);

    pWrt.close();

    PEMReader pRd = new PEMReader(new InputStreamReader(new ByteArrayInputStream(bOut.toByteArray())), new PasswordFinder()
    {
        public char[] getPassword()
        {
            return "hello".toCharArray();
        }
    });

    PrivateKey rdKey = (PrivateKey)pRd.readObject();

    assertEquals(key, rdKey);
}
 
开发者ID:credentials,项目名称:irma_future_id,代码行数:30,代码来源:AllTests.java

示例11: testPKCS8Plain

import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
public void testPKCS8Plain()
    throws Exception
{
    KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");

    kpGen.initialize(1024);

    PrivateKey key = kpGen.generateKeyPair().getPrivate();
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    PEMWriter pWrt = new PEMWriter(new OutputStreamWriter(bOut));
    PKCS8Generator pkcs8 = new PKCS8Generator(key);

    pWrt.writeObject(pkcs8);

    pWrt.close();

    PEMReader pRd = new PEMReader(new InputStreamReader(new ByteArrayInputStream(bOut.toByteArray())), new PasswordFinder()
    {
        public char[] getPassword()
        {
            return "hello".toCharArray();
        }
    });

    PrivateKey rdKey = (PrivateKey)pRd.readObject();

    assertEquals(key, rdKey);
}
 
开发者ID:credentials,项目名称:irma_future_id,代码行数:29,代码来源:AllTests.java

示例12: testPKCS8PlainNew

import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
public void testPKCS8PlainNew()
    throws Exception
{
    KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");

    kpGen.initialize(1024);

    PrivateKey key = kpGen.generateKeyPair().getPrivate();
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    PEMWriter pWrt = new PEMWriter(new OutputStreamWriter(bOut));
    PKCS8Generator pkcs8 = new JcaPKCS8Generator(key, null);

    pWrt.writeObject(pkcs8);

    pWrt.close();

    PEMReader pRd = new PEMReader(new InputStreamReader(new ByteArrayInputStream(bOut.toByteArray())), new PasswordFinder()
    {
        public char[] getPassword()
        {
            return "hello".toCharArray();
        }
    });

    PrivateKey rdKey = (PrivateKey)pRd.readObject();

    assertEquals(key, rdKey);
}
 
开发者ID:credentials,项目名称:irma_future_id,代码行数:29,代码来源:AllTests.java

示例13: from

import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
/**
 * Serialize objects to PEM format. Not all objects are supported. Supported objects are identical to those
 * supported by {@link PEMWriter#writeObject(Object)}.
 *
 * @param obj The object to serialize.
 * @return A string containing the object in PEM format.
 * @throws IllegalArgumentException when the object is not supported.
 */
public static String from(final Object obj) {
    try {
        final StringWriter out = new StringWriter();
        final PEMWriter pem = new PEMWriter(out);
        pem.writeObject(obj);
        pem.close();
        return out.getBuffer().toString();
    } catch (IOException e) {
        throw new IllegalArgumentException("Unable to encode as PEM", e);
    }
}
 
开发者ID:dglatt,项目名称:aws-sns-builders,代码行数:20,代码来源:Pem.java

示例14: test000GenerateCertificate

import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
@Test
    public void test000GenerateCertificate() {
        String cn = "www.example.it";
        String keystoreFile = "guanxi_idp_cert.jks";
        String keystorePassword = "changeit";
        String privateKeyPassword = "changeit";
        String privateKeyAlias = "www.example.it";

        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

        KeyStore ks = null;

        try {
            ks = KeyStore.getInstance("JKS");
            ks.load(null, null);

//            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
            keyGen.initialize(1024, new SecureRandom());
            KeyPair keypair = keyGen.generateKeyPair();
            PrivateKey privkey = keypair.getPrivate();
            PublicKey pubkey = keypair.getPublic();

            Hashtable<DERObjectIdentifier, String> attrs = new Hashtable<DERObjectIdentifier, String>();
            Vector<DERObjectIdentifier> ordering = new Vector<DERObjectIdentifier>();
            ordering.add(X509Name.CN);
            attrs.put(X509Name.CN, cn);
            X509Name issuerDN = new X509Name(ordering, attrs);
            X509Name subjectDN = new X509Name(ordering, attrs);

            Date validFrom = new Date();
            validFrom.setTime(validFrom.getTime() - (10 * 60 * 1000));

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

            Date validTo = new Date();
            validTo.setTime(cal.getTime().getTime());
//            validTo.setTime(validTo.getTime() + (20 * (24 * 60 * 60 * 1000)));

            X509V3CertificateGenerator x509 = new X509V3CertificateGenerator();
            //x509.setSignatureAlgorithm("SHA1withDSA");
            x509.setSignatureAlgorithm("SHA256withRSA");
            x509.setIssuerDN(issuerDN);
            x509.setSubjectDN(subjectDN);
            x509.setPublicKey(pubkey);
            x509.setNotBefore(validFrom);
            x509.setNotAfter(validTo);
            x509.setSerialNumber(new BigInteger(128, new Random()));

            X509Certificate[] cert = new X509Certificate[1];
            cert[0] = x509.generate(privkey, "BC");
            java.security.cert.Certificate[] chain = new java.security.cert.Certificate[1];
            chain[0] = cert[0];

            ks.setKeyEntry(privateKeyAlias, privkey, privateKeyPassword.toCharArray(), cert);
            ks.setKeyEntry(privateKeyAlias, privkey, privateKeyPassword.toCharArray(), chain);
            ks.store(new FileOutputStream(keystoreFile), keystorePassword.toCharArray());

            String IDP_RFC_CERT = "/tmp/guanxi_idp_cert.txt";

            PEMWriter pemWriter = new PEMWriter(new FileWriter(IDP_RFC_CERT));
            pemWriter.writeObject(cert[0]);
            pemWriter.close();

        } catch (Exception se) {
            se.printStackTrace(System.err);
        }
    }
 
开发者ID:publisys-spa,项目名称:spid-ims-discovery,代码行数:70,代码来源:ImsDiscoveryApplicationTests.java

示例15: displayCitizenCertificates

import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
@Test
public void displayCitizenCertificates() throws Exception {
	PcscEid pcscEidSpi = new PcscEid(new TestView(), this.messages);
	if (false == pcscEidSpi.isEidPresent()) {
		LOG.debug("insert eID card");
		pcscEidSpi.waitForEidPresent();
	}

	byte[] authnCertData = pcscEidSpi.readFile(PcscEid.AUTHN_CERT_FILE_ID);
	byte[] signCertData = pcscEidSpi.readFile(PcscEid.SIGN_CERT_FILE_ID);
	byte[] citizenCaCertData = pcscEidSpi.readFile(PcscEid.CA_CERT_FILE_ID);
	byte[] rootCaCertData = pcscEidSpi.readFile(PcscEid.ROOT_CERT_FILE_ID);
	byte[] nationalRegitryCertData = pcscEidSpi.readFile(PcscEid.RRN_CERT_FILE_ID);

	CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
	X509Certificate authnCert = (X509Certificate) certificateFactory
			.generateCertificate(new ByteArrayInputStream(authnCertData));
	X509Certificate signCert = (X509Certificate) certificateFactory
			.generateCertificate(new ByteArrayInputStream(signCertData));
	X509Certificate citizenCaCert = (X509Certificate) certificateFactory
			.generateCertificate(new ByteArrayInputStream(citizenCaCertData));
	X509Certificate rootCaCert = (X509Certificate) certificateFactory
			.generateCertificate(new ByteArrayInputStream(rootCaCertData));
	X509Certificate nationalRegitryCert = (X509Certificate) certificateFactory
			.generateCertificate(new ByteArrayInputStream(nationalRegitryCertData));

	LOG.debug("authentication certificate: " + authnCert);
	LOG.debug("signature certificate: " + signCert);
	LOG.debug("national registry certificate: " + nationalRegitryCert);
	LOG.debug("authn cert size: " + authnCertData.length);
	LOG.debug("sign cert size: " + signCertData.length);
	LOG.debug("citizen CA certificate: " + citizenCaCert);
	LOG.debug("root CA certificate: " + rootCaCert);
	LOG.debug("authn cert serial number: " + authnCert.getSerialNumber());
	LOG.debug("authn certificate issuer: " + authnCert.getIssuerX500Principal());

	File rootCaFile = File.createTempFile("test-root-ca-", ".pem");
	FileWriter rootCaFileWriter = new FileWriter(rootCaFile);
	PEMWriter rootCaPemWriter = new PEMWriter(rootCaFileWriter);
	rootCaPemWriter.writeObject(rootCaCert);
	rootCaPemWriter.close();

	File citizenCaFile = File.createTempFile("test-citizen-ca-", ".pem");
	FileWriter citizenCaFileWriter = new FileWriter(citizenCaFile);
	PEMWriter citizenCaPemWriter = new PEMWriter(citizenCaFileWriter);
	citizenCaPemWriter.writeObject(citizenCaCert);
	citizenCaPemWriter.close();

	pcscEidSpi.close();
	LOG.debug("root ca file: " + rootCaFile.getAbsolutePath());
	LOG.debug("citizen CA file: " + citizenCaFile.getAbsolutePath());
}
 
开发者ID:e-Contract,项目名称:eid-applet,代码行数:53,代码来源:PcscTest.java


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