List of usage examples for org.bouncycastle.openpgp PGPSecretKeyRing PGPSecretKeyRing
public PGPSecretKeyRing(InputStream in, KeyFingerPrintCalculator fingerPrintCalculator) throws IOException, PGPException
From source file:com.google.e2e.bcdriver.Util.java
License:Apache License
static final PGPSecretKeyRing readSecretKeyRing(File path) throws IOException, PGPException { InputStream in = null;//from w w w . ja v a 2 s . c o m try { in = PGPUtil.getDecoderStream(new BufferedInputStream(new FileInputStream(path))); return new PGPSecretKeyRing(in, new BcKeyFingerprintCalculator()); } finally { if (in != null) { try { in.close(); } catch (IOException ignore) { ; // do nothing } } } }
From source file:com.google.gerrit.gpg.testutil.TestKey.java
License:Apache License
public TestKey(String pubArmored, String secArmored) { this.pubArmored = pubArmored; this.secArmored = secArmored; BcKeyFingerprintCalculator fc = new BcKeyFingerprintCalculator(); try {/* w ww . jav a 2s . c om*/ this.pubRing = new PGPPublicKeyRing(newStream(pubArmored), fc); this.secRing = new PGPSecretKeyRing(newStream(secArmored), fc); } catch (PGPException | IOException e) { throw new AssertionError(e); } }
From source file:com.google.gerrit.server.git.gpg.TestKey.java
License:Apache License
private TestKey(String pubArmored, String secArmored) throws PGPException, IOException { this.pubArmored = pubArmored; this.secArmored = secArmored; BcKeyFingerprintCalculator fc = new BcKeyFingerprintCalculator(); this.pub = new PGPPublicKeyRing(newStream(pubArmored), fc).getPublicKey(); this.sec = new PGPSecretKeyRing(newStream(secArmored), fc).getSecretKey(); }
From source file:org.kontalk.certgen.PGP.java
License:Open Source License
@SuppressWarnings("unchecked") public static PrivateKey convertPrivateKey(byte[] privateKeyData, String passphrase) throws PGPException, IOException { PGPDigestCalculatorProvider sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build(); PBESecretKeyDecryptor decryptor = new JcePBESecretKeyDecryptorBuilder(sha1Calc).setProvider(PGP.PROVIDER) .build(passphrase.toCharArray()); // load the secret key ring KeyFingerPrintCalculator fpr = new BcKeyFingerprintCalculator(); PGPSecretKeyRing secRing = new PGPSecretKeyRing(privateKeyData, fpr); // search and decrypt the master (signing key) // secret keys Iterator<PGPSecretKey> skeys = secRing.getSecretKeys(); while (skeys.hasNext()) { PGPSecretKey key = skeys.next(); PGPSecretKey sec = secRing.getSecretKey(); if (key.isMasterKey()) return convertPrivateKey(sec.extractPrivateKey(decryptor)); }/*from w ww .j a v a2s . c o m*/ throw new PGPException("no suitable private key found."); }
From source file:org.kontalk.certgen.X509Bridge.java
License:Open Source License
public static X509Certificate createCertificate(byte[] privateKeyData, byte[] publicKeyData, String passphrase, String subjectAltName)//ww w .j a va 2 s .c om throws PGPException, IOException, InvalidKeyException, IllegalStateException, NoSuchAlgorithmException, SignatureException, CertificateException, NoSuchProviderException, OperatorCreationException { KeyFingerPrintCalculator fpr = new BcKeyFingerprintCalculator(); PGPSecretKeyRing secRing = new PGPSecretKeyRing(privateKeyData, fpr); PGPPublicKeyRing pubRing = new PGPPublicKeyRing(publicKeyData, fpr); PGPDigestCalculatorProvider sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build(); PBESecretKeyDecryptor decryptor = new JcePBESecretKeyDecryptorBuilder(sha1Calc).setProvider(PGP.PROVIDER) .build(passphrase.toCharArray()); // secret key PGPSecretKey secKey = secRing.getSecretKey(); return createCertificate(pubRing, secKey.extractPrivateKey(decryptor), subjectAltName); }
From source file:org.kontalk.crypto.PersonalKey.java
License:Open Source License
/** Creates a {@link PersonalKey} from private and public key byte buffers. */ @SuppressWarnings("unchecked") public static PersonalKey load(byte[] privateKeyData, byte[] publicKeyData, char[] passphrase, byte[] bridgeCertData) throws KonException, IOException, PGPException, CertificateException, NoSuchProviderException { KeyFingerPrintCalculator fpr = new BcKeyFingerprintCalculator(); PGPSecretKeyRing secRing = new PGPSecretKeyRing(privateKeyData, fpr); PGPPublicKeyRing pubRing = new PGPPublicKeyRing(publicKeyData, fpr); PGPDigestCalculatorProvider sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build(); PBESecretKeyDecryptor decryptor = new JcePBESecretKeyDecryptorBuilder(sha1Calc) .setProvider(PGPUtils.PROVIDER).build(passphrase); PGPKeyPair signKp, encryptKp;/* w ww . java 2s . co m*/ PGPPublicKey signPub = null; PGPPrivateKey signPriv = null; PGPPublicKey encPub = null; PGPPrivateKey encPriv = null; // public keys Iterator<PGPPublicKey> pkeys = pubRing.getPublicKeys(); while (pkeys.hasNext()) { PGPPublicKey key = pkeys.next(); if (key.isMasterKey()) { // master (signing) key signPub = key; } else { // sub (encryption) key encPub = key; } } // secret keys Iterator<PGPSecretKey> skeys = secRing.getSecretKeys(); while (skeys.hasNext()) { PGPSecretKey key = skeys.next(); if (key.isMasterKey()) { // master (signing) key try { signPriv = key.extractPrivateKey(decryptor); } catch (PGPException ex) { throw new KonException(KonException.Error.LOAD_KEY_DECRYPT, ex); } } else { // sub (encryption) key encPriv = key.extractPrivateKey(decryptor); } } // X.509 bridge certificate X509Certificate bridgeCert = X509Bridge.load(bridgeCertData); if (encPriv == null || encPub == null || signPriv == null || signPub == null || bridgeCert == null) throw new PGPException("invalid key data"); signKp = new PGPKeyPair(signPub, signPriv); encryptKp = new PGPKeyPair(encPub, encPriv); return new PersonalKey(signKp, encryptKp, bridgeCert); }
From source file:org.kontalk.crypto.PGPUtils.java
License:Open Source License
public static PGPSecretKeyRing copySecretKeyRingWithNewPassword(byte[] privateKeyData, char[] oldPassphrase, char[] newPassphrase) throws PGPException, IOException { // load the secret key ring KeyFingerPrintCalculator fpr = new BcKeyFingerprintCalculator(); PGPSecretKeyRing secRing = new PGPSecretKeyRing(privateKeyData, fpr); PGPDigestCalculatorProvider sha1CalcProv = new JcaPGPDigestCalculatorProviderBuilder().build(); PBESecretKeyDecryptor decryptor = new JcePBESecretKeyDecryptorBuilder(sha1CalcProv) .setProvider(PGPUtils.PROVIDER).build(oldPassphrase); PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build() .get(HashAlgorithmTags.SHA1); PBESecretKeyEncryptor encryptor = new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, sha1Calc) .setProvider(PROVIDER).build(newPassphrase); return PGPSecretKeyRing.copyWithNewPassword(secRing, decryptor, encryptor); }
From source file:org.pgptool.gui.encryption.implpgp.KeyDataPgp.java
License:Open Source License
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject();// w ww. ja v a 2 s. c om try { if (ois.readBoolean()) { secretKeyRing = new PGPSecretKeyRing(initInputStream(ois), KeyFilesOperationsPgpImpl.fingerprintCalculator); } if (ois.readBoolean()) { publicKeyRing = new PGPPublicKeyRing(initInputStream(ois), KeyFilesOperationsPgpImpl.fingerprintCalculator); } } catch (PGPException e) { throw new IOException("Failed to read key", e); } }
From source file:org.sufficientlysecure.keychain.pgp.PgpKeyOperation.java
License:Open Source License
public PgpEditKeyResult createSecretKeyRing(SaveKeyringParcel saveParcel) { OperationLog log = new OperationLog(); int indent = 0; try {/* ww w .jav a 2 s. com*/ log.add(LogType.MSG_CR, indent); progress(R.string.progress_building_key, 0); indent += 1; if (saveParcel.mAddSubKeys.isEmpty()) { log.add(LogType.MSG_CR_ERROR_NO_MASTER, indent); return new PgpEditKeyResult(PgpEditKeyResult.RESULT_ERROR, log, null); } if (saveParcel.mAddUserIds.isEmpty()) { log.add(LogType.MSG_CR_ERROR_NO_USER_ID, indent); return new PgpEditKeyResult(PgpEditKeyResult.RESULT_ERROR, log, null); } SubkeyAdd add = saveParcel.mAddSubKeys.remove(0); if ((add.mFlags & KeyFlags.CERTIFY_OTHER) != KeyFlags.CERTIFY_OTHER) { log.add(LogType.MSG_CR_ERROR_NO_CERTIFY, indent); return new PgpEditKeyResult(PgpEditKeyResult.RESULT_ERROR, log, null); } if (add.mExpiry == null) { log.add(LogType.MSG_CR_ERROR_NULL_EXPIRY, indent); return new PgpEditKeyResult(PgpEditKeyResult.RESULT_ERROR, log, null); } Date creationTime = new Date(); subProgressPush(10, 30); PGPKeyPair keyPair = createKey(add, creationTime, log, indent); subProgressPop(); // return null if this failed (an error will already have been logged by createKey) if (keyPair == null) { return new PgpEditKeyResult(PgpEditKeyResult.RESULT_ERROR, log, null); } progress(R.string.progress_building_master_key, 40); // Build key encrypter and decrypter based on passphrase PGPDigestCalculator encryptorHashCalc = new JcaPGPDigestCalculatorProviderBuilder().build() .get(PgpSecurityConstants.SECRET_KEY_ENCRYPTOR_HASH_ALGO); PBESecretKeyEncryptor keyEncryptor = new JcePBESecretKeyEncryptorBuilder( PgpSecurityConstants.SECRET_KEY_ENCRYPTOR_SYMMETRIC_ALGO, encryptorHashCalc, PgpSecurityConstants.SECRET_KEY_ENCRYPTOR_S2K_COUNT) .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build("".toCharArray()); PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build() .get(PgpSecurityConstants.SECRET_KEY_SIGNATURE_CHECKSUM_HASH_ALGO); PGPSecretKey masterSecretKey = new PGPSecretKey(keyPair.getPrivateKey(), keyPair.getPublicKey(), sha1Calc, true, keyEncryptor); PGPSecretKeyRing sKR = new PGPSecretKeyRing(masterSecretKey.getEncoded(), new JcaKeyFingerprintCalculator()); subProgressPush(50, 100); CryptoInputParcel cryptoInput = new CryptoInputParcel(creationTime, new Passphrase("")); return internal(sKR, masterSecretKey, add.mFlags, add.mExpiry, cryptoInput, saveParcel, log, indent); } catch (PGPException e) { log.add(LogType.MSG_CR_ERROR_INTERNAL_PGP, indent); Log.e(Constants.TAG, "pgp error encoding key", e); return new PgpEditKeyResult(PgpEditKeyResult.RESULT_ERROR, log, null); } catch (IOException e) { Log.e(Constants.TAG, "io error encoding key", e); return new PgpEditKeyResult(PgpEditKeyResult.RESULT_ERROR, log, null); } }
From source file:org.sufficientlysecure.keychain.pgp.UncachedKeyringCanonicalizeTest.java
License:Open Source License
@Before public void setUp() throws Exception { // show Log.x messages in system.out ShadowLog.stream = System.out; ring = staticRing;/* ww w.j a v a2 s . c o m*/ subHashedPacketsGen = new PGPSignatureSubpacketGenerator(); secretKey = new PGPSecretKeyRing(ring.getEncoded(), new JcaKeyFingerprintCalculator()).getSecretKey(); }