Creates a encryption stream. - Java Security

Java examples for Security:Stream

Description

Creates a encryption stream.

Demo Code


//package com.java2s;

import java.io.OutputStream;
import java.security.GeneralSecurityException;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;

import javax.crypto.Cipher;

import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;

public class Main {
    /**// w ww.  j ava 2 s .c o  m
     * Cipher algorithm to use. "AES"
     */
    private static final String CIPHER_ALGORITHM = "AES";

    /**
     * Creates a encryption stream. It is a compressed then encrypted stream.
     *
     * @param outputStream
     *            source stream
     * @param secret
     *            secret for the cipher
     * @return the stream
     * @throws GeneralSecurityException
     */
    public static OutputStream buildEncryptStream(
            final OutputStream outputStream, final SecretKey secret)
            throws GeneralSecurityException {

        final Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secret);
        return new DeflaterOutputStream(new CipherOutputStream(
                outputStream, cipher), new Deflater(9, false));
    }
}

Related Tutorials