Example usage for org.apache.commons.crypto.utils Utils getCipherInstance

List of usage examples for org.apache.commons.crypto.utils Utils getCipherInstance

Introduction

In this page you can find the example usage for org.apache.commons.crypto.utils Utils getCipherInstance.

Prototype

public static CryptoCipher getCipherInstance(String transformation, Properties props) throws IOException 

Source Link

Document

Helper method to create a CryptoCipher instance and throws only IOException.

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. j  a  va 2  s . com*/

    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.commons.crypto.examples.CipherByteBufferExample.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();
    //Creates a CryptoCipher instance with the transformation and properties.
    final String transform = "AES/CBC/PKCS5Padding";
    final ByteBuffer outBuffer;
    final int bufferSize = 1024;
    final int updateBytes;
    final int finalBytes;
    try (CryptoCipher encipher = Utils.getCipherInstance(transform, properties)) {

        ByteBuffer inBuffer = ByteBuffer.allocateDirect(bufferSize);
        outBuffer = ByteBuffer.allocateDirect(bufferSize);
        inBuffer.put(getUTF8Bytes("hello world!"));

        inBuffer.flip(); // ready for the cipher to read it
        // Show the data is there
        System.out.println("inBuffer=" + asString(inBuffer));

        // 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 buffer.
        updateBytes = encipher.update(inBuffer, outBuffer);
        System.out.println(updateBytes);

        // We should call do final at the end of encryption/decryption.
        finalBytes = encipher.doFinal(inBuffer, outBuffer);
        System.out.println(finalBytes);
    }/*w  w  w  . ja  v  a 2  s. co m*/

    outBuffer.flip(); // ready for use as decrypt
    byte[] encoded = new byte[updateBytes + finalBytes];
    outBuffer.duplicate().get(encoded);
    System.out.println(Arrays.toString(encoded));

    // Now reverse the process
    try (CryptoCipher decipher = Utils.getCipherInstance(transform, properties)) {
        decipher.init(Cipher.DECRYPT_MODE, key, iv);
        ByteBuffer decoded = ByteBuffer.allocateDirect(bufferSize);
        decipher.update(outBuffer, decoded);
        decipher.doFinal(outBuffer, decoded);
        decoded.flip(); // ready for use
        System.out.println("decoded=" + asString(decoded));
    }
}

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

public CryptoAES(String transformation, Properties properties, byte[] inKey, byte[] outKey, byte[] inIv,
        byte[] outIv) throws IOException {
    checkTransformation(transformation);
    // encryptor/*from   w  w w.  ja v  a  2s .  co  m*/
    encryptor = Utils.getCipherInstance(transformation, properties);
    try {
        SecretKeySpec outKEYSpec = new SecretKeySpec(outKey, "AES");
        IvParameterSpec outIVSpec = new IvParameterSpec(outIv);
        encryptor.init(Cipher.ENCRYPT_MODE, outKEYSpec, outIVSpec);
    } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
        throw new IOException("Failed to initialize encryptor", e);
    }

    // decryptor
    decryptor = Utils.getCipherInstance(transformation, properties);
    try {
        SecretKeySpec inKEYSpec = new SecretKeySpec(inKey, "AES");
        IvParameterSpec inIVSpec = new IvParameterSpec(inIv);
        decryptor.init(Cipher.DECRYPT_MODE, inKEYSpec, inIVSpec);
    } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
        throw new IOException("Failed to initialize decryptor", e);
    }

    integrity = new Integrity(outKey, inKey);
}