當前位置: 首頁>>代碼示例>>Java>>正文


Java PEMParser.readPemObject方法代碼示例

本文整理匯總了Java中org.bouncycastle.openssl.PEMParser.readPemObject方法的典型用法代碼示例。如果您正苦於以下問題:Java PEMParser.readPemObject方法的具體用法?Java PEMParser.readPemObject怎麽用?Java PEMParser.readPemObject使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.bouncycastle.openssl.PEMParser的用法示例。


在下文中一共展示了PEMParser.readPemObject方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getSocketFactoryFromPEM

import org.bouncycastle.openssl.PEMParser; //導入方法依賴的package包/類
private SSLSocketFactory getSocketFactoryFromPEM(String filePath) throws Exception {
    Security.addProvider(new BouncyCastleProvider());

    PEMParser pemParser = new PEMParser(new FileReader(getConfigFilename(filePath)));
    pemParser.readObject();
    PemObject pemObject = pemParser.readPemObject();
    pemParser.close();

    X509CertificateHolder holder = new X509CertificateHolder(pemObject.getContent());
    X509Certificate bc = new JcaX509CertificateConverter().setProvider("BC")
            .getCertificate(holder);

    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    keyStore.load(null, null);
    keyStore.setCertificateEntry("ca", bc);

    TrustManager trustManager = TrustManagerUtils.getDefaultTrustManager(keyStore);
    SSLContext sslContext = SSLContextUtils.createSSLContext("TLS", null, trustManager);

    return sslContext.getSocketFactory();
}
 
開發者ID:Appdynamics,項目名稱:mongo-monitoring-extension,代碼行數:22,代碼來源:MongoDBMonitor.java

示例2: getCertificateFromPem

import org.bouncycastle.openssl.PEMParser; //導入方法依賴的package包/類
private static X509Certificate getCertificateFromPem(String certInput) throws IOException, CertificateException {
    StringReader reader = new StringReader(certInput);
    PEMParser pr = new PEMParser(reader);
    try {
        PemObject pem = pr.readPemObject();
        X509CertificateHolder holder = new X509CertificateHolder(pem.getContent());
        return new JcaX509CertificateConverter().setProvider("BC")
                .getCertificate(holder);
    } finally {
        pr.close();
    }
}
 
開發者ID:rancher,項目名稱:cattle,代碼行數:13,代碼來源:SslCertificateUtils.java

示例3: decryptForgePkcs8PrivateKeyPem_PBEWithSHA1AndDESede

import org.bouncycastle.openssl.PEMParser; //導入方法依賴的package包/類
/**
 * Test to show that tripple des PKCS8 private key pem created with ForgeJS
 * can be decrypted with BC. 
 * 
 * @throws Exception 
 */
@Test
public void decryptForgePkcs8PrivateKeyPem_PBEWithSHA1AndDESede() throws Exception {
    // http://bouncy-castle.1462172.n4.nabble.com/Help-with-EncryptedPrivateKeyInfo-td1468363.html
    // https://community.oracle.com/thread/1530354?start=0&tstart=0
    Security.addProvider(new BouncyCastleProvider());

    //PEMParser keyPemParser = new PEMParser(new StringReader(getPkcs8ForgePriKeyPem_PBEWithMD5AndDES()));
    //String passwd = "1234567890";
    PEMParser keyPemParser = new PEMParser(new StringReader(getPkcs8ForgePriKeyPem_EncryptedWithPBEWithSHA1AndDESede()));
    String passwd = "password";
    PemObject keyObj = keyPemParser.readPemObject();
    byte[] keyBytes = keyObj.getContent();

    EncryptedPrivateKeyInfo encryptPKInfo = new EncryptedPrivateKeyInfo(keyBytes);
    // 1.2.840.113549.1.5.13 == PBEWithMD5AndDES
    // 1.2.840.113549.1.12.1.3 == PBEWithSHA1AndDESede
    String algName = encryptPKInfo.getAlgName();
    String algId = encryptPKInfo.getAlgParameters().getAlgorithm();
    assertEquals("PBEWithSHA1AndDESede", algName);
    assertEquals("1.2.840.113549.1.12.1.3", algId);
    assertEquals("1.2.840.113549.1.12.1.3", PKCS8Generator.PBE_SHA1_3DES.getId());

    // Decrypt private key 
    Cipher cipher = Cipher.getInstance(algName);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(passwd.toCharArray());
    SecretKeyFactory secFac = SecretKeyFactory.getInstance(algName);
    Key pbeKey = secFac.generateSecret(pbeKeySpec);
    AlgorithmParameters algParams = encryptPKInfo.getAlgParameters();
    cipher.init(Cipher.DECRYPT_MODE, pbeKey, algParams);
    KeySpec pkcs8KeySpec = encryptPKInfo.getKeySpec(cipher);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PrivateKey priKeyDecryptedBC = kf.generatePrivate(pkcs8KeySpec);

    // Compare decrypted private key with a version that was decrypted using 
    // openssl and assert that they are the same. 
    JcaPKCS8Generator pkcs8GeneratorNoEnc = new JcaPKCS8Generator(priKeyDecryptedBC, null);
    PemObject pkcs8PemDecryptedBC = pkcs8GeneratorNoEnc.generate();
    StringWriter writer3 = new StringWriter();
    PEMWriter pemWrite3 = new PEMWriter(writer3);
    pemWrite3.writeObject(pkcs8PemDecryptedBC);
    pemWrite3.close();
    String pkcs8StrDecryptedBC = writer3.toString().trim().replaceAll("\\r\\n", "\n");;
    String pkcs8StrDecryptedOpenSSL = getPkcs8ForgePriKeyPem_DecryptedWithOpenSSL().trim().replaceAll("\\r\\n", "\n");; 
    //System.out.println("["+pkcs8StrNoEncBC+"]");
    //System.out.println("["+pkcs8StrNoEncOpenssL+"]");
    assertTrue(pkcs8StrDecryptedBC.equals(pkcs8StrDecryptedOpenSSL));  
}
 
開發者ID:UKCA,項目名稱:CAPortal,代碼行數:54,代碼來源:TestBC_CreatePKCS10.java

示例4: loadKeyPairAndCertificateFromFile

import org.bouncycastle.openssl.PEMParser; //導入方法依賴的package包/類
protected void loadKeyPairAndCertificateFromFile() throws Exception {
    PemReader certificatePEMReader=new PemReader(new FileReader(this.localCertificateFile));
    PemObject certificatePEM=certificatePEMReader.readPemObject();
    certificatePEMReader.close();
    CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");
    this.cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(certificatePEM.getContent()));
    localCertificateChainArray=new Certificate[]{cert};


    PKCS8EncodedKeySpec encodedKeySpec;
//    if(false){
//      PemReader privateKeyReader=new PemReader(new FileReader(this.localPrivateKeyFile));
//      PemObject privateKeyPEM=privateKeyReader.readPemObject();
//      privateKeyReader.close();
//      encodedKeySpec = new PKCS8EncodedKeySpec(privateKeyPEM.getContent());
//    }
//    else if(true){
      PEMParser privateKeyParser=new PEMParser(new FileReader(this.localPrivateKeyFile));
      PemObject privateKeyPEM=privateKeyParser.readPemObject();
      privateKeyParser.close();
      encodedKeySpec = new PKCS8EncodedKeySpec(privateKeyPEM.getContent());
//    }
//    else{
//      //For whatever reason PemReader is not accessible from outside their package
//      Class pemReaderClass=Class.forName("io.netty.handler.ssl.PemReader");
//      Method readPrivateKeyMethod = pemReaderClass.getDeclaredMethod("readPrivateKey", File.class);
//      readPrivateKeyMethod.setAccessible(true);
//      ByteBuf encodedKeyBuf = (ByteBuf) readPrivateKeyMethod.invoke(null, localPrivateKeyFile);
//      //    ByteBuf encodedKeyBuf = PemReader.readPrivateKey(localPrivateKeyFile);
//      byte[] encodedKey = new byte[encodedKeyBuf.readableBytes()];
//      encodedKeyBuf.readBytes(encodedKey).release();
//      encodedKeySpec = new PKCS8EncodedKeySpec(encodedKey);
//    }
    KeyFactory ecKeyFactory = KeyFactory.getInstance("EC", BC_PROVIDER);
    localPrivateECKey = (BCECPrivateKey) ecKeyFactory.generatePrivate(encodedKeySpec);
    localPublicECKey = (BCECPublicKey) localCertificateChainArray[0].getPublicKey();

    localId=new NodeIdentifier(localPublicECKey);
    LOGGER.debug("Loaded identity "+localPublicECKey);
    LOGGER.debug("Loaded identity "+localId);
  }
 
開發者ID:pmarches,項目名稱:peercentrum-core,代碼行數:42,代碼來源:NodeIdentity.java


注:本文中的org.bouncycastle.openssl.PEMParser.readPemObject方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。