Apache Commons logo Commons Crypto? logo

User guide

Apache Commons Crypto is a cryptographic library optimized with AES-NI (Advanced Encryption Standard New Instructions). It provides Java API for both cipher level and Java stream level. Developers can use it to implement high performance AES encryption/decryption with the minimum code and effort. Please note that Apache Commons Crypto doesn't implement the cryptographic algorithm such as AES directly. It wraps to Openssl or JCE which implement the algorithms.

Interfaces Overview

Interfaces and classes used by the various implementation in the sub-packages.

random The interface for SecureRandom.
cipher The interface of cryptographic cipher for encryption and decryption.
stream The interface wraps the underlying stream and it automatically encrypts the stream when data is written and decrypts the stream when data is read.

Usage

    Usage of Random API

    SecureRandom provides a cryptographically strong random number generators. The default implementation will use Intel® Digital Random Number Generator (DRNG) for accelerating the random generation.

    //Constructs a byte array to store random data.
    byte[] key = new byte[16];
    byte[] iv = new byte[16];
    Properties properties = new Properties();
    //Gets the 'SecureRandom' instance.
    SecureRandom secureRandom = SecureRandomFactory.getSecureRandom(properties);
    //Generates random bytes and places them into the byte array.
    secureRandom.nextBytes(key);
    secureRandom.nextBytes(iv);
    //Closes the SecureRandom.
    secureRandom.close();

    Usage of Cipher API

    Cipher provides an cryptographic interface for encryption and decryption. We provide two kind of implementations: JCE Cipher and Openssl Cipher. The JCE implementation uses JCE provider and the Openssl implementation uses Intel® AES New Instructions (Intel® AES NI).

    Usage of Byte Array Encryption/Decryption
    Properties properties = new Properties();
    //Creates a Cipher instance with the transformation and properties.
    Cipher cipher = Utils.getCipherInstance(CipherTransformation.AES_CTR_NOPADDING, properties);

    String input = "hello world!";
    int inputOffset = 0;
    int inputLen = input.length();
    byte[] output = new byte[1024];
    int outputOffset = 0;
    //Initializes the cipher with ENCRYPT_MODE,key and iv.
    cipher.init(Cipher.ENCRYPT_MODE, key, iv);
    //Continues a multiple-part encryption/decryption operation for byte array.
    cipher.update(input.getBytes("UTF-8"), inputOffset, inputLen, output, outputOffset);
    //We should call do final at the end of encryption/decryption.
    cipher.doFinal(inBuffer, outBuffer);
    //Closes the cipher.
    cipher.close();
    Usage of ByteBuffer Encryption/Decryption
    Properties properties = new Properties();
    //Creates a Cipher instance with the transformation and properties.
    Cipher cipher = Utils.getCipherInstance(CipherTransformation.AES_CTR_NOPADDING, properties);

    int bufferSize = 4096;
    ByteBuffer inBuffer = ByteBuffer.allocateDirect(bufferSize);
    ByteBuffer outBuffer = ByteBuffer.allocateDirect(bufferSize);
    inBuffer.put("The data you want to encrypt or decrypt".getBytes("UTF-8"));
    //Initializes the cipher with ENCRYPT_MODE,key and iv.
    cipher..init(Cipher.ENCRYPT_MODE, key, iv);
    //Continues a multiple-part encryption/decryption operation for byte array.
    cipher.update(inBuffer, outBuffer);
    //We should call do final at the end of encryption/decryption.
    cipher.doFinal(inBuffer, outBuffer);
    //Closes the cipher.
    cipher.close();

    Usage of Stream API

    Stream provides the data encryption and decryption in stream manner. We provide CipherInputStream,CTRCipherInputStream,PositionedCipherInputstream implementations for InputStream and CipherOutputStream,CTRCipherOutputStream implementations for OutputStream.

    Usage of stream encryption
    int bufferSize = 4096;
    String input = "hello world!";
    byte[] decryptedData = new byte[1024];
    //Encryption with CipherOutputStream.
    //Initializes the cipher with ENCRYPT_MODE,key and iv.
    cipher.init(Cipher.ENCRYPT_MODE, key, iv);
    // Constructs the original OutputStream.
    OutputStream outputStream = new ByteArrayOutputStream();
    //Constructs the instance of CipherOutputStream.
    CipherOutputStream cos = new CipherOutputStream(outputStream, cipher, bufferSize, key, iv);
    cos.write(input.getBytes("UTF-8"));
    cos.flush();
    cos.close();
    Usage of stream decryption
    // Decryption with CipherInputStream.
    //Initializes the cipher with DECRYPT_MODE,key and iv.
    cipher.init(Cipher.DECRYPT_MODE, key, iv);
    //Constructs the original InputStream.
    InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    //Constructs the instance of CipherInputStream.
    CipherInputStream cis = new CipherInputStream(inputStream, cipher, bufferSize, key, iv);
    int decryptedLen = cis.read(decryptedData, 0, 1024);
    cis.close();