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 OpenSSL or JCE which implement the algorithms. OpenSSL 1.1.1 is required for building and running.

Interfaces Overview

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

random The interface for CryptoRandom.
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

Prerequisites

Commons Crypto relies on standard JDK 1.8 (or above) and OpenSSL 1.1.1 for production deployment. If it is installed, the command openssl version can be used to show the version.

OpenSSL may already be installed on your system; if not, please visit OpenSSL.org for information on installation.

Using Commons Crypto in your Apache Maven build

To build with Apache Maven, add the dependencies listed below to your pom.xml file.

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-crypto</artifactId>
    <version>1.1.0</version>
</dependency>

Usage of Random API

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

RandomExample.java

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
CipherByteArrayExample.java
Usage of ByteBuffer Encryption/Decryption
CipherByteBufferExample.java

Usage of Stream API

Stream provides the data encryption and decryption in stream manner. We provide CryptoInputStream, CTRCryptoInputStream, PositionedCryptoInputStream implementations for InputStream and CryptoOutputStream, CTRCryptoOutputStream implementations for OutputStream.

Usage of stream encryption/decryption
StreamExample.java