Example usage for javax.crypto CipherOutputStream CipherOutputStream

List of usage examples for javax.crypto CipherOutputStream CipherOutputStream

Introduction

In this page you can find the example usage for javax.crypto CipherOutputStream CipherOutputStream.

Prototype

public CipherOutputStream(OutputStream os, Cipher c) 

Source Link

Document

Constructs a CipherOutputStream from an OutputStream and a Cipher.

Usage

From source file:org.zuinnote.hadoop.office.format.common.writer.msexcel.internal.EncryptedTempData.java

public OutputStream getOutputStream() throws FileNotFoundException {
    if (this.ciEncrypt != null) { // encrypted tempdata
        LOG.debug("Returning encrypted OutputStream for " + this.tempFile.getAbsolutePath());

        return new CipherOutputStream(new FileOutputStream(this.tempFile), this.ciEncrypt);
    }//from  w  ww.  j  a  va  2  s .c o m
    return new FileOutputStream(this.tempFile);
}

From source file:org.sakuli.services.cipher.AesCbcCipher.java

public static byte[] encryptBytes(SecureRandom rng, SecretKey aesKey, byte[] plaintext)
        throws SakuliCipherException {
    checkCipherParameters(aesKey, plaintext);
    final byte[] ciphertext;
    try {/*ww  w .java 2 s  .c  om*/

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();

        final Cipher aesCBC = Cipher.getInstance(CBC_ALGORITHM);
        final IvParameterSpec ivForCBC = createIV(aesCBC.getBlockSize(), Optional.of(rng));
        aesCBC.init(Cipher.ENCRYPT_MODE, aesKey, ivForCBC);

        baos.write(ivForCBC.getIV());

        try (final CipherOutputStream cos = new CipherOutputStream(baos, aesCBC)) {
            cos.write(plaintext);
        }

        ciphertext = baos.toByteArray();
        return ciphertext;
    } catch (Exception e) {
        throw new SakuliCipherException(e, "Error during encrypting secret!");
    }
}

From source file:org.pieShare.pieShareApp.service.fileService.fileEncryptionService.FileEncryptionService.java

@Override
public void encryptFile(File source, File target) {

    try {//from  w  ww .ja v  a2s  . c om
        FileInputStream stream = new FileInputStream(source);
        FileOutputStream fileStream = new FileOutputStream(target);
        CipherOutputStream outputStream = new CipherOutputStream(fileStream,
                this.getCipher(Cipher.ENCRYPT_MODE));
        //Base64OutputStream base64OutStream = new Base64OutputStream(fileStream);
        this.rewriteFile(stream, outputStream);
    } catch (FileNotFoundException ex) {
        PieLogger.error(this.getClass(), "Exception in FileEncrypterService!", ex);
    } catch (IOException ex) {
        PieLogger.error(this.getClass(), "Exception in FileEncrypterService!", ex);
    } catch (InvalidKeyException ex) {
        PieLogger.error(this.getClass(), "Exception in FileEncrypterService!", ex);
    }
}

From source file:jatoo.properties.FileProperties.java

public synchronized FileProperties save() throws IOException {

    ///*  www  .  jav  a2  s  . com*/
    // ensure the parent directories

    file.getAbsoluteFile().getParentFile().mkdirs();

    //
    // save

    OutputStream stream = null;

    try {

        stream = new FileOutputStream(file);

        if (cipherEncrypt != null) {
            stream = new CipherOutputStream(stream, cipherEncrypt);
        }

        store(stream, null);
    }

    catch (IOException e) {
        throw e;
    }

    finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
            }
        }
    }

    return this;
}

From source file:gobblin.crypto.EncodingBenchmark.java

@Benchmark
public byte[] write1KRecordsDirectCipherStream(EncodingBenchmarkState state) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, state.credStore.getKey());
    ByteArrayOutputStream sink = new ByteArrayOutputStream();
    OutputStream os = new CipherOutputStream(sink, cipher);
    os.write(state.OneKBytes);//from w  ww.ja  v a  2  s.c  om
    os.close();

    return sink.toByteArray();
}

From source file:CipherSocket.java

public OutputStream getOutputStream() throws IOException {
    OutputStream os = delegate == null ? super.getOutputStream() : delegate.getOutputStream();
    Cipher cipher = null;/*from  w  w  w  .  j a va2s  . c  o m*/
    try {
        cipher = Cipher.getInstance(algorithm);
        int size = cipher.getBlockSize();
        byte[] tmp = new byte[size];
        Arrays.fill(tmp, (byte) 15);
        IvParameterSpec iv = new IvParameterSpec(tmp);
        cipher.init(Cipher.ENCRYPT_MODE, key, iv);
    } catch (Exception e) {
        throw new IOException("Failed to init cipher: " + e.getMessage());
    }
    CipherOutputStream cos = new CipherOutputStream(os, cipher);
    return cos;
}

From source file:org.codice.ddf.configuration.migration.CipherUtils.java

/**
 * Wraps a given {@link OutputStream} in a {@link CipherOutputStream}
 *
 * @param outputStream/*from  ww w. j  av a 2 s  .  c  o  m*/
 * @return a {@link CipherOutputStream} for writing encrypted {@link java.util.zip.ZipEntry}
 *     contents
 */
public CipherOutputStream getCipherOutputStream(OutputStream outputStream) {
    return new CipherOutputStream(outputStream, cipher);
}

From source file:jfs.sync.encryption.JFSEncryptedStream.java

public static OutputStream createOutputStream(long compressionLimit, OutputStream baseOutputStream, long length,
        Cipher cipher) throws IOException {
    OutputStream result = null;//w w  w .java  2 s .  c o  m
    Runtime runtime = Runtime.getRuntime();
    long freeMem = runtime.totalMemory() / SPACE_RESERVE;
    if (length >= freeMem) {
        if (log.isWarnEnabled()) {
            log.warn("JFSEncryptedStream.createOutputStream() GC " + freeMem + "/" + runtime.maxMemory());
        } // if
        runtime.gc();
        freeMem = runtime.totalMemory() / SPACE_RESERVE;
    } // if
    if ((length < compressionLimit) && (length < freeMem)) {
        result = new JFSEncryptedStream(baseOutputStream, cipher);
    } else {
        if (length < freeMem) {
            if (log.isInfoEnabled()) {
                log.info("JFSEncryptedStream.createOutputStream() not compressing");
            } // if
        } else {
            if (log.isWarnEnabled()) {
                log.warn("JFSEncryptedStream.createOutputStream() due to memory constraints (" + length + "/"
                        + freeMem + ") not compressing");
            } // if
        } // if
        ObjectOutputStream oos = new ObjectOutputStream(baseOutputStream);
        oos.writeByte(COMPRESSION_NONE);
        oos.writeLong(length);
        oos.flush();
        result = baseOutputStream;
        if (cipher != null) {
            result = new CipherOutputStream(result, cipher);
        } // if
    } // if
    return result;
}

From source file:com.doplgangr.secrecy.filesystem.encryption.AES_ECB_Crypter.java

@Override
public CipherOutputStream getCipherOutputStream(File file, String outputFileName)
        throws SecrecyCipherStreamException, FileNotFoundException {
    Cipher c;/* w  w  w.  ja va2  s  .  com*/
    try {
        c = Cipher.getInstance(mode);
    } catch (NoSuchAlgorithmException e) {
        throw new SecrecyCipherStreamException("Encryption algorithm not found!");
    } catch (NoSuchPaddingException e) {
        throw new SecrecyCipherStreamException("Selected padding not found!");
    }

    try {
        c.init(Cipher.ENCRYPT_MODE, aesKey);
    } catch (InvalidKeyException e) {
        throw new SecrecyCipherStreamException("Invalid encryption key!");
    }

    String filename = Base64Coder.encodeString(FilenameUtils.removeExtension(file.getName())) + "."
            + FilenameUtils.getExtension(file.getName());
    File outputFile = new File(vaultPath + "/" + filename);

    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(outputFile),
            Config.BLOCK_SIZE);

    return new CipherOutputStream(bufferedOutputStream, c);
}

From source file:org.yes.cart.shoppingcart.support.impl.AbstractCryptedTuplizerImpl.java

/**
 * Converts cart object into a String tuple.
 *
 * @param serializable cart/*  w ww . j  a va 2s  . co  m*/
 *
 * @return string
 *
 * @throws CartTuplizationException when cannot convert to string tuple
 */
protected String toToken(final Serializable serializable) throws CartTuplizationException {

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    synchronized (desCipher) {
        Base64OutputStream base64EncoderStream = new Base64OutputStream(byteArrayOutputStream, true,
                Integer.MAX_VALUE, null); //will be split manually
        CipherOutputStream cipherOutputStream = new CipherOutputStream(base64EncoderStream, desCipher);
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(cipherOutputStream);
            objectOutputStream.writeObject(serializable);
            objectOutputStream.flush();
            objectOutputStream.close();
        } catch (Throwable ioe) {
            LOG.error(MessageFormat.format("Unable to serialize object {0}", serializable), ioe);
            throw new CartTuplizationException(ioe);
        } finally {
            try {
                if (objectOutputStream != null) {
                    objectOutputStream.close();
                }
                cipherOutputStream.close();
                base64EncoderStream.close();
                byteArrayOutputStream.close();
            } catch (IOException e) {
                LOG.error("Can not close stream", e);
            }
        }
    }

    return byteArrayOutputStream.toString();

}