use of org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor in project nifi by apache.
the class OpenPGPKeyBasedEncryptor method getDecryptedPrivateKey.
private static PGPPrivateKey getDecryptedPrivateKey(String provider, String secretKeyringFile, long keyId, char[] passphrase) throws IOException, PGPException {
// Read in from the secret keyring file
try (FileInputStream keyInputStream = new FileInputStream(secretKeyringFile)) {
// Form the SecretKeyRing collection (1.53 way with fingerprint calculator)
PGPSecretKeyRingCollection pgpSecretKeyRingCollection = new PGPSecretKeyRingCollection(keyInputStream, new BcKeyFingerprintCalculator());
// The decryptor is identical for all keys
final PBESecretKeyDecryptor decryptor = new JcePBESecretKeyDecryptorBuilder().setProvider(provider).build(passphrase);
// Iterate over all secret keyrings
Iterator<PGPSecretKeyRing> keyringIterator = pgpSecretKeyRingCollection.getKeyRings();
PGPSecretKeyRing keyRing;
PGPSecretKey secretKey;
while (keyringIterator.hasNext()) {
keyRing = keyringIterator.next();
// If keyId exists, get a specific secret key; else, iterate over all
if (keyId != 0) {
secretKey = keyRing.getSecretKey(keyId);
try {
return secretKey.extractPrivateKey(decryptor);
} catch (Exception e) {
throw new PGPException("No private key available using passphrase", e);
}
} else {
Iterator<PGPSecretKey> keyIterator = keyRing.getSecretKeys();
while (keyIterator.hasNext()) {
secretKey = keyIterator.next();
try {
return secretKey.extractPrivateKey(decryptor);
} catch (Exception e) {
// TODO: Log (expected) failures?
}
}
}
}
}
// If this point is reached, no private key could be extracted with the given passphrase
throw new PGPException("No private key available using passphrase");
}
use of org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor in project jPOS by jpos.
the class PGPHelper method findSecretKey.
private static PGPPrivateKey findSecretKey(PGPSecretKeyRingCollection pgpSec, long keyID, char[] pass) throws PGPException, NoSuchProviderException {
PGPSecretKey pgpSecKey = pgpSec.getSecretKey(keyID);
if (pgpSecKey == null) {
return null;
}
PBESecretKeyDecryptor decryptor = new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(pass);
return pgpSecKey.extractPrivateKey(decryptor);
}
use of org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor in project ant-ivy by apache.
the class OpenPGPSignatureGenerator method sign.
public void sign(File src, File dest) throws IOException {
OutputStream out = null;
InputStream in = null;
InputStream keyIn = null;
try {
if (secring == null) {
secring = System.getProperty("user.home") + "/.gnupg/secring.gpg";
}
if (pgpSec == null) {
keyIn = new FileInputStream(secring);
pgpSec = readSecretKey(keyIn);
}
PBESecretKeyDecryptor decryptor = new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(password.toCharArray());
PGPPrivateKey pgpPrivKey = pgpSec.extractPrivateKey(decryptor);
PGPSignatureGenerator sGen = new PGPSignatureGenerator(new BcPGPContentSignerBuilder(pgpSec.getPublicKey().getAlgorithm(), PGPUtil.SHA1));
sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);
in = new FileInputStream(src);
out = new BCPGOutputStream(new ArmoredOutputStream(new FileOutputStream(dest)));
int ch = 0;
while ((ch = in.read()) >= 0) {
sGen.update((byte) ch);
}
sGen.generate().encode(out);
} catch (PGPException e) {
throw new IOException(e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (keyIn != null) {
try {
keyIn.close();
} catch (IOException e) {
}
}
}
}
Aggregations