本文整理匯總了Java中org.bouncycastle.openssl.PEMKeyPair.getPrivateKeyInfo方法的典型用法代碼示例。如果您正苦於以下問題:Java PEMKeyPair.getPrivateKeyInfo方法的具體用法?Java PEMKeyPair.getPrivateKeyInfo怎麽用?Java PEMKeyPair.getPrivateKeyInfo使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.bouncycastle.openssl.PEMKeyPair
的用法示例。
在下文中一共展示了PEMKeyPair.getPrivateKeyInfo方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: readPrivateKey
import org.bouncycastle.openssl.PEMKeyPair; //導入方法依賴的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);
}
示例2: decodePemEncodedPrivateKey
import org.bouncycastle.openssl.PEMKeyPair; //導入方法依賴的package包/類
@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);
}
}
示例3: extractRSAKeyPair
import org.bouncycastle.openssl.PEMKeyPair; //導入方法依賴的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;
}
示例4: apply
import org.bouncycastle.openssl.PEMKeyPair; //導入方法依賴的package包/類
@Override
public Object apply(Object input) {
if (input instanceof String && input.toString().contains("BEGIN RSA PRIVATE KEY")) {
try {
PEMKeyPair pemKeyPair = (PEMKeyPair) new PEMParser(new StringReader(input.toString())).readObject();
PrivateKeyInfo privateKeyInfo = pemKeyPair.getPrivateKeyInfo();
KeyFactory keyFact = KeyFactory.getInstance(
privateKeyInfo.getPrivateKeyAlgorithm().getAlgorithm().getId(), new BouncyCastleProvider());
return keyFact.generatePrivate(new PKCS8EncodedKeySpec(privateKeyInfo.getEncoded()));
} catch (Exception ex) {
return input;
}
}
return input;
}
示例5: getPrivateKey
import org.bouncycastle.openssl.PEMKeyPair; //導入方法依賴的package包/類
public static PrivateKey getPrivateKey(String privateKeyPem) throws IOException {
PEMParser pemParser = new PEMParser(new StringReader(privateKeyPem));
PEMKeyPair pemKeyPair = (PEMKeyPair) pemParser.readObject();
PrivateKeyInfo privateKeyInfo = pemKeyPair.getPrivateKeyInfo();
return new JcaPEMKeyConverter().getPrivateKey(privateKeyInfo);
}