private static PrivateKey generatePrivateKey(PrivateKeyInfo keyInfo) throws IOException { return new JcaPEMKeyConverter().getPrivateKey(keyInfo); }
} else if (object instanceof PrivateKeyInfo) { PrivateKeyInfo privateKeyInfo = (PrivateKeyInfo) object; privateKey = converter.getPrivateKey(privateKeyInfo);
/** * Return private key ("key.pem") from Reader */ @CheckForNull public static PrivateKey loadPrivateKey(final Reader reader) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { try (PEMParser pemParser = new PEMParser(reader)) { Object readObject = pemParser.readObject(); while (readObject != null) { PrivateKeyInfo privateKeyInfo = getPrivateKeyInfoOrNull(readObject); if (privateKeyInfo != null) { return new JcaPEMKeyConverter().getPrivateKey(privateKeyInfo); } readObject = pemParser.readObject(); } } return null; }
PrivateKey key = new JcaPEMKeyConverter().setProvider("BC").getPrivateKey((PrivateKeyInfo) parser.readObject());
PrivateKey key = new JcaPEMKeyConverter().setProvider("BC").getPrivateKey((PrivateKeyInfo) parser.readObject());
final InputDecryptorProvider provider = new JceOpenSSLPKCS8DecryptorProviderBuilder().build( passPhrase.toCharArray() ); final PrivateKeyInfo privateKeyInfo = encryptedInfo.decryptPrivateKeyInfo( provider ); return converter.getPrivateKey( privateKeyInfo ); return converter.getPrivateKey( (PrivateKeyInfo) object );
privateKey = pemConverter.getPrivateKey(pKeyInfo);
@Override public PrivateKey decodePemEncodedPrivateKey(Reader privateKeyReader, String password) { try (PEMParser pemParser = new PEMParser(privateKeyReader)) { Object keyPair = pemParser.readObject(); // retrieve the PrivateKeyInfo from the returned keyPair object. if the key is encrypted, it needs to be // decrypted using the specified password first. PrivateKeyInfo keyInfo; if (keyPair instanceof PEMEncryptedKeyPair) { if (password == null) { throw new ImportException("Unable to import private key. Key is encrypted, but no password was provided."); } PEMDecryptorProvider decryptor = new JcePEMDecryptorProviderBuilder().build(password.toCharArray()); PEMKeyPair decryptedKeyPair = ((PEMEncryptedKeyPair) keyPair).decryptKeyPair(decryptor); keyInfo = decryptedKeyPair.getPrivateKeyInfo(); } else { keyInfo = ((PEMKeyPair) keyPair).getPrivateKeyInfo(); } return new JcaPEMKeyConverter().getPrivateKey(keyInfo); } catch (IOException e) { throw new ImportException("Unable to read PEM-encoded PrivateKey", e); } }
private static PrivateKey getPrivateKeyFromString(String data) throws IOException { final Reader pemReader = new StringReader(data); final PrivateKeyInfo pemPair; try (PEMParser pemParser = new PEMParser(pemReader)) { pemPair = (PrivateKeyInfo) pemParser.readObject(); } return new JcaPEMKeyConverter().getPrivateKey(pemPair); }
/** * Return PrivateKey from pem bytes. * * @param pemKey pem-encoded private key * @return */ public PrivateKey bytesToPrivateKey(byte[] pemKey) throws CryptoException { PrivateKey pk = null; CryptoException ce = null; try { PemReader pr = new PemReader(new StringReader(new String(pemKey))); PemObject po = pr.readPemObject(); PEMParser pem = new PEMParser(new StringReader(new String(pemKey))); if (po.getType().equals("PRIVATE KEY")) { pk = new JcaPEMKeyConverter().getPrivateKey((PrivateKeyInfo) pem.readObject()); } else { logger.trace("Found private key with type " + po.getType()); PEMKeyPair kp = (PEMKeyPair) pem.readObject(); pk = new JcaPEMKeyConverter().getPrivateKey(kp.getPrivateKeyInfo()); } } catch (Exception e) { throw new CryptoException("Failed to convert private key bytes", e); } return pk; }
private static KeyPair toKeyPair(final PrivateKeyInfo pki) throws PEMException, NoSuchAlgorithmException, InvalidKeySpecException { final PrivateKey privateKey = pemConverter.getPrivateKey(pki); // If it's RSA, we can use the modulus and public exponents as BigIntegers to create a public key if (privateKey instanceof RSAPrivateCrtKey) { final RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(((RSAPrivateCrtKey) privateKey).getModulus(), ((RSAPrivateCrtKey) privateKey).getPublicExponent()); final KeyFactory keyFactory = KeyFactory.getInstance("RSA"); final PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); return new KeyPair(publicKey, privateKey); } // If was a private EC key, it would already have been received as a PEMKeyPair return new KeyPair(null, privateKey); } }
private static PrivateKey convertToPrivateKey(Object pemObject, String type, PasswordSupplier pf) throws IOException { PrivateKeyInfo pki; try { pki = resolvePK(type, pemObject, pf); } catch (OperatorCreationException e) { throw new IOException("Can't initialize decryption infrastructure", e); } catch (PKCSException e) { throw new IOException("Error decrypting private key: the password is " + "incorrect or the " + type + " data is corrupted.", e); } JcaPEMKeyConverter converter = new JcaPEMKeyConverter(); return converter.getPrivateKey(pki); }
static PrivateKey getPrivateKeyFromBytes(byte[] data) throws IOException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException { final Reader pemReader = new StringReader(new String(data)); final PrivateKeyInfo pemPair; try (PEMParser pemParser = new PEMParser(pemReader)) { pemPair = (PrivateKeyInfo) pemParser.readObject(); } PrivateKey privateKey = new JcaPEMKeyConverter().setProvider(BouncyCastleProvider.PROVIDER_NAME).getPrivateKey( pemPair); return privateKey; }
public static PrivateKey loadKey(String file) throws IOException { PEMParser parser = new PEMParser(new FileReader(file)); try { PEMKeyPair pemObject = (PEMKeyPair) parser.readObject(); PrivateKeyInfo info=pemObject.getPrivateKeyInfo(); JcaPEMKeyConverter converter = new JcaPEMKeyConverter(); return converter.getPrivateKey(info); } finally { IOUtils.closeQuietly(parser); } }
static PrivateKey getPrivateKeyFromBytes(byte[] data) throws IOException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException { final Reader pemReader = new StringReader(new String(data)); final PrivateKeyInfo pemPair; try (final PEMParser pemParser = new PEMParser(pemReader)) { pemPair = (PrivateKeyInfo) pemParser.readObject(); } PrivateKey privateKey = new JcaPEMKeyConverter().setProvider(BouncyCastleProvider.PROVIDER_NAME) .getPrivateKey(pemPair); return privateKey; }
private static PrivateKey getPrivateKeyFromString(String data) throws IOException { final Reader pemReader = new StringReader(data); final PrivateKeyInfo pemPair; try (PEMParser pemParser = new PEMParser(pemReader)) { pemPair = (PrivateKeyInfo) pemParser.readObject(); } return new JcaPEMKeyConverter().getPrivateKey(pemPair); }
private static Key getPrivateKey(Object possiblePrivateKey) throws IOException { if (possiblePrivateKey instanceof PEMKeyPair) { return new JcaPEMKeyConverter().getKeyPair((PEMKeyPair) possiblePrivateKey) .getPrivate(); } else if (possiblePrivateKey instanceof PrivateKeyInfo) { return new JcaPEMKeyConverter().getPrivateKey((PrivateKeyInfo) possiblePrivateKey); } else { throw new IOException("Unable to parse private key, type unknown"); } } }
private static Key getPrivateKey(Object possiblePrivateKey) throws IOException { if (possiblePrivateKey instanceof PEMKeyPair) { return new JcaPEMKeyConverter().getKeyPair((PEMKeyPair) possiblePrivateKey) .getPrivate(); } else if (possiblePrivateKey instanceof PrivateKeyInfo) { return new JcaPEMKeyConverter().getPrivateKey((PrivateKeyInfo) possiblePrivateKey); } else { throw new IOException("Unable to parse private key, type unknown"); } } }
/** * 通过字节数组信息获取私钥 * * @param data 字节数组 * @return 私钥 */ private PrivateKey getPrivateKeyFromBytes(String data) throws IOException { final Reader pemReader = new StringReader(data); final PrivateKeyInfo pemPair; try (PEMParser pemParser = new PEMParser(pemReader)) { pemPair = (PrivateKeyInfo) pemParser.readObject(); } return new JcaPEMKeyConverter().setProvider(BouncyCastleProvider.PROVIDER_NAME).getPrivateKey(pemPair); }
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);