List of usage examples for org.bouncycastle.openpgp PGPCompressedDataGenerator open
public OutputStream open(OutputStream out) throws IOException
From source file:alpha.offsync.security.OpenPGPSecurityUtility.java
License:Apache License
@Override public void encrypt(final OutputStream outputStream, final InputStream inputStream, final String[] keyInfo) { try {//from ww w. ja v a 2s . c o m // = // this.readPublicKey(this.publicKeyRing); final ArmoredOutputStream out = new ArmoredOutputStream(outputStream); try { final BcPGPDataEncryptorBuilder builder = new BcPGPDataEncryptorBuilder( SymmetricKeyAlgorithmTags.CAST5); builder.setSecureRandom(new SecureRandom()); final PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(builder, true); for (final String info : keyInfo) { final PGPPublicKey encKey = this.getEncryptionKey(info); if (encKey != null) { cPk.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(encKey)); } else { OpenPGPSecurityUtility.LOGGER .info("Encryption key for recipient " + info + " could not be found!"); } } final OutputStream cOut = cPk.open(out, new byte[1 << 16]); final PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator( CompressionAlgorithmTags.ZIP); final PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator(); final byte[] buffer = new byte[1 << 16]; final OutputStream pOut = lData.open(comData.open(cOut), PGPLiteralData.BINARY, "", new Date(), buffer); final byte[] buf = new byte[buffer.length]; int len; while ((len = inputStream.read(buf)) > 0) { pOut.write(buf, 0, len); } lData.close(); inputStream.close(); comData.close(); cOut.close(); out.close(); } catch (final PGPException e) { System.err.println(e); if (e.getUnderlyingException() != null) { e.getUnderlyingException().printStackTrace(); } } } catch (final IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:alpha.offsync.security.OpenPGPSecurityUtility.java
License:Apache License
@Override public void sign(final OutputStream outputStream, final InputStream inputStream, final String keyInfo) { try {// ww w. j av a 2 s. c o m final File keyFile = this.secretKeyRing; final char[] pass = this.secretKeyRingPassword; final ArmoredOutputStream out = new ArmoredOutputStream(outputStream); final PGPSecretKey pgpSec = this.getSignKey(keyInfo); // readSecretKey(new // FileInputStream(keyFile)); final PGPPrivateKey pgpPrivKey = pgpSec.extractPrivateKey( new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(pass)); final PGPSignatureGenerator sGen = new PGPSignatureGenerator( new BcPGPContentSignerBuilder(pgpSec.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1)); sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey); final Iterator it = pgpSec.getPublicKey().getUserIDs(); if (it.hasNext()) { final PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); spGen.setSignerUserID(false, (String) it.next()); sGen.setHashedSubpackets(spGen.generate()); } final PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(CompressionAlgorithmTags.ZLIB); final BCPGOutputStream bOut = new BCPGOutputStream(cGen.open(out)); sGen.generateOnePassVersion(false).encode(bOut); final PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator(); final byte[] buffer = new byte[1 << 16]; final OutputStream lOut = lGen.open(bOut, PGPLiteralData.BINARY, "", new Date(), buffer); int ch = 0; while ((ch = inputStream.read()) >= 0) { lOut.write(ch); sGen.update((byte) ch); } lGen.close(); sGen.generate().encode(bOut); cGen.close(); out.close(); } catch (final FileNotFoundException e) { e.printStackTrace(); } catch (final IOException e) { e.printStackTrace(); } catch (final PGPException e) { e.printStackTrace(); } catch (final SignatureException e) { e.printStackTrace(); } }
From source file:com.geekcommune.identity.EncryptionUtil.java
License:Open Source License
/** * UNUSED IN FRIENDLY BACKUP//from w w w . ja v a 2 s. co m * Sign the passed in message stream (version 3 signature) */ private void signDataV3(File inFile, OutputStream aOut, PGPPublicKey publicKey, PGPPrivateKey privateKey) throws PGPException { try { PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(PGPCompressedData.ZIP); BCPGOutputStream bOut = new BCPGOutputStream(cGen.open(aOut)); PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator(true); PGPV3SignatureGenerator s3Gen = new PGPV3SignatureGenerator(publicKey.getAlgorithm(), PGPUtil.SHA1, "BC"); s3Gen.initSign(PGPSignature.BINARY_DOCUMENT, privateKey); s3Gen.generateOnePassVersion(false).encode(bOut); OutputStream lOut = lGen.open(bOut, PGPLiteralData.BINARY, inFile); FileInputStream fIn = new FileInputStream(inFile); int ch; while ((ch = fIn.read()) >= 0) { lOut.write(ch); s3Gen.update((byte) ch); } fIn.close(); // close() finishes the writing of the literal data and flushes the stream // It does not close bOut so this is ok here lGen.close(); // Generate the signature s3Gen.generate().encode(bOut); // Must not close bOut here bOut.finish(); bOut.flush(); cGen.close(); } catch (PGPException e) { throw e; } catch (Exception e) { throw new PGPException("Error in signing", e); } }
From source file:com.geekcommune.identity.EncryptionUtil.java
License:Open Source License
/** * Sign the passed in message stream/*from w ww .j a v a2 s. c o m*/ */ private void signData(File inFile, OutputStream aOut, PGPPublicKey publicKey, PGPPrivateKey privateKey) throws PGPException { try { PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(PGPCompressedData.ZIP); BCPGOutputStream bOut = new BCPGOutputStream(cGen.open(aOut)); PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator(); PGPSignatureGenerator sGen = new PGPSignatureGenerator(publicKey.getAlgorithm(), PGPUtil.SHA1, "BC"); sGen.initSign(PGPSignature.BINARY_DOCUMENT, privateKey); @SuppressWarnings("unchecked") Iterator<String> users = publicKey.getUserIDs(); if (users.hasNext()) { PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); spGen.setSignerUserID(false, users.next()); sGen.setHashedSubpackets(spGen.generate()); } sGen.generateOnePassVersion(false).encode(bOut); OutputStream lOut = lGen.open(bOut, PGPLiteralData.BINARY, inFile); FileInputStream fIn = new FileInputStream(inFile); int ch; while ((ch = fIn.read()) >= 0) { lOut.write(ch); sGen.update((byte) ch); } fIn.close(); // close() finishes the writing of the literal data and flushes the stream // It does not close bOut so this is ok here lGen.close(); // Generate the signature sGen.generate().encode(bOut); // Must not close bOut here bOut.finish(); bOut.flush(); cGen.close(); } catch (PGPException e) { throw e; } catch (Exception e) { throw new PGPException("Error in signing", e); } }
From source file:com.geekcommune.identity.EncryptionUtil.java
License:Open Source License
/** * Compress the data in the input stream *///w ww .j av a 2 s . c o m public void compressData(InputStream fIn, OutputStream bOut, String fileName, long dataLength, Date date, boolean oldFormat, Format compress) throws PGPException { try { if (compress == Format.COMPRESSED) { PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedData.ZIP); bOut = comData.open(bOut); } PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator(oldFormat); OutputStream pOut = lData.open(bOut, PGPLiteralData.BINARY, fileName, dataLength, date); byte[] bytes = new byte[4096]; int len; while ((len = fIn.read(bytes)) > 0) { pOut.write(bytes, 0, len); } fIn.close(); lData.close(); if (compress == Format.COMPRESSED) { bOut.close(); } } catch (Exception e) { throw new PGPException("Error in encryption", e); } }
From source file:com.geoxp.oss.CryptoHelper.java
License:Apache License
public static byte[] encryptPGP(byte[] data, PGPPublicKey key, boolean armored, String name, int compressionAlgorithm, int encAlgorithm) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream out = armored ? new ArmoredOutputStream(baos) : baos; BcPGPDataEncryptorBuilder dataEncryptor = new BcPGPDataEncryptorBuilder(encAlgorithm); dataEncryptor.setWithIntegrityPacket(true); dataEncryptor.setSecureRandom(CryptoHelper.getSecureRandom()); PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(dataEncryptor); encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(key)); try {/*from w w w .j a v a 2 s. c om*/ OutputStream encout = encryptedDataGenerator.open(out, 1024); PGPCompressedDataGenerator pgpcdg = new PGPCompressedDataGenerator(compressionAlgorithm); OutputStream compout = pgpcdg.open(encout); PGPLiteralDataGenerator pgpldg = new PGPLiteralDataGenerator(false); OutputStream ldout = pgpldg.open(compout, PGPLiteralData.BINARY, name, data.length, PGPLiteralData.NOW); ldout.write(data); ldout.close(); compout.close(); encout.close(); out.close(); baos.close(); return baos.toByteArray(); } catch (PGPException pgpe) { throw new IOException(pgpe); } }
From source file:com.ginema.crypto.encryption.PGPEncryption.java
License:Apache License
/** * Simple PGP encryptor between byte[].// w ww. jav a2s. c o m * * @param clearData The test to be encrypted * @param passPhrase The pass phrase (key). This method assumes that the key is a simple pass * phrase, and does not yet support RSA or more sophisiticated keying. * @param fileName File name. This is used in the Literal Data Packet (tag 11) which is really * inly important if the data is to be related to a file to be recovered later. Because * this routine does not know the source of the information, the caller can set something * here for file name use that will be carried. If this routine is being used to encrypt * SOAP MIME bodies, for example, use the file name from the MIME type, if applicable. Or * anything else appropriate. * * @param armor * * @return encrypted data. * @exception IOException * @exception PGPException * @exception NoSuchProviderException */ private static byte[] _doEncrypt(byte[] clearData, PGPPublicKey encKey, String fileName, boolean withIntegrityCheck, boolean armor) throws IOException, PGPException, NoSuchProviderException { if (fileName == null) { fileName = PGPLiteralData.CONSOLE; } ByteArrayOutputStream encOut = new ByteArrayOutputStream(); OutputStream out = encOut; if (armor) { out = new ArmoredOutputStream(out); } ByteArrayOutputStream bOut = new ByteArrayOutputStream(); PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedDataGenerator.ZIP); OutputStream cos = comData.open(bOut); // open it with the final // destination PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator(); // we want to generate compressed data. This might be a user option // later, // in which case we would pass in bOut. OutputStream pOut = lData.open(cos, // the compressed output stream PGPLiteralData.BINARY, fileName, // "filename" to store clearData.length, // length of clear data new Date() // current time ); pOut.write(clearData); lData.close(); comData.close(); PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(PGPEncryptedData.CAST5, withIntegrityCheck, new SecureRandom(), "BC"); cPk.addMethod(encKey); byte[] bytes = bOut.toByteArray(); OutputStream cOut = cPk.open(out, bytes.length); cOut.write(bytes); // obtain the actual bytes from the compressed stream cOut.close(); out.close(); return encOut.toByteArray(); }
From source file:com.google.gerrit.server.contact.EncryptedContactStore.java
License:Apache License
private static byte[] compress(final String fileName, Date fileDate, final byte[] plainText) throws IOException { final ByteArrayOutputStream buf = new ByteArrayOutputStream(); final PGPCompressedDataGenerator comdg; final int len = plainText.length; if (fileDate == null) { fileDate = PGPLiteralData.NOW;/*from ww w.j a v a 2 s . c om*/ } comdg = new PGPCompressedDataGenerator(PGPCompressedData.ZIP); final OutputStream out = new PGPLiteralDataGenerator().open(comdg.open(buf), PGPLiteralData.BINARY, fileName, len, fileDate); out.write(plainText); out.close(); comdg.close(); return buf.toByteArray(); }
From source file:com.lyndir.lhunath.opal.crypto.gpg.GPG.java
License:Apache License
/** * PGP Encrypt a stream.//from www . j av a2 s .c o m * * @param plainTextStream The stream that contains the plain-text data. * @param publicKey The public key to use for encryption. * @param armoured {@code true}: ASCII armor the encrypted data. * * @return The encrypted data stream. * * @throws NoSuchProviderException * @throws IOException * @throws PGPException */ public static InputStream encrypt(final InputStream plainTextStream, final PGPPublicKey publicKey, final boolean armoured) throws IOException, NoSuchProviderException, PGPException { /* Compress and extract literal data packets that can be encrypted. */ PGPEncryptedDataGenerator encryptedDataGenerator = null; try (ByteArrayOutputStream decryptedStream = new ByteArrayOutputStream(); ByteArrayOutputStream encryptedByteStream = new ByteArrayOutputStream()) { PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator(); PGPCompressedDataGenerator compressor = new PGPCompressedDataGenerator(CompressionAlgorithmTags.ZLIB); OutputStream literalStream = literalDataGenerator.open(compressor.open(decryptedStream), PGPLiteralData.BINARY, "", new Date(), new byte[4096]); ByteStreams.copy(plainTextStream, literalStream); compressor.close(); /* Encrypt compressed data. */ encryptedDataGenerator = new PGPEncryptedDataGenerator(SymmetricKeyAlgorithmTags.CAST5, new SecureRandom(), BouncyCastleProvider.PROVIDER_NAME); encryptedDataGenerator.addMethod(publicKey); /* Create the encrypted output stream, armour if necessary. */ OutputStream encryptedStream = encryptedByteStream; if (armoured) encryptedStream = new ArmoredOutputStream(encryptedStream); /* Create and write out the encrypted file. */ OutputStream encryptionStream = encryptedDataGenerator.open(encryptedStream, new byte[4096]); ByteStreams.copy(new ByteArrayInputStream(decryptedStream.toByteArray()), encryptionStream); return new ByteArrayInputStream(encryptedByteStream.toByteArray()); } finally { if (encryptedDataGenerator != null) encryptedDataGenerator.close(); } }
From source file:com.verhas.licensor.License.java
License:Open Source License
/** * Encode the currently loaded/created license. * /*from www . j av a2 s . c o m*/ * @param keyPassPhraseString * the pass phrase to the signing key that was loaded. * @return the license encoded as ascii string. * @throws java.io.IOException * @throws java.security.NoSuchAlgorithmException * @throws java.security.NoSuchProviderException * @throws org.bouncycastle.openpgp.PGPException * @throws java.security.SignatureException */ public String encodeLicense(final String keyPassPhraseString) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, PGPException, SignatureException { final char[] keyPassPhrase = keyPassPhraseString.toCharArray(); final String licensePlain = getLicenseString(); final ByteArrayOutputStream baOut = new ByteArrayOutputStream(); final OutputStream out = new ArmoredOutputStream(baOut); final PGPPrivateKey pgpPrivKey = key.extractPrivateKey(keyPassPhrase, "BC"); final PGPSignatureGenerator sGen = new PGPSignatureGenerator(key.getPublicKey().getAlgorithm(), hashAlgorithm, "BC"); sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey); @SuppressWarnings("unchecked") final Iterator<String> it = key.getPublicKey().getUserIDs(); if (it.hasNext()) { final PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); spGen.setSignerUserID(false, it.next()); sGen.setHashedSubpackets(spGen.generate()); } final PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(PGPCompressedData.ZLIB); final BCPGOutputStream bOut = new BCPGOutputStream(cGen.open(out)); sGen.generateOnePassVersion(false).encode(bOut); final PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator(); final OutputStream lOut = lGen.open(bOut, PGPLiteralData.BINARY, "licenseFileName-Ignored", new Date(), new byte[1024]); final InputStream fIn = new ByteArrayInputStream(licensePlain.getBytes("utf-8")); int ch = 0; while ((ch = fIn.read()) >= 0) { lOut.write(ch); sGen.update((byte) ch); } lGen.close(); sGen.generate().encode(bOut); cGen.close(); out.close(); return new String(baOut.toByteArray()); }