List of usage examples for org.bouncycastle.openpgp PGPPublicKeyEncryptedData isIntegrityProtected
public boolean isIntegrityProtected()
From source file:org.pgptool.gui.encryption.implpgp.EncryptionServicePgpImpl.java
License:Open Source License
/** * decrypt the passed in message stream. * /* ww w . j a va 2s. co m*/ * Inspired by * https://github.com/bcgit/bc-java/blob/master/pg/src/main/java/org/bouncycastle/openpgp/examples/KeyBasedFileProcessor.java * * @param countingStream * this stream is passed for progress reporting only, must not be * used to actually read data */ private void decryptStream(PGPPublicKeyEncryptedData pbe, PGPPrivateKey privateKey, OutputStream outputStream, Updater optionalProgress, CountingInputStream countingStream) throws UserRequestedCancellationException { try { InputStream clear = pbe.getDataStream(new BcPublicKeyDataDecryptorFactory(privateKey)); BcPGPObjectFactory plainFact = new BcPGPObjectFactory(clear); Object message = plainFact.nextObject(); if (message instanceof PGPMarker) { message = plainFact.nextObject(); } BcPGPObjectFactory pgpFactory = null; if (message instanceof PGPCompressedData) { PGPCompressedData cData = (PGPCompressedData) message; pgpFactory = new BcPGPObjectFactory(cData.getDataStream()); message = pgpFactory.nextObject(); } int watchDog = 0; while (message != null) { Preconditions.checkState(watchDog++ < 100, "Inifinite loop watch dog just hit"); if (message instanceof PGPLiteralData) { PGPLiteralData ld = (PGPLiteralData) message; // NOTE: We know initial file name (in case we need it): // ld.getFileName(); InputStream unc = ld.getInputStream(); OutputStream fOut = new BufferedOutputStream(outputStream); if (optionalProgress != null) { optionalProgress.updateStepInfo("progress.decrypting"); } pipeStream(unc, fOut, BUFFER_SIZE, optionalProgress, countingStream); fOut.close(); unc.close(); if (pbe.isIntegrityProtected()) { if (!pbe.verify()) { throw new RuntimeException("message failed integrity check"); } } return; } else if (message instanceof PGPOnePassSignatureList) { log.info("PGPOnePassSignatureList is not implemented yet. Skipping signature validation"); // NOTE: Here is a place to copyright from // http://stackoverflow.com/questions/19173181/bouncycastle-pgp-decrypt-and-verify Preconditions.checkArgument(pgpFactory != null, "File format is not supported. pgpFact is supposed to be initialized by that time"); message = pgpFactory.nextObject(); } else if (message instanceof PGPSignatureList) { log.info("PGPSignatureList is not implemented yet. Skipping signature validation"); Preconditions.checkArgument(pgpFactory != null, "File format is not supported. pgpFact is supposed to be initialized by that time"); message = pgpFactory.nextObject(); } else { throw new PGPException( "Don't know how to decrypt the input file. Encountered unexpected block: " + message); } } } catch (Throwable e) { Throwables.throwIfInstanceOf(e, UserRequestedCancellationException.class); throw new RuntimeException("Decryption failed", e); } }