org.bouncycastle.openpgp.PGPCompressedData Java Examples
The following examples show how to use
org.bouncycastle.openpgp.PGPCompressedData.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BouncyCastleTest.java From nomulus with Apache License 2.0 | 6 votes |
@Test public void testCompressDecompress() throws Exception { // Compress the data and write out a compressed data OpenPGP message. byte[] data; try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { PGPCompressedDataGenerator kompressor = new PGPCompressedDataGenerator(ZIP); try (OutputStream output2 = kompressor.open(output)) { output2.write(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8)); } data = output.toByteArray(); } logger.atInfo().log("Compressed data: %s", dumpHex(data)); // Decompress the data. try (ByteArrayInputStream input = new ByteArrayInputStream(data)) { PGPObjectFactory pgpFact = new BcPGPObjectFactory(input); PGPCompressedData object = (PGPCompressedData) pgpFact.nextObject(); InputStream original = object.getDataStream(); // Closing this would close input. assertThat(CharStreams.toString(new InputStreamReader(original, UTF_8))) .isEqualTo(FALL_OF_HYPERION_A_DREAM); assertThat(pgpFact.nextObject()).isNull(); } }
Example #2
Source File: EncryptionServicePgpImpl.java From pgptool with GNU General Public License v3.0 | 5 votes |
private static void doEncryptFile(InputStream pIn, SourceInfo encryptionSourceInfo, OutputStream out, PGPEncryptedDataGenerator encDataGen, Updater progress, char outputType) throws IOException, NoSuchProviderException, PGPException, UserRequestedCancellationException { OutputStream encryptedStream = encDataGen.open(out, new byte[BUFFER_SIZE]); PGPCompressedDataGenerator compressedDataGen = new PGPCompressedDataGenerator(PGPCompressedData.ZIP); OutputStream compressedStream = compressedDataGen.open(encryptedStream); estimateFullOperationSize(encryptionSourceInfo, progress); writeFileToLiteralData(pIn, encryptionSourceInfo, compressedStream, outputType, new byte[BUFFER_SIZE], progress); compressedDataGen.close(); encryptedStream.close(); }
Example #3
Source File: PGPEncryptionUtil.java From peer-os with Apache License 2.0 | 5 votes |
/** * *********************************************** */ private static PGPLiteralData asLiteral( final InputStream clear ) throws IOException, PGPException { final PGPObjectFactory plainFact = new PGPObjectFactory( clear, new JcaKeyFingerprintCalculator() ); final Object message = plainFact.nextObject(); if ( message instanceof PGPCompressedData ) { final PGPCompressedData cData = ( PGPCompressedData ) message; final PGPObjectFactory pgpFact = new PGPObjectFactory( cData.getDataStream(), new JcaKeyFingerprintCalculator() ); // Find the first PGPLiteralData object Object object = null; for ( int safety = 0; ( safety++ < 1000 ) && !( object instanceof PGPLiteralData ); object = pgpFact.nextObject() ) { //ignore } return ( PGPLiteralData ) object; } else if ( message instanceof PGPLiteralData ) { return ( PGPLiteralData ) message; } else if ( message instanceof PGPOnePassSignatureList ) { throw new PGPException( "encrypted message contains a signed message - not literal data." ); } else { throw new PGPException( "message is not a simple encrypted file - type unknown: " + message.getClass().getName() ); } }
Example #4
Source File: RydeCompression.java From nomulus with Apache License 2.0 | 5 votes |
/** * Creates an InputStream that decompresses the data. * * <p>TODO(b/110465964): document where the input comes from / output goes to. Something like * documenting that input is the result of openDecryptor and the result goes into openFileDecoder. */ @CheckReturnValue static ImprovedInputStream openDecompressor(@WillNotClose InputStream input) { try { PGPCompressedData compressed = PgpUtils.readSinglePgpObject(input, PGPCompressedData.class); return new ImprovedInputStream("RydeDecompressor", compressed.getDataStream()); } catch (PGPException e) { throw new RuntimeException(e); } }
Example #5
Source File: PGPEncryptionUtil.java From OpenAs2App with BSD 2-Clause "Simplified" License | 5 votes |
public PGPEncryptionUtil(PGPPublicKey key, String payloadFilename, OutputStream out) throws PGPException, NoSuchProviderException, IOException { BcPGPDataEncryptorBuilder builder = new BcPGPDataEncryptorBuilder(payloadEncryptAlg); builder.setSecureRandom(new SecureRandom()); // create an encrypted payload and set the public key on the data // generator PGPEncryptedDataGenerator encryptGen = new PGPEncryptedDataGenerator(builder, supportPGP2_6); encryptGen.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(key)); // open an output stream connected to the encrypted data generator // and have the generator write its data out to the ascii-encoding // stream byte[] buffer = new byte[BUFFER_SIZE]; // write data out using "ascii-armor" encoding if enabled - this is the normal PGP text output. encryptedOut = encryptGen.open(isArmor ? new ArmoredOutputStream(out) : out, buffer); // add a data compressor if compression is enabled else just write the encrypted stream to the literal PGPLiteralDataGenerator literalGen = new PGPLiteralDataGenerator(); if (isCompressData) { // compress data. before encryption ... far better compression on unencrypted data. PGPCompressedDataGenerator compressor = new PGPCompressedDataGenerator(PGPCompressedData.ZIP); compressedOut = compressor.open(encryptedOut); literalOut = literalGen.open(compressedOut, PGPLiteralDataGenerator.UTF8, payloadFilename, new Date(), new byte[BUFFER_SIZE]); } else { literalOut = literalGen.open(encryptedOut, PGPLiteralDataGenerator.UTF8, payloadFilename, new Date(), new byte[BUFFER_SIZE]); } }
Example #6
Source File: GPGFileDecryptor.java From incubator-gobblin with Apache License 2.0 | 5 votes |
/** * Move to the next {@link InputStream} if available, otherwise set {@link #currentUnderlyingStream} to null to * indicate that there is no more data. * @throws IOException */ private void moveToNextInputStream() throws IOException { Object pgpfObject = this.pgpFact.nextObject(); // no more data if (pgpfObject == null) { this.currentUnderlyingStream = null; return; } if (pgpfObject instanceof PGPCompressedData) { PGPCompressedData cData = (PGPCompressedData) pgpfObject; try { this.pgpFact = new JcaPGPObjectFactory(cData.getDataStream()); } catch (PGPException e) { throw new IOException("Could not get the PGP data stream", e); } pgpfObject = this.pgpFact.nextObject(); } if (pgpfObject instanceof PGPLiteralData) { this.currentUnderlyingStream = ((PGPLiteralData) pgpfObject).getInputStream(); } else if (pgpfObject instanceof PGPOnePassSignatureList) { throw new IOException("encrypted message contains PGPOnePassSignatureList message - not literal data."); } else if (pgpfObject instanceof PGPSignatureList) { throw new IOException("encrypted message contains PGPSignatureList message - not literal data."); } else { throw new IOException("message is not a simple encrypted file - type unknown."); } }
Example #7
Source File: PGPSign.java From peer-os with Apache License 2.0 | 4 votes |
public static byte[] sign( byte data[], PGPPrivateKey privateKey ) throws IOException, PGPException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ArmoredOutputStream aos = new ArmoredOutputStream( bos ); PGPCompressedDataGenerator compressGen = new PGPCompressedDataGenerator( PGPCompressedData.ZLIB ); BCPGOutputStream bcOut = new BCPGOutputStream( compressGen.open( aos ) ); PGPSignatureGenerator signGen = getSignatureGenerator( privateKey, bcOut ); produceSign( data, bcOut, signGen ); compressGen.close(); aos.close(); return bos.toByteArray(); }
Example #8
Source File: PGPDecrypt.java From peer-os with Apache License 2.0 | 4 votes |
private static InputStream getInputStream( PGPPrivateKey privateKey, PGPPublicKeyEncryptedData pgpEncData ) throws PGPException, IOException { InputStream is = pgpEncData .getDataStream( new JcePublicKeyDataDecryptorFactoryBuilder().setProvider( "BC" ).build( privateKey ) ); JcaPGPObjectFactory objectFactory = new JcaPGPObjectFactory( is ); Object message = objectFactory.nextObject(); PGPCompressedData compressedData = ( PGPCompressedData ) message; JcaPGPObjectFactory pgpObjectFactory = new JcaPGPObjectFactory( compressedData.getDataStream() ); PGPLiteralData literalData = ( PGPLiteralData ) pgpObjectFactory.nextObject(); return literalData.getInputStream(); }
Example #9
Source File: PGPEncryptionUtil.java From peer-os with Apache License 2.0 | 4 votes |
public static byte[] sign( byte[] message, PGPSecretKey secretKey, String secretPwd, boolean armor ) throws PGPException { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStream theOut = armor ? new ArmoredOutputStream( out ) : out; PGPPrivateKey pgpPrivKey = secretKey.extractPrivateKey( new JcePBESecretKeyDecryptorBuilder().setProvider( provider ).build( secretPwd.toCharArray() ) ); PGPSignatureGenerator sGen = new PGPSignatureGenerator( new JcaPGPContentSignerBuilder( secretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1 ) .setProvider( provider ) ); sGen.init( PGPSignature.BINARY_DOCUMENT, pgpPrivKey ); Iterator it = secretKey.getPublicKey().getUserIDs(); if ( it.hasNext() ) { PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); spGen.setSignerUserID( false, ( String ) it.next() ); sGen.setHashedSubpackets( spGen.generate() ); } PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator( PGPCompressedData.ZLIB ); BCPGOutputStream bOut = new BCPGOutputStream( cGen.open( theOut ) ); sGen.generateOnePassVersion( false ).encode( bOut ); PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator(); OutputStream lOut = lGen.open( bOut, PGPLiteralData.BINARY, "filename", new Date(), new byte[4096] ); // InputStream fIn = new ByteArrayInputStream( message ); int ch; while ( ( ch = fIn.read() ) >= 0 ) { lOut.write( ch ); sGen.update( ( byte ) ch ); } lGen.close(); sGen.generate().encode( bOut ); cGen.close(); theOut.close(); return out.toByteArray(); } catch ( Exception e ) { throw new PGPException( "Error in sign", e ); } }
Example #10
Source File: Encryptor.java From desktopclient-java with GNU General Public License v3.0 | 4 votes |
/** * Encrypt, sign and write input stream data to output stream. * Input and output stream are closed. */ private static void encryptAndSign( InputStream plainInput, OutputStream encryptedOutput, PersonalKey myKey, List<PGPUtils.PGPCoderKey> receiverKeys) throws IOException, PGPException { // setup data encryptor & generator BcPGPDataEncryptorBuilder encryptor = new BcPGPDataEncryptorBuilder(PGPEncryptedData.AES_192); encryptor.setWithIntegrityPacket(true); encryptor.setSecureRandom(new SecureRandom()); // add public key recipients PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(encryptor); receiverKeys.forEach(key -> encGen.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(key.encryptKey))); OutputStream encryptedOut = encGen.open(encryptedOutput, new byte[BUFFER_SIZE]); // setup compressed data generator PGPCompressedDataGenerator compGen = new PGPCompressedDataGenerator(PGPCompressedData.ZIP); OutputStream compressedOut = compGen.open(encryptedOut, new byte[BUFFER_SIZE]); // setup signature generator int algo = myKey.getSigningAlgorithm(); PGPSignatureGenerator sigGen = new PGPSignatureGenerator( new BcPGPContentSignerBuilder(algo, HashAlgorithmTags.SHA256)); sigGen.init(PGPSignature.BINARY_DOCUMENT, myKey.getPrivateSigningKey()); PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); spGen.setSignerUserID(false, myKey.getUserId()); sigGen.setUnhashedSubpackets(spGen.generate()); sigGen.generateOnePassVersion(false).encode(compressedOut); // Initialize literal data generator PGPLiteralDataGenerator literalGen = new PGPLiteralDataGenerator(); OutputStream literalOut = literalGen.open( compressedOut, PGPLiteralData.BINARY, "", new Date(), new byte[BUFFER_SIZE]); // read the "in" stream, compress, encrypt and write to the "out" stream // this must be done if clear data is bigger than the buffer size // but there are other ways to optimize... byte[] buf = new byte[BUFFER_SIZE]; int len; while ((len = plainInput.read(buf)) > 0) { literalOut.write(buf, 0, len); sigGen.update(buf, 0, len); } literalGen.close(); // generate the signature, compress, encrypt and write to the "out" stream sigGen.generate().encode(compressedOut); compGen.close(); encGen.close(); }
Example #11
Source File: PGPVerify.java From peer-os with Apache License 2.0 | 3 votes |
private static JcaPGPObjectFactory getObjectFactory( byte signedData[] ) throws IOException, PGPException { InputStream in = PGPUtil.getDecoderStream( new ByteArrayInputStream( signedData ) ); JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory( in ); PGPCompressedData compressedData = ( PGPCompressedData ) pgpFact.nextObject(); return new JcaPGPObjectFactory( compressedData.getDataStream() ); }