List of usage examples for org.bouncycastle.openpgp PGPPrivateKey getKeyID
public long getKeyID()
From source file:com.google.e2e.bcdriver.Decryptor.java
License:Apache License
static final Result decrypt(InputStream in, PGPPrivateKey decryptKey, KeyChecker.PKR verify) throws IOException, PGPException, SignatureException { PGPObjectFactory pgpf = new PGPObjectFactory(PGPUtil.getDecoderStream(in), new BcKeyFingerprintCalculator()); Object o = pgpf.nextObject(); if (o == null) { throw new IOException("No encrypted content"); }//from w w w . j a v a 2s .c o m PGPEncryptedDataList enclist; if (o instanceof PGPEncryptedDataList) { enclist = (PGPEncryptedDataList) o; } else { enclist = (PGPEncryptedDataList) (pgpf.nextObject()); } Iterator<PGPPublicKeyEncryptedData> pkedi = Util.getTypedIterator(enclist.getEncryptedDataObjects(), PGPPublicKeyEncryptedData.class); if (pkedi == null) { throw new IOException("no encrypted data found!"); } while (pkedi.hasNext()) { PGPPublicKeyEncryptedData pked = pkedi.next(); if (pked.getKeyID() == decryptKey.getKeyID()) { return decryptSignedContent(pked, decryptKey, verify); } } return null; }
From source file:de.dentrassi.rpm.builder.SigningHelper.java
License:Open Source License
public static PGPPrivateKey loadKey(final Signature signature, final Logger logger) throws MojoFailureException, MojoExecutionException { if (signature == null) { return null; }/*from w ww.ja v a 2s. c o m*/ if (signature.isSkip()) { return null; } if (signature.getKeyId() == null || signature.getKeyId().isEmpty()) { throw new MojoFailureException(signature, "'keyId' parameter not set", "Signing requires the 'keyId' to the user id of the GPG key to use."); } if (signature.getKeyringFile() == null) { throw new MojoFailureException(signature, "'keyringFile' parameter not set", "Signing requires the 'keyringFile' to be set to a valid GPG keyring file, containing the secret keys."); } if (signature.getPassphrase() == null) { throw new MojoFailureException(signature, "'passphrase' parameter not set", "Signing requires the 'passphrase' parameter to be set."); } try (InputStream input = new FileInputStream(signature.getKeyringFile())) { final PGPPrivateKey privateKey = PgpHelper.loadPrivateKey(input, signature.getKeyId(), signature.getPassphrase()); if (privateKey == null) { throw new MojoFailureException(String.format("Unable to load GPG key '%s' from '%s'", signature.getKeyId(), signature.getKeyringFile())); } logger.info("Signing RPM - keyId: %016x", privateKey.getKeyID()); return privateKey; } catch (final PGPException | IOException e) { throw new MojoExecutionException("Failed to load private key for signing", e); } }
From source file:google.registry.keyring.api.ComparatorKeyring.java
License:Open Source License
@VisibleForTesting static boolean compare(@Nullable PGPPrivateKey a, @Nullable PGPPrivateKey b) { if (a == null || b == null) { return a == null && b == null; }//from w ww.j a va2 s. c o m return a.getKeyID() == b.getKeyID() && compare(a.getPrivateKeyDataPacket(), b.getPrivateKeyDataPacket()) && compare(a.getPublicKeyPacket(), b.getPublicKeyPacket()); }
From source file:google.registry.keyring.api.ComparatorKeyring.java
License:Open Source License
@VisibleForTesting static String stringify(PGPPrivateKey a) { if (a == null) { return "null"; }// www . j a v a 2 s. co m // We need to be careful what information we output here. The private key should be private, and // I'm not sure what is safe to put in the logs. return MoreObjects.toStringHelper(PGPPrivateKey.class).add("keyId", a.getKeyID()).toString(); }
From source file:google.registry.keyring.kms.KmsKeyringTest.java
License:Open Source License
@Test public void test_getRdeStagingDecryptionKey() throws Exception { savePrivateKeySecret("rde-staging-private"); PGPPrivateKey rdeStagingDecryptionKey = keyring.getRdeStagingDecryptionKey(); assertThat(rdeStagingDecryptionKey.getKeyID()) .isEqualTo(KmsTestHelper.getPrivateKeyring().getSecretKey().getKeyID()); }
From source file:google.registry.rde.BouncyCastleTest.java
License:Open Source License
@Test public void testEncryptDecrypt_ExplicitStyle() throws Exception { int bufferSize = 64 * 1024; // Alice loads Bob's "publicKey" into memory. PGPPublicKeyRing publicKeyRing = new BcPGPPublicKeyRing(PUBLIC_KEY); PGPPublicKey publicKey = publicKeyRing.getPublicKey(); // Alice encrypts the secret message for Bob using his "publicKey". PGPEncryptedDataGenerator encryptor = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(AES_128)); encryptor.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey)); byte[] encryptedData; try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { try (OutputStream output2 = encryptor.open(output, new byte[bufferSize])) { output2.write(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8)); }//from w w w .j a va 2s . com encryptedData = output.toByteArray(); } logger.info("Encrypted data: " + dumpHex(encryptedData)); // Bob loads his "privateKey" into memory. PGPSecretKeyRing privateKeyRing = new BcPGPSecretKeyRing(PRIVATE_KEY); PGPPrivateKey privateKey = extractPrivateKey(privateKeyRing.getSecretKey()); // Bob decrypt's the OpenPGP message (w/ ciphertext) using his "privateKey". try (ByteArrayInputStream input = new ByteArrayInputStream(encryptedData)) { PGPObjectFactory pgpFact = new BcPGPObjectFactory(input); PGPEncryptedDataList encDataList = (PGPEncryptedDataList) pgpFact.nextObject(); assertThat(encDataList.size()).isEqualTo(1); PGPPublicKeyEncryptedData encData = (PGPPublicKeyEncryptedData) encDataList.get(0); assertThat(encData.getKeyID()).isEqualTo(publicKey.getKeyID()); assertThat(encData.getKeyID()).isEqualTo(privateKey.getKeyID()); try (InputStream original = encData.getDataStream(new BcPublicKeyDataDecryptorFactory(privateKey))) { assertThat(CharStreams.toString(new InputStreamReader(original, UTF_8))) .isEqualTo(FALL_OF_HYPERION_A_DREAM); } } }
From source file:google.registry.rde.Ghostryde.java
License:Open Source License
/** * Opens a new {@link Decryptor} (Reading Step 1/3) * * <p>This is the first step in opening a ghostryde file. After this method, you'll want to * call {@link #openDecompressor(Decryptor)}. * * @param input is an {@link InputStream} of the ghostryde file data. * @param privateKey is the private encryption key of the recipient (which is us!) * @throws IOException/*from ww w . ja v a2 s .c o m*/ * @throws PGPException */ @CheckReturnValue public Decryptor openDecryptor(@WillNotClose InputStream input, PGPPrivateKey privateKey) throws IOException, PGPException { checkNotNull(privateKey, "privateKey"); PGPObjectFactory fact = new BcPGPObjectFactory(checkNotNull(input, "input")); PGPEncryptedDataList crypts = pgpCast(fact.nextObject(), PGPEncryptedDataList.class); checkState(crypts.size() > 0); if (crypts.size() > 1) { logger.warningfmt("crypts.size() is %d (should be 1)", crypts.size()); } PGPPublicKeyEncryptedData crypt = pgpCast(crypts.get(0), PGPPublicKeyEncryptedData.class); if (crypt.getKeyID() != privateKey.getKeyID()) { throw new PGPException(String.format("Message was encrypted for keyid %x but ours is %x", crypt.getKeyID(), privateKey.getKeyID())); } return new Decryptor(crypt.getDataStream(new BcPublicKeyDataDecryptorFactory(privateKey)), crypt); }
From source file:ubicrypt.core.crypto.PGPEC.java
License:Open Source License
public static InputStream decrypt(final PGPPrivateKey privateKey, final InputStream cipherText) throws PGPException { final JcaPGPObjectFactory pgpF = new JcaPGPObjectFactory(cipherText); try {//from www . ja va 2s .co m final PGPEncryptedDataList encList = (PGPEncryptedDataList) pgpF.nextObject(); log.trace("decrypt with sk:{}", privateKey.getKeyID()); final PGPPublicKeyEncryptedData encP = toStream( (Iterator<PGPPublicKeyEncryptedData>) encList.iterator()) .filter((PGPPublicKeyEncryptedData ed) -> { log.debug("pgp message encrypted with key:{}", ed.getKeyID()); return ed.getKeyID() == privateKey.getKeyID(); }).findFirst().orElseThrow(() -> new PGPException( "the message is not encrypted with the related public key")); try (InputStream clear = encP.getDataStream( new JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC").build(privateKey))) { Object next = new JcaPGPObjectFactory(clear).nextObject(); if (next instanceof PGPCompressedData) { next = new JcaPGPObjectFactory(((PGPCompressedData) next).getDataStream()).nextObject(); } return ((PGPLiteralData) next).getInputStream(); } } catch (final PGPException e) { throw e; } catch (final Exception e) { Throwables.propagate(e); } return null; }
From source file:ubicrypt.core.crypto.PGPECTest.java
License:Open Source License
@Test public void signPK() throws Exception { final char[] password = "ciao".toCharArray(); final PGPSecretKeyRing skr = PGPEC.createSecretKeyRing(password); final byte[] encSkr = skr.getEncoded(); final PGPKeyPair keyPair = PGPEC.extractEncryptKeyPair(PGPEC.readSK(new ByteArrayInputStream(encSkr)), "ciao".toCharArray()); final PGPKeyRingGenerator pkgen = PGPEC.keyRingGenerator(); final PGPSecretKeyRing targetSecRing = PGPEC.createSecretKeyRing("g".toCharArray()); final PGPPrivateKey priv = PGPEC.extractSignKey(targetSecRing, "g".toCharArray()); final PGPPublicKeyRing pkr = PGPPublicKeyRing.insertPublicKey(pkgen.generatePublicKeyRing(), PGPEC.signPK(keyPair.getPublicKey(), priv)); final byte[] pkis = pkr.getEncoded(); final List<PGPPublicKey> loadRing = PGPEC.readPKring(new ByteArrayInputStream(pkis)); assertThat(loadRing).hasSize(1);//from ww w. ja v a 2 s . com assertThat(Utils.toStream(loadRing.get(0).getKeySignatures()) .filter(sig -> ((PGPSignature) sig).getKeyID() == priv.getKeyID()).findFirst()).isPresent(); }