List of usage examples for org.bouncycastle.bcpg ArmoredOutputStream ArmoredOutputStream
public ArmoredOutputStream(OutputStream out)
From source file:SELSKeyGen.java
License:Open Source License
private static void exportKeyPair(OutputStream secretOut, OutputStream publicOut, KeyPair dsaKp, KeyPair elgKp, String identity, char[] passPhrase, boolean armor, int exptimesec) throws IOException, InvalidKeyException, NoSuchProviderException, SignatureException, PGPException { if ((armor) && (secretOut != null)) { secretOut = new ArmoredOutputStream(secretOut); }// w w w. j a v a 2 s. c o m //Create subpacket vector for expiration time PGPSignatureSubpacketGenerator subpacketGenerator = new PGPSignatureSubpacketGenerator(); int secondsToExpire = exptimesec; subpacketGenerator.setKeyExpirationTime(false, secondsToExpire); subpacketGenerator.setExportable(true, true); PGPSignatureSubpacketVector subpacketVector = subpacketGenerator.generate(); PGPKeyPair dsaKeyPair = new PGPKeyPair(PGPPublicKey.DSA, dsaKp, new Date(), "BC"); PGPKeyPair elgKeyPair = new PGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elgKp, new Date(), "BC"); PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, dsaKeyPair, identity, PGPEncryptedData.AES_256, passPhrase, subpacketVector, null, new SecureRandom(), "BC"); keyRingGen.addSubKey(elgKeyPair); if (secretOut != null) { keyRingGen.generateSecretKeyRing().encode(secretOut); secretOut.close(); } if (armor) { publicOut = new ArmoredOutputStream(publicOut); } keyRingGen.generatePublicKeyRing().encode(publicOut); publicOut.close(); }
From source file:genkeys.java
License:Open Source License
private static void exportSecretKey(OutputStream out, KeyPair key, int cipher, int s2kmode, int hashAlgorithm, boolean useSHA1, String pass, boolean armor) throws IOException, NoSuchProviderException, PGPException { if (armor) {/*from ww w .ja v a 2 s . c om*/ out = new ArmoredOutputStream(out); } SecureRandom rand = new SecureRandom(); byte[] iv = new byte[8]; rand.nextBytes(iv); S2K s2k = new S2K(hashAlgorithm, iv, 0x60); SecretKeyPacket packet = secretKeyPacket(key, cipher, useSHA1, s2k, pass); BCPGOutputStream bcOut; if (out instanceof BCPGOutputStream) { bcOut = (BCPGOutputStream) out; } else { bcOut = new BCPGOutputStream(out); } bcOut.writePacket(packet); if (armor) { out.close(); } }
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 {/* www. j a v a2s . co 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 a va2s. co 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.arcusx.simplepgp.PgpDataEncryptor.java
public void encryptAndSign(InputStream dataIn, InputStream recipientPublicKeyFileIn, String dataFileName, InputStream senderPrivateKeyFileIn, OutputStream dataOut, boolean isArmoredOutput) throws IOException, PGPException { PGPCompressedDataGenerator comData = null; try {/*from w ww . j a v a 2s . co m*/ OutputStream out = dataOut; PGPPublicKey recipientPublicKey = PgpKeyUtils.readPublicKey(recipientPublicKeyFileIn); if (isArmoredOutput) { out = new ArmoredOutputStream(out); } BcPGPDataEncryptorBuilder dataEncryptor = new BcPGPDataEncryptorBuilder(PGPEncryptedData.TRIPLE_DES); dataEncryptor.setWithIntegrityPacket(true); dataEncryptor.setSecureRandom(new SecureRandom()); PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(dataEncryptor); encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(recipientPublicKey)); OutputStream encryptedOut = encryptedDataGenerator.open(out, new byte[BUFFER_SIZE]); // Initialize compressed data generator PGPCompressedDataGenerator compressedDataGenerator = new PGPCompressedDataGenerator( PGPCompressedData.ZIP); OutputStream compressedOut = compressedDataGenerator.open(encryptedOut, new byte[BUFFER_SIZE]); // Initialize signature generator final PGPSecretKey senderSecretKey = PgpKeyUtils.findSecretKey(senderPrivateKeyFileIn); PGPPrivateKey privateKey = PgpKeyUtils.getPrivateKeyFrom(senderSecretKey); PGPContentSignerBuilder signerBuilder = new BcPGPContentSignerBuilder( senderSecretKey.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1); PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(signerBuilder); signatureGenerator.init(PGPSignature.BINARY_DOCUMENT, privateKey); PGPSignatureSubpacketGenerator signatureSubpacketGenerator = new PGPSignatureSubpacketGenerator(); signatureSubpacketGenerator.setSignerUserID(false, PgpKeyUtils.getUserIdFrom(senderSecretKey)); signatureGenerator.setHashedSubpackets(signatureSubpacketGenerator.generate()); signatureGenerator.generateOnePassVersion(false).encode(compressedOut); // Initialize literal data generator PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator(); OutputStream literalOut = literalDataGenerator.open(compressedOut, PGPLiteralData.BINARY, dataFileName, new Date(), new byte[BUFFER_SIZE]); byte[] buf = new byte[BUFFER_SIZE]; int len; while ((len = dataIn.read(buf)) > 0) { literalOut.write(buf, 0, len); signatureGenerator.update(buf, 0, len); } dataIn.close(); literalDataGenerator.close(); // generate the signature, compress, encrypt and write to the "out" stream signatureGenerator.generate().encode(compressedOut); compressedDataGenerator.close(); encryptedDataGenerator.close(); if (isArmoredOutput) { out.close(); } } finally { if (comData != null) { comData.close(); } IOUtils.closeQuietly(dataOut); } }
From source file:com.bekwam.resignator.util.CryptUtils.java
License:Apache License
private byte[] encrypt(byte[] clearData, char[] passPhrase) throws IOException, PGPException, NoSuchProviderException { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); ///* w ww . j av a 2 s . co m*/ // armor makes the encrypted output more readable (includes header, footer, printable chars) // OutputStream out = bOut; out = new ArmoredOutputStream(out); // // The standard jre installation limits keysize to 128. Use the unlimited jars to go higher. // PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator( new JcePGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.AES_128) .setSecureRandom(new SecureRandom()).setProvider("BC")); encGen.addMethod(new JcePBEKeyEncryptionMethodGenerator(passPhrase).setProvider("BC")); OutputStream encOut = encGen.open(out, clearData.length); encOut.write(clearData); encOut.close(); out.close(); return bOut.toByteArray(); }
From source file:com.fuzion.tools.pgp.BCPGPKeyGenTools.java
License:Open Source License
public static final void exportSecretKey(PGPKeyRingGenerator pgpKeyRingGen, File keyFile, boolean asciiArmor) throws IOException { PGPSecretKeyRing pgpSecKeyRing = pgpKeyRingGen.generateSecretKeyRing(); if (asciiArmor) { ArmoredOutputStream aos = new ArmoredOutputStream(new FileOutputStream(keyFile)); pgpSecKeyRing.encode(aos);/*from w w w. j ava2s . c om*/ aos.close(); } else { FileOutputStream fos = new FileOutputStream(keyFile); pgpSecKeyRing.encode(fos); fos.close(); } }
From source file:com.fuzion.tools.pgp.BCPGPKeyGenTools.java
License:Open Source License
public static final void exportPublicKey(PGPKeyRingGenerator pgpKeyRingGen, File keyFile, boolean asciiArmor) throws IOException { PGPPublicKeyRing pgpPubKeyRing = pgpKeyRingGen.generatePublicKeyRing(); if (asciiArmor) { ArmoredOutputStream aos = new ArmoredOutputStream(new FileOutputStream(keyFile)); pgpPubKeyRing.encode(aos);// www .j a va 2 s .c o m aos.close(); } else { FileOutputStream fos = new FileOutputStream(keyFile); pgpPubKeyRing.encode(fos); fos.close(); } }
From source file:com.geekcommune.identity.EncryptionUtil.java
License:Open Source License
/** * Encrypt and sign the specified input file. If you pass in a seed, you * will get the same encrypted output for the same file + same seed + same signor. * /*from w w w. jav a2 s. c om*/ * DANGER! If you use the same seed for multiple different messages, you are * making your key stream vulnerable to hacking, and your encryption is near * meaningless! Make sure to use different seeds for different contents! * * @param seed */ public void encryptAndSignFile(String outputFilename, File inFile, InputStream publicRing, InputStream secretRing, String recipient, String signor, char[] passwd, boolean armor, boolean withIntegrityCheck, boolean oldFormat, byte[] seed) throws PGPException { try { // Get the public keyring PGPPublicKeyRingCollection pubRing = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream(publicRing)); PGPSecretKeyRingCollection secRing = readSecretKeyRingCollection(secretRing); // Find the recipient's key PGPPublicKey encKey = readPublicKey(pubRing, recipient, true); if (encKey.isRevoked()) { String keyId = Long.toHexString(encKey.getKeyID()).substring(8); throw new PGPException("Encryption key (0x" + keyId + ") has been revoked"); } // Find the signing key PGPPublicKey publicKey; PGPSecretKey secretKey; if (signor != null) { publicKey = readPublicKey(pubRing, signor, false); secretKey = findSecretKey(secRing, publicKey.getKeyID(), true); } else { // Just look for the first signing key on the secret keyring (if any) secretKey = findSigningKey(secRing); publicKey = findPublicKey(pubRing, secretKey.getKeyID(), false); } if (publicKey.isRevoked()) { String keyId = Long.toHexString(publicKey.getKeyID()).substring(8); throw new PGPException("Signing key (0x" + keyId + ") has been revoked"); } PGPPrivateKey privateKey = secretKey.extractPrivateKey(passwd, "BC"); // Sign the data into an in-memory stream ByteArrayOutputStream bOut = new ByteArrayOutputStream(); if (oldFormat) { signDataV3(inFile, bOut, publicKey, privateKey); } else { signData(inFile, bOut, publicKey, privateKey); } SecureRandom secRand = makeSecureRandom(seed); PGPEncryptedDataGenerator cPk = oldFormat ? new PGPEncryptedDataGenerator(PGPEncryptedData.AES_256, secRand, oldFormat, "BC") : new PGPEncryptedDataGenerator(PGPEncryptedData.AES_256, withIntegrityCheck, secRand, "BC"); cPk.addMethod(encKey); byte[] bytes = bOut.toByteArray(); OutputStream out = new FileOutputStream(outputFilename); OutputStream aOut = armor ? new ArmoredOutputStream(out) : out; OutputStream cOut = cPk.open(aOut, bytes.length); cOut.write(bytes); cPk.close(); if (armor) { aOut.close(); } out.close(); } catch (PGPException e) { throw e; } catch (Exception e) { throw new PGPException("Error in encryption", e); } }
From source file:com.geekcommune.identity.EncryptionUtil.java
License:Open Source License
/** * Encrypt the specified input file//from w ww .ja v a 2 s.co m * @param seed */ public void encryptFile(OutputStream out, InputStream in, String inName, long inLength, Date inDate, PGPPublicKey encKey, boolean armor, boolean withIntegrityCheck, boolean oldFormat, char[] passphrase, byte[] seed) throws PGPException { try { if (encKey.isRevoked()) { String keyId = Long.toHexString(encKey.getKeyID()).substring(8); throw new PGPException("Encryption key (0x" + keyId + ") has been revoked"); } // Compress the data into an in-memory stream ByteArrayOutputStream bOut = new ByteArrayOutputStream(); compressData(in, bOut, inName, inLength, inDate, oldFormat, Format.UNCOMPRESSED); // Now encrypt the result SecureRandom secRand = makeSecureRandom(seed); // Now encrypt the result PGPEncryptedDataGenerator cPk = oldFormat ? new PGPEncryptedDataGenerator(PGPEncryptedData.AES_256, secRand, oldFormat, "BC") : new PGPEncryptedDataGenerator(PGPEncryptedData.AES_256, withIntegrityCheck, secRand, "BC"); cPk.addMethod(encKey); byte[] bytes = bOut.toByteArray(); OutputStream aOut = armor ? new ArmoredOutputStream(out) : out; OutputStream cOut = cPk.open(aOut, bytes.length); cOut.write(bytes); cPk.close(); if (armor) { aOut.close(); } out.close(); } catch (PGPException e) { throw e; } catch (Exception e) { throw new PGPException("Error in encryption", e); } }