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

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

Introduction

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

Prototype

protected CryptoInputStream(Input input, CryptoCipher cipher, int bufferSize, Key key,
        AlgorithmParameterSpec params) throws IOException 

Source Link

Document

Constructs a CryptoInputStream .

Usage

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

public InputStream getCryptIn(final InputStream in, final KeyIv keyIv) throws IOException {
    return new CryptoInputStream(TRANSFORM, new Properties(), in, 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   w w w .j  a  v  a2  s.c  o m
        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.CommonsCryptoAESDecryptor.java

@Override
public InputStream createDecryptionStream(InputStream in) {
    try {/* www  . j a  v a 2 s.com*/
        return new CryptoInputStream(cipherMode, properties, in, key, new IvParameterSpec(iv));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

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

private CryptoInputStream createInputStream(ReadableByteChannel ch) throws IOException {
    return new CryptoInputStream(cipher, conf, ch, key, new IvParameterSpec(inIv));
}

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

/**
 * Create AES crypto input stream//from   w  w w. j  a v  a 2s .  co m
 * @param ch The underlying channel used to read data.
 * @return Return input crypto stream for decryption.
 * @throws IOException
 */
private CryptoInputStream createInputStream(ReadableByteChannel ch) throws IOException {
    return new CryptoInputStream(TRANSFORM, properties, ch, inKeySpec, inIvSpec);
}