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


Java PEMKeyPair类代码示例

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


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

示例1: setUp

import org.bouncycastle.openssl.PEMKeyPair; //导入依赖的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: getKeyPair

import org.bouncycastle.openssl.PEMKeyPair; //导入依赖的package包/类
public KeyPair getKeyPair(PEMKeyPair keyPair)
    throws PEMException
{
    try
    {
        String algorithm =  keyPair.getPrivateKeyInfo().getPrivateKeyAlgorithm().getAlgorithm().getId();

        if (X9ObjectIdentifiers.id_ecPublicKey.getId().equals(algorithm))
        {
            algorithm = "ECDSA";
        }

        KeyFactory keyFactory = helper.createKeyFactory(algorithm);

        return new KeyPair(keyFactory.generatePublic(new X509EncodedKeySpec(keyPair.getPublicKeyInfo().getEncoded())),
                            keyFactory.generatePrivate(new PKCS8EncodedKeySpec(keyPair.getPrivateKeyInfo().getEncoded())));
    }
    catch (Exception e)
    {
        throw new PEMException("unable to convert key pair: " + e.getMessage(), e);
    }
}
 
开发者ID:Appdome,项目名称:ipack,代码行数:23,代码来源:JcaPEMKeyConverter.java

示例3: generate

import org.bouncycastle.openssl.PEMKeyPair; //导入依赖的package包/类
static PrivateKey generate(Path path) throws IOException {
    try (Reader in = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
        PEMParser parser = new PEMParser(in);

        Object keyPair = parser.readObject();
        if (!(keyPair instanceof PEMKeyPair)) {
            throw new IllegalStateException(String.format("%s contains an artifact that is not a key pair: %s", path, keyPair));
        }

        PrivateKeyInfo privateKeyInfo = ((PEMKeyPair) keyPair).getPrivateKeyInfo();
        if (privateKeyInfo == null) {
            throw new IllegalStateException(String.format("%s does not contain a private key", path));
        }

        return CONVERTER.getPrivateKey(privateKeyInfo);
    }
}
 
开发者ID:cloudfoundry,项目名称:java-buildpack-security-provider,代码行数:18,代码来源:PrivateKeyFactory.java

示例4: createKeyPair

import org.bouncycastle.openssl.PEMKeyPair; //导入依赖的package包/类
/**
 * Utility method to decode a PEM encoded private key string to a PrivateKey instance
 * @param key
 *         - a PEM encoded private key string
 * @return - decoded PrivateKey instance
 */
public static KeyPair createKeyPair(String key) {
    AssertUtil.assertNotNull(key, "key");
    String decryptedKey = EncryptionUtils.decrypt(key);
    try (PEMParser parser = new PEMParser(new StringReader(decryptedKey))) {

        JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
        PEMKeyPair keyPair = (PEMKeyPair) parser.readObject();
        if (keyPair == null) {
            throw new RuntimeException("A valid key pair was not found");
        }
        return converter.getKeyPair(keyPair);

    } catch (IOException e) {
        throw new RuntimeException("Failed to create key pair", e);
    }
}
 
开发者ID:vmware,项目名称:photon-model,代码行数:23,代码来源:CertificateUtil.java

示例5: readPrivateKey

import org.bouncycastle.openssl.PEMKeyPair; //导入依赖的package包/类
/**
 * Reads a base64-format PEM key and returns a Java PrivateKey for it.
 * @param privateKey PEM-encoded private key
 */
public static PrivateKey readPrivateKey(String privateKey) {
    try (StringReader keyReader = new StringReader(privateKey);
         PEMParser pemReader = new PEMParser(keyReader)) {
        
        JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
        Object keyPair = pemReader.readObject();
        if (keyPair instanceof PrivateKeyInfo) {
            return converter.getPrivateKey((PrivateKeyInfo) keyPair);
        } else {
            return converter.getPrivateKey(((PEMKeyPair) keyPair).getPrivateKeyInfo());
        }
    } catch (IOException x) {
        // Shouldn't occur, since we're only reading from strings
        throw new RuntimeException(x);            
    }
}
 
开发者ID:Tradeshift,项目名称:ts-reaktive,代码行数:21,代码来源:SSLFactory.java

示例6: getPrivateKeyFromPEM

import org.bouncycastle.openssl.PEMKeyPair; //导入依赖的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

示例7: readVapidKeys

import org.bouncycastle.openssl.PEMKeyPair; //导入依赖的package包/类
/**
 * Read the VAPID key from the .pem file in the resources folder.
 *
 * @return
 * @throws IOException
 */
public static KeyPair readVapidKeys() throws IOException {
    try (InputStreamReader inputStreamReader = new InputStreamReader(TestUtils.class.getResourceAsStream("/vapid.pem"))) {
        PEMParser pemParser = new PEMParser(inputStreamReader);
        PEMKeyPair pemKeyPair = (PEMKeyPair) pemParser.readObject();

        return new JcaPEMKeyConverter().getKeyPair(pemKeyPair);
    } catch (IOException e) {
        throw new IOException("The private key could not be decrypted", e);
    }
}
 
开发者ID:web-push-libs,项目名称:webpush-java,代码行数:17,代码来源:TestUtils.java

示例8: rebuildKeyPair

import org.bouncycastle.openssl.PEMKeyPair; //导入依赖的package包/类
/**
 * Rebuild the {@link KeyPair} associated with the submitted {@link PrivateKey}.
 *
 * @param privateKey The {@link PrivateKey} to rebuild the {@link KeyPair} for.
 * @return The rebuild {@link KeyPair}.
 * @throws IOException if an I/O error occurs during the rebuild.
 */
public static KeyPair rebuildKeyPair(PrivateKey privateKey) throws IOException {
	StringWriter stringWriter = new StringWriter();

	try (JcaPEMWriter pemWriter = new JcaPEMWriter(stringWriter)) {
		pemWriter.writeObject(privateKey);
		pemWriter.flush();
	}

	StringReader stringReader = new StringReader(stringWriter.toString());
	Object pemObject;

	try (PEMParser pemParser = new PEMParser(stringReader)) {
		pemObject = pemParser.readObject();
	}

	if (!(pemObject instanceof PEMKeyPair)) {
		throw new IOException("Unexpected pem object: " + pemObject);
	}

	PEMKeyPair pemKeyPair = (PEMKeyPair) pemObject;

	return PEM_KEY_CONVERTER.getKeyPair(pemKeyPair);
}
 
开发者ID:hdecarne,项目名称:certmgr,代码行数:31,代码来源:KeyHelper.java

示例9: convertKey

import org.bouncycastle.openssl.PEMKeyPair; //导入依赖的package包/类
private static KeyPair convertKey(PEMEncryptedKeyPair pemObject, String resource, PasswordCallback password)
		throws IOException {
	PEMKeyPair pemKeyPair = null;
	Throwable passwordException = null;

	while (pemKeyPair == null) {
		char[] passwordChars = password.queryPassword(resource);

		if (passwordChars == null) {
			throw new PasswordRequiredException(resource, passwordException);
		}

		PEMDecryptorProvider pemDecryptorProvider = PEM_DECRYPTOR_PROVIDER_BUILDER.build(passwordChars);

		try {
			pemKeyPair = pemObject.decryptKeyPair(pemDecryptorProvider);
		} catch (EncryptionException e) {
			passwordException = e;
		}
	}
	return convertKey(pemKeyPair);
}
 
开发者ID:hdecarne,项目名称:certmgr,代码行数:23,代码来源:PEMCertReaderWriter.java

示例10: dwimKey

import org.bouncycastle.openssl.PEMKeyPair; //导入依赖的package包/类
static Key dwimKey(String v) {
    File f = new File(v);
    if (f.isFile()) {
        try (PEMParser pem = new PEMParser(Files.newBufferedReader(f.toPath(), StandardCharsets.UTF_8))) {
            Object x = pem.readObject();
            if (x instanceof PEMKeyPair) {
                PEMKeyPair kp = (PEMKeyPair) x;
                // Make a private key
                return new JcaPEMKeyConverter().getPrivateKey(kp.getPrivateKeyInfo());
            } else {
                fail("-key only supports plain PEM keypairs");
                return null; // Static code analyzer
            }
        } catch (IOException e) {
            throw new IllegalArgumentException("Could not parse key: " + e.getMessage(), e);
        }
    } else {
        // Assume it is an AES key
        // TODO: check for validity before
        return new SecretKeySpec(HexUtils.stringToBin(v), "AES");
    }
}
 
开发者ID:martinpaljak,项目名称:cdoc,代码行数:23,代码来源:Tool.java

示例11: loadKeyPair

import org.bouncycastle.openssl.PEMKeyPair; //导入依赖的package包/类
private PrivateKey loadKeyPair() throws IOException {
    PEMParser reader = new PEMParser(file);
    Object pemObject;

    JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
    //PEMDecryptorProvider decryptionProv = new JcePEMDecryptorProviderBuilder().build(passphrase);

    while((pemObject = reader.readObject()) != null) {
        logger.debug("PemObject type: " + pemObject.getClass().getName());

        if(pemObject instanceof PEMKeyPair) {
            logger.debug("it match");
            PrivateKeyInfo pki = ((PEMKeyPair) pemObject).getPrivateKeyInfo();
            logger.debug("content: " + pki.getEncoded("UTF-8"));
            return converter.getPrivateKey(pki);
        } else {
            logger.debug("Dont match");
        }
    }

    logger.debug("fsdfsfs");
    return null;
}
 
开发者ID:DTStack,项目名称:jlogstash-input-plugin,代码行数:24,代码来源:PrivateKeyConverter.java

示例12: buildCertificateRequest

import org.bouncycastle.openssl.PEMKeyPair; //导入依赖的package包/类
@Override
public String buildCertificateRequest() {
	try {
		CompanyInfo companyInfo = wsaaDao.loadActiveCompanyInfo();

		JcaPEMKeyConverter converter = new JcaPEMKeyConverter();

		PEMKeyPair pemPrivateKey = fromPem(companyInfo.getPrivateKey());
		PrivateKey privateKey = converter.getPrivateKey(pemPrivateKey
				.getPrivateKeyInfo());
		PEMKeyPair pemPublicKey = fromPem(companyInfo.getPrivateKey());
		PublicKey publicKey = converter.getPublicKey(pemPublicKey
				.getPublicKeyInfo());

		X500Principal subject = new X500Principal(companyInfo.buildSource());
		ContentSigner signGen = new JcaContentSignerBuilder(SIGNING_ALGORITHM)
				.build(privateKey);

		PKCS10CertificationRequest csr = new JcaPKCS10CertificationRequestBuilder(
				subject, publicKey).build(signGen);

		return toPem(csr);
	} catch (IOException | OperatorCreationException e) {
		throw Throwables.propagate(e);
	}
}
 
开发者ID:NibiruOS,项目名称:afip,代码行数:27,代码来源:BouncyCastleWsaaManager.java

示例13: providePrivateKey

import org.bouncycastle.openssl.PEMKeyPair; //导入依赖的package包/类
@Singleton
@Provides
static PrivateKey providePrivateKey(@Named("pemObjects") ImmutableList<Object> pemObjects) {
  JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
  Function<PEMKeyPair, PrivateKey> privateKeyConverter =
      pemKeyPair -> {
        try {
          return converter.getKeyPair(pemKeyPair).getPrivate();
        } catch (PEMException e) {
          logger.severefmt(e, "Error converting private key: %s", pemKeyPair);
          throw new RuntimeException(e);
        }
      };
  ImmutableList<PrivateKey> privateKeys =
      filterAndConvert(pemObjects, PEMKeyPair.class, privateKeyConverter);
  checkState(
      privateKeys.size() == 1,
      "The pem file must contain exactly one private key, but %s keys are found",
      privateKeys.size());
  return privateKeys.get(0);
}
 
开发者ID:google,项目名称:nomulus,代码行数:22,代码来源:CertificateModule.java

示例14: init

import org.bouncycastle.openssl.PEMKeyPair; //导入依赖的package包/类
public JsonWebToken init() throws IOException {
	Security.addProvider(new BouncyCastleProvider());
	Reader reader;
	String keyFile = config.getAppKeyFile();
	boolean isOnClasspath = keyFile.startsWith("classpath:");
	if (isOnClasspath) {
		keyFile = keyFile.substring(10);
		reader = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(keyFile));
	} else {
		reader = new FileReader(new File(keyFile));
	}
	try (PEMParser pp = new PEMParser(reader)) {
		PEMKeyPair pemKeyPair = (PEMKeyPair) pp.readObject();
		keyPair = new JcaPEMKeyConverter().getKeyPair(pemKeyPair);
	}
	return this;
}
 
开发者ID:poynt,项目名称:java-cloud-sdk,代码行数:18,代码来源:JsonWebToken.java

示例15: readKeyPairFromFile

import org.bouncycastle.openssl.PEMKeyPair; //导入依赖的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


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