Example usage for org.bouncycastle.crypto.io CipherOutputStream close

List of usage examples for org.bouncycastle.crypto.io CipherOutputStream close

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.io CipherOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with this stream.

Usage

From source file:edu.wisc.doit.tcrypt.BouncyCastleFileDecrypter.java

License:Apache License

@Override
public void decrypt(InputStream inputStream, OutputStream outputStream)
        throws InvalidCipherTextException, IOException, DecoderException {
    final TarArchiveInputStream tarInputStream = new TarArchiveInputStream(inputStream, FileEncrypter.ENCODING);

    final BufferedBlockCipher cipher = createCipher(tarInputStream);

    //Advance to the next entry in the tar file
    tarInputStream.getNextTarEntry();//  w  w  w .j a  v  a 2s .  c om

    //Create digest output stream used to generate digest while decrypting
    final DigestOutputStream digestOutputStream = new DigestOutputStream(this.createDigester());

    //Do a streaming decryption of the file output
    final CipherOutputStream cipherOutputStream = new CipherOutputStream(
            new TeeOutputStream(outputStream, digestOutputStream), cipher);
    IOUtils.copy(tarInputStream, cipherOutputStream);
    cipherOutputStream.close();

    //Capture the hash of the decrypted output
    final byte[] hashBytes = digestOutputStream.getDigest();
    verifyOutputHash(tarInputStream, hashBytes);
}

From source file:edu.wisc.doit.tcrypt.BouncyCastleFileEncrypter.java

License:Apache License

@Override
public void encrypt(String fileName, int size, InputStream inputStream, OutputStream outputStream)
        throws InvalidCipherTextException, IOException {
    final TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(outputStream, 512,
            ENCODING);/*from  w w w .  j  av  a2s  . co  m*/

    final BufferedBlockCipher cipher = createCipher(tarArchiveOutputStream);

    startEncryptedFile(fileName, size, tarArchiveOutputStream, cipher);

    //Setup cipher output stream, has to protect from close as the cipher stream must close but the tar cannot get closed yet
    final CipherOutputStream cipherOutputStream = new CipherOutputStream(
            new CloseShieldOutputStream(tarArchiveOutputStream), cipher);

    //Setup digester
    final DigestOutputStream digestOutputStream = new DigestOutputStream(this.createDigester());

    //Perform streaming encryption and hashing of the file
    IOUtils.copy(inputStream, new TeeOutputStream(cipherOutputStream, digestOutputStream));
    cipherOutputStream.close();

    tarArchiveOutputStream.closeArchiveEntry();

    //Capture the hash code of the encrypted file
    digestOutputStream.close();
    final byte[] hashBytes = digestOutputStream.getDigest();

    this.writeHashfile(tarArchiveOutputStream, hashBytes);

    //Close the TAR stream, nothing else should be written to it
    tarArchiveOutputStream.close();
}

From source file:net.nharyes.secrete.curve.Curve25519PrivateKey.java

License:Open Source License

public void serialize(OutputStream out, char[] password) throws IOException {

    try {//from  w  ww  .  j a  v a2 s  .  c  o  m

        // generate initial vector
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        byte[] iv = new byte[16];
        random.nextBytes(iv);

        // generate salt
        byte[] salt = new byte[64];
        random.nextBytes(salt);

        // initialize cipher
        CipherParameters params = new ParametersWithIV(new KeyParameter(deriveKey(password, salt)), iv);
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
                new PKCS7Padding());
        cipher.reset();
        cipher.init(true, params);

        // write magic number
        out.write(MagicNumbers.PRIVATE_KEY);
        out.flush();

        // write initial vector and salt
        out.write(iv);
        out.write(salt);
        out.flush();

        // write encrypted key to output stream
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        CipherOutputStream cout = new CipherOutputStream(buf, cipher);
        cout.write(key);
        cout.close();
        out.write(buf.toByteArray());
        out.flush();

    } catch (UnsupportedEncodingException | NoSuchAlgorithmException ex) {

        throw new UnsupportedOperationException(ex.getMessage(), ex);
    }
}