本文整理汇总了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();
}
示例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();
}
}
示例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));
}
示例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);
}