Example usage for org.apache.commons.crypto.cipher CryptoCipher update

List of usage examples for org.apache.commons.crypto.cipher CryptoCipher update

Introduction

In this page you can find the example usage for org.apache.commons.crypto.cipher CryptoCipher update.

Prototype

int update(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)
        throws ShortBufferException;

Source Link

Document

Continues a multiple-part encryption/decryption operation.

Usage

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

public static void main(String[] args) throws Exception {

    final SecretKeySpec key = new SecretKeySpec(getUTF8Bytes("1234567890123456"), "AES");
    final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456"));

    Properties properties = new Properties();
    properties.setProperty(CryptoCipherFactory.CLASSES_KEY, CipherProvider.OPENSSL.getClassName());
    //Creates a CryptoCipher instance with the transformation and properties.
    final String transform = "AES/CBC/PKCS5Padding";
    CryptoCipher encipher = Utils.getCipherInstance(transform, properties);
    System.out.println("Cipher:  " + encipher.getClass().getCanonicalName());

    final String sampleInput = "hello world!";
    System.out.println("input:  " + sampleInput);

    byte[] input = getUTF8Bytes(sampleInput);
    byte[] output = new byte[32];

    //Initializes the cipher with ENCRYPT_MODE, key and iv.
    encipher.init(Cipher.ENCRYPT_MODE, key, iv);
    //Continues a multiple-part encryption/decryption operation for byte array.
    int updateBytes = encipher.update(input, 0, input.length, output, 0);
    System.out.println(updateBytes);
    //We must call doFinal at the end of encryption/decryption.
    int finalBytes = encipher.doFinal(input, 0, 0, output, updateBytes);
    System.out.println(finalBytes);
    //Closes the cipher.
    encipher.close();//from w  w  w .ja  v a  2 s .c  o  m

    System.out.println(Arrays.toString(Arrays.copyOf(output, updateBytes + finalBytes)));

    // Now reverse the process using a different implementation with the same settings
    properties.setProperty(CryptoCipherFactory.CLASSES_KEY, CipherProvider.JCE.getClassName());
    CryptoCipher decipher = Utils.getCipherInstance(transform, properties);
    System.out.println("Cipher:  " + encipher.getClass().getCanonicalName());

    decipher.init(Cipher.DECRYPT_MODE, key, iv);
    byte[] decoded = new byte[32];
    decipher.doFinal(output, 0, updateBytes + finalBytes, decoded, 0);

    System.out.println("output: " + new String(decoded, StandardCharsets.UTF_8));
}

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

private byte[] doCipherOp(CryptoCipher cipher, byte[] in, boolean isFinal) throws GeneralSecurityException {

    Preconditions.checkState(cipher != null);

    int scale = 1;
    while (true) {
        int size = in.length * scale;
        byte[] buffer = new byte[size];
        try {//  ww w. j  a  v  a 2  s.  com
            int outSize = isFinal ? cipher.doFinal(in, 0, in.length, buffer, 0)
                    : cipher.update(in, 0, in.length, buffer, 0);
            if (outSize != buffer.length) {
                byte[] output = new byte[outSize];
                System.arraycopy(buffer, 0, output, 0, output.length);
                return output;
            } else {
                return buffer;
            }
        } catch (ShortBufferException e) {
            // Try again with a bigger buffer.
            scale *= 2;
        }
    }
}