Example usage for org.apache.commons.crypto.stream CryptoOutputStream CryptoOutputStream

List of usage examples for org.apache.commons.crypto.stream CryptoOutputStream CryptoOutputStream

Introduction

In this page you can find the example usage for org.apache.commons.crypto.stream CryptoOutputStream CryptoOutputStream.

Prototype

protected CryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, Key key,
        AlgorithmParameterSpec params) throws IOException 

Source Link

Document

Constructs a CryptoOutputStream .

Usage

From source file:de.siegmar.securetransfer.component.Cryptor.java

public OutputStream getCryptOut(final OutputStream out, final KeyIv keyIv) throws IOException {
    return new CryptoOutputStream(TRANSFORM, new Properties(), out, new SecretKeySpec(keyIv.getKey(), "AES"),
            new IvParameterSpec(keyIv.getIv()));
}

From source file:org.apache.commons.crypto.examples.StreamExample.java

public static void main(String[] args) throws IOException {
    final SecretKeySpec key = new SecretKeySpec(getUTF8Bytes("1234567890123456"), "AES");
    final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456"));
    Properties properties = new Properties();
    final String transform = "AES/CBC/PKCS5Padding";

    String input = "hello world!";
    //Encryption with CryptoOutputStream.

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    try (CryptoOutputStream cos = new CryptoOutputStream(transform, properties, outputStream, key, iv)) {
        cos.write(getUTF8Bytes(input));/*from   ww w  .java 2  s. c  om*/
        cos.flush();
    }

    // The encrypted data:
    System.out.println("Encrypted: " + Arrays.toString(outputStream.toByteArray()));

    // Decryption with CryptoInputStream.
    InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());

    try (CryptoInputStream cis = new CryptoInputStream(transform, properties, inputStream, key, iv)) {
        byte[] decryptedData = new byte[1024];
        int decryptedLen = 0;
        int i;
        while ((i = cis.read(decryptedData, decryptedLen, decryptedData.length - decryptedLen)) > -1) {
            decryptedLen += i;
        }
        System.out.println("Decrypted: " + new String(decryptedData, 0, decryptedLen, StandardCharsets.UTF_8));
    }
}

From source file:org.apache.hadoop.hbase.io.crypto.aes.CommonsCryptoAESEncryptor.java

@Override
public OutputStream createEncryptionStream(OutputStream out) {
    if (!initialized) {
        reset();//from   ww  w . j  a va2s . c  o m
    }
    try {
        return new CryptoOutputStream(cipherMode, properties, out, key, new IvParameterSpec(iv));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.spark.network.crypto.TransportCipher.java

private CryptoOutputStream createOutputStream(WritableByteChannel ch) throws IOException {
    return new CryptoOutputStream(cipher, conf, ch, key, new IvParameterSpec(outIv));
}

From source file:org.apache.spark.network.sasl.aes.AesCipher.java

/**
 * Create AES crypto output stream/*w  w  w  .  j  av  a 2s  .c  om*/
 * @param ch The underlying channel to write out.
 * @return Return output crypto stream for encryption.
 * @throws IOException
 */
private CryptoOutputStream createOutputStream(WritableByteChannel ch) throws IOException {
    return new CryptoOutputStream(TRANSFORM, properties, ch, outKeySpec, outIvSpec);
}