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