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


Java PEMParser.close方法代码示例

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


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

示例1: setUp

import org.bouncycastle.openssl.PEMParser; //导入方法依赖的package包/类
@Before
public void setUp() throws Exception {
    // TODO should do this in @BeforeClass. Need to find out how to get to
    // files from static junit method
    BufferedInputStream bis = new BufferedInputStream(this.getClass().getResourceAsStream("/ca.crt"));
    testCACert = cf.generateCertificate(bis);
    bis.close();
    crypto.addCACertificateToTrustStore(testCACert, "ca");

    bis = new BufferedInputStream(this.getClass().getResourceAsStream("/keypair-signed.crt"));
    Certificate cert = cf.generateCertificate(bis);
    bis.close();

    // TODO: get PEM file without dropping down to BouncyCastle ?
    PEMParser pem = new PEMParser(new FileReader(this.getClass().getResource("/keypair-signed.key").getFile()));
    PEMKeyPair bcKeyPair = (PEMKeyPair) pem.readObject();
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bcKeyPair.getPrivateKeyInfo().getEncoded());
    PrivateKey key = kf.generatePrivate(keySpec);

    Certificate[] certificates = new Certificate[] {cert, testCACert};
    crypto.getTrustStore().setKeyEntry("key", key, "123456".toCharArray(), certificates);
    pem.close();
}
 
开发者ID:hyperledger,项目名称:fabric-sdk-java,代码行数:24,代码来源:CryptoPrimitivesTest.java

示例2: verifyUserSig

import org.bouncycastle.openssl.PEMParser; //导入方法依赖的package包/类
@Override
public boolean verifyUserSig(String identifier, String sig)throws QCloudException {
	try {
		Security.addProvider(new BouncyCastleProvider());
		
		//DeBaseUrl64 urlSig to json
		Base64 decoder = new Base64();

		byte [] compressBytes = Base64Url.base64DecodeUrl(sig.getBytes(Charset.forName("UTF-8")));
		
		//Decompression
		Inflater decompression =  new Inflater();
		decompression.setInput(compressBytes, 0, compressBytes.length);
		byte [] decompressBytes = new byte [1024];
		int decompressLength = decompression.inflate(decompressBytes);
		decompression.end();
		
		String jsonString = new String(Arrays.copyOfRange(decompressBytes, 0, decompressLength));
		
		//Get TLS.Sig from json
		JSONObject jsonObject= JSON.parseObject(jsonString);
		String sigTLS = jsonObject.getString("TLS.sig");
		
		//debase64 TLS.Sig to get serailString
		byte[] signatureBytes = decoder.decode(sigTLS.getBytes(Charset.forName("UTF-8")));
		
		String strSdkAppid = jsonObject.getString("TLS.sdk_appid");
		String sigTime = jsonObject.getString("TLS.time");
		String sigExpire = jsonObject.getString("TLS.expire_after");
		
		if (!imConfig.getSdkAppId().equals(strSdkAppid))
		{
			return false;
		}

		if ( System.currentTimeMillis()/1000 - Long.parseLong(sigTime) > Long.parseLong(sigExpire)) {
			return false;
		}
		
		//Get Serial String from json
		String SerialString = 
			"TLS.appid_at_3rd:" + 0 + "\n" +
			"TLS.account_type:" + 0 + "\n" +
			"TLS.identifier:" + identifier + "\n" + 
			"TLS.sdk_appid:" + imConfig.getSdkAppId() + "\n" + 
			"TLS.time:" + sigTime + "\n" + 
			"TLS.expire_after:" + sigExpire + "\n";
	
        Reader reader = new CharArrayReader(imConfig.getPublicKey().toCharArray());
        PEMParser  parser = new PEMParser(reader);
        JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
        Object obj = parser.readObject();
        parser.close();
        PublicKey pubKeyStruct  = converter.getPublicKey((SubjectPublicKeyInfo) obj);

		Signature signature = Signature.getInstance("SHA256withECDSA","BC");
		signature.initVerify(pubKeyStruct);
		signature.update(SerialString.getBytes(Charset.forName("UTF-8")));
		return signature.verify(signatureBytes);
	}catch (Exception e) {
		throw new QCloudException(e);
	}
}
 
开发者ID:51wakeup,项目名称:wakeup-qcloud-sdk,代码行数:64,代码来源:DefaultQCloudClient.java

示例3: getPrivateKeyFromPEM

import org.bouncycastle.openssl.PEMParser; //导入方法依赖的package包/类
private static PrivateKey getPrivateKeyFromPEM(final Reader keyReader)
  throws IOException {
  final JcaPEMKeyConverter jcaPEMKeyConverter = new JcaPEMKeyConverter();

  final PEMParser pem = new PEMParser(keyReader);

  PrivateKey key;
  Object pemContent = pem.readObject();
  if(pemContent instanceof PEMKeyPair) {
    PEMKeyPair pemKeyPair = (PEMKeyPair)pemContent;
    KeyPair keyPair = jcaPEMKeyConverter.getKeyPair(pemKeyPair);
    key = keyPair.getPrivate();
  } else if (pemContent instanceof PrivateKeyInfo) {
    PrivateKeyInfo privateKeyInfo = (PrivateKeyInfo) pemContent;
    key = jcaPEMKeyConverter.getPrivateKey(privateKeyInfo);
  } else {
    throw new IllegalArgumentException("Unsupported private key format '" + pemContent.getClass().getSimpleName() + '"');
  }

  pem.close();
  return key;
}
 
开发者ID:heroku,项目名称:env-keystore,代码行数:23,代码来源:EnvKeyStore.java

示例4: createTrustStore

import org.bouncycastle.openssl.PEMParser; //导入方法依赖的package包/类
private static KeyStore createTrustStore(final Reader certReader)
    throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException {
  PEMParser parser = new PEMParser(certReader);

  KeyStore ks = KeyStore.getInstance(DEFAULT_TYPE);
  ks.load(null);

  int i = 0;
  X509Certificate certificate;

  while ((certificate = parseCert(parser)) != null) {
    ks.setCertificateEntry(format("alias%d", i), certificate);
    i += 1;
  }

  parser.close();

  return ks;
}
 
开发者ID:heroku,项目名称:env-keystore,代码行数:20,代码来源:EnvKeyStore.java

示例5: readKeyPairFromFile

import org.bouncycastle.openssl.PEMParser; //导入方法依赖的package包/类
/**
 * Reads a keypair from a File.
 * 
 * @throws IOException If an error occurs while reading the file.
 * @throws InvalidKeySpecException If the key isn't encoded in x509.
 */
public static KeyPair readKeyPairFromFile(File file) throws IOException, InvalidKeySpecException {
  PEMParser parser =
      new PEMParser(new InputStreamReader(new FileInputStream(file), Constants.charset));

  Object obj = parser.readObject();
  parser.close();

  if (obj instanceof PEMKeyPair) {
    PEMKeyPair pem = (PEMKeyPair) obj;
    JcaPEMKeyConverter conv = new JcaPEMKeyConverter();

    return conv.getKeyPair(pem);
  }

  return null;
}
 
开发者ID:kit-tm,项目名称:PTP,代码行数:23,代码来源:CryptHelper.java

示例6: readPrivateKey

import org.bouncycastle.openssl.PEMParser; //导入方法依赖的package包/类
private PrivateKey readPrivateKey(String privateKeyPath, String keyPassword) throws IOException {

        FileReader fileReader = new FileReader(privateKeyPath);
        PEMParser keyReader = new PEMParser(fileReader);

        JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
        PEMDecryptorProvider decryptionProv = new JcePEMDecryptorProviderBuilder().build(keyPassword.toCharArray());

        Object keyPair = keyReader.readObject();
        PrivateKeyInfo keyInfo;

        if (keyPair instanceof PEMEncryptedKeyPair) {
            PEMKeyPair decryptedKeyPair = ((PEMEncryptedKeyPair) keyPair).decryptKeyPair(decryptionProv);
            keyInfo = decryptedKeyPair.getPrivateKeyInfo();
        } else {
            keyInfo = ((PEMKeyPair) keyPair).getPrivateKeyInfo();
        }

        keyReader.close();
        return converter.getPrivateKey(keyInfo);
    }
 
开发者ID:abbaspour,项目名称:urmia,代码行数:22,代码来源:HttpSigner.java

示例7: loadCertificateFromPEM

import org.bouncycastle.openssl.PEMParser; //导入方法依赖的package包/类
public static X509Certificate loadCertificateFromPEM(InputStream certfile)
		throws IOException, CertificateException, FileFormatException {
	PEMParser parser = new PEMParser(new InputStreamReader(certfile));

	try {

		Object obj = parser.readObject();

		if (obj instanceof X509CertificateHolder) {
			return new JcaX509CertificateConverter().setProvider("BC")
					.getCertificate((X509CertificateHolder) obj);
		} else {
			throw new FileFormatException(
					"Failed to read X509Certificate from InputStream provided");
		}

	} finally {
		IOUtils.closeQuietly(certfile);
		parser.close();
	}
}
 
开发者ID:ludup,项目名称:hypersocket-framework,代码行数:22,代码来源:X509CertificateUtils.java

示例8: 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

示例9: getUserSig

import org.bouncycastle.openssl.PEMParser; //导入方法依赖的package包/类
@Override
public String getUserSig(String identifier, long expire)throws QCloudException {
	try {
		 	Security.addProvider(new BouncyCastleProvider());
	        Reader reader = new CharArrayReader(imConfig.getPrivateKey().toCharArray());
	        JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
	        PEMParser parser = new PEMParser(reader);
	        Object obj = parser.readObject();
	        parser.close();
	    	PrivateKey privKeyStruct = converter.getPrivateKey((PrivateKeyInfo) obj);
			
			String jsonString = "{" 
			+ "\"TLS.account_type\":\"" + 0 +"\","
			+"\"TLS.identifier\":\"" + identifier +"\","
			+"\"TLS.appid_at_3rd\":\"" + 0 +"\","
		    +"\"TLS.sdk_appid\":\"" + imConfig.getSdkAppId() +"\","
			+"\"TLS.expire_after\":\"" + expire +"\","
	        +"\"TLS.version\": \"201512300000\""
			+"}";
			
			String time = String.valueOf(System.currentTimeMillis()/1000);
			String SerialString = 
				"TLS.appid_at_3rd:" + 0 + "\n" +
				"TLS.account_type:" + 0 + "\n" +
				"TLS.identifier:" + identifier + "\n" + 
				"TLS.sdk_appid:" + imConfig.getSdkAppId() + "\n" + 
				"TLS.time:" + time + "\n" +
				"TLS.expire_after:" + expire +"\n";
			
			//Create Signature by SerialString
			Signature signature = Signature.getInstance("SHA256withECDSA", "BC");
			signature.initSign(privKeyStruct);
			signature.update(SerialString.getBytes(Charset.forName("UTF-8")));
			byte[] signatureBytes = signature.sign();
			
			String sigTLS = Base64.encodeBase64String(signatureBytes);
			
			//Add TlsSig to jsonString
		    JSONObject jsonObject= JSON.parseObject(jsonString);
		    jsonObject.put("TLS.sig", (Object)sigTLS);
		    jsonObject.put("TLS.time", (Object)time);
		    jsonString = jsonObject.toString();
		    
		    //compression
		    Deflater compresser = new Deflater();
		    compresser.setInput(jsonString.getBytes(Charset.forName("UTF-8")));

		    compresser.finish();
		    byte [] compressBytes = new byte [512];
		    int compressBytesLength = compresser.deflate(compressBytes);
		    compresser.end();
		    return new String(Base64Url.base64EncodeUrl(Arrays.copyOfRange(compressBytes,0,compressBytesLength)));
	}catch (Exception e) {
		throw new  QCloudException(e);
	}
}
 
开发者ID:51wakeup,项目名称:wakeup-qcloud-sdk,代码行数:57,代码来源:DefaultQCloudClient.java

示例10: createKeyStore

import org.bouncycastle.openssl.PEMParser; //导入方法依赖的package包/类
private static KeyStore createKeyStore(final Reader keyReader, final Reader certReader, final String password)
    throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException {
  PrivateKey key = getPrivateKeyFromPEM(keyReader);
  keyReader.close();

  PEMParser parser = new PEMParser(certReader);
  X509Certificate certificate = parseCert(parser);
  parser.close();

  KeyStore ks = KeyStore.getInstance(DEFAULT_TYPE);
  ks.load(null);
  ks.setKeyEntry("alias", key, password.toCharArray(), new X509Certificate[]{certificate});
  return ks;
}
 
开发者ID:heroku,项目名称:env-keystore,代码行数:15,代码来源:EnvKeyStore.java

示例11: extractRSAKeyPair

import org.bouncycastle.openssl.PEMParser; //导入方法依赖的package包/类
/**
 * makes RSA private key from PEM string.
 *
 * @param s PEM string that contains the key
 * @return
 * @see JCERSAPublicKey
 */
public static RSAKeyPair extractRSAKeyPair(final String s) {
    RSAKeyPair rsaKeyPair;
    try {
        // parse
        final PEMParser parser = new PEMParser(new StringReader(s));
        final Object o = parser.readObject();
        parser.close();
        // check types
        if (!(o instanceof PEMKeyPair)) {
            throw new IOException("Encryption.extractRSAKeyPair: no private key found in string '" + s + "'");
        }
        final PEMKeyPair keyPair = (PEMKeyPair) o;
        if (keyPair.getPrivateKeyInfo() == null) {
            throw new IOException("Encryption.extractRSAKeyPair: no private key found in key pair of string '" + s + "'");
        }
        if (keyPair.getPublicKeyInfo() == null) {
            throw new IOException("Encryption.extractRSAKeyPair: no public key found in key pair of string '" + s + "'");
        }

        // convert keys and pack them together into a key pair
        final RSAPrivateCrtKey privateKey = new TempJCERSAPrivateCrtKey(keyPair.getPrivateKeyInfo());
        logger.debug("JCEPrivateKey={}", privateKey);
        final RSAPublicKey publicKey = new TempJCERSAPublicKey(keyPair.getPublicKeyInfo());
        rsaKeyPair = new RSAKeyPair(publicKey, privateKey);

    } catch (final Exception e) {
        logger.warn("Encryption.extractPrivateRSAKey: Caught exception:" + e.getMessage());
        rsaKeyPair = null;
    }

    return rsaKeyPair;
}
 
开发者ID:rovemonteux,项目名称:silvertunnel-monteux,代码行数:40,代码来源:Encryption.java

示例12: 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

示例13: loadCertificateChainFromPEM

import org.bouncycastle.openssl.PEMParser; //导入方法依赖的package包/类
public static X509Certificate[] loadCertificateChainFromPEM(
		InputStream certfile) throws IOException, CertificateException,
		FileFormatException {

	List<X509Certificate> certs = new ArrayList<X509Certificate>();

	PEMParser parser = new PEMParser(new InputStreamReader(certfile));

	try {

		Object obj = null;
		while ((obj = parser.readObject()) != null) {

			if (obj instanceof X509CertificateHolder) {
				certs.add(new JcaX509CertificateConverter().setProvider(
						"BC").getCertificate((X509CertificateHolder) obj));
			} else {
				throw new FileFormatException(
						"Failed to read X509Certificate from InputStream provided");
			}
		}

		return certs.toArray(new X509Certificate[0]);

	} finally {
		IOUtils.closeQuietly(certfile);
		parser.close();
	}
}
 
开发者ID:ludup,项目名称:hypersocket-framework,代码行数:30,代码来源:X509CertificateUtils.java

示例14: loadKeyInternal

import org.bouncycastle.openssl.PEMParser; //导入方法依赖的package包/类
public static KeyPair loadKeyInternal(String file, PublicKey publicKey) throws IOException {
	BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
	PEMParser parser = new PEMParser(bufferedReader);
	Object key;
	PrivateKeyInfo privateKeyInfo = (key = parser.readObject()) instanceof PrivateKeyInfo ? (PrivateKeyInfo)key : ((PEMKeyPair)key).getPrivateKeyInfo();
	parser.close();
	bufferedReader.close();
	JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
	converter.setProvider("BC");
	return new KeyPair(publicKey, converter.getPrivateKey(privateKeyInfo));
}
 
开发者ID:oberien,项目名称:Oberien,代码行数:12,代码来源:CertificateClientManagement.java

示例15: readPEMFile

import org.bouncycastle.openssl.PEMParser; //导入方法依赖的package包/类
private Object readPEMFile(String filePath) throws Exception {
  PEMParser reader = new PEMParser(new FileReader(filePath));
  Object fileHolder = reader.readObject();
  reader.close();
  return fileHolder;
}
 
开发者ID:osswangxining,项目名称:iot-edge-greengrass,代码行数:7,代码来源:CertPemClientCredentials.java


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