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.
cipher
|
The interface of cryptographic cipher for encryption and decryption.
|
random
|
The interface for SecureRandom.
|
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
//Constructs a byte array to store random data.
byte[] data = new byte[16];
Properties properties = new Properties();
// Default value is com.intel.chimera.random.JavaSecureRandom.
properties.setProperty("chimera.crypto.secure.random.classes", "com.intel.chimera.random.OpensslSecureRandom,com.intel.chimera.random.OsSecureRandom");
//Gets the 'SecureRandom' instance.
SecureRandom secureRandom = SecureRandomFactory.getSecureRandom(properties);
//Generates random bytes and places them into the byte array.
secureRandom.nextBytes(data);
//Closes the SecureRandom.
secureRandom.close();
|
Usage of Cipher API
Properties properties = new Properties();
// Default value is com.intel.chimera.crypto.JceCipher.
properties.setProperty("chimera.crypto.cipher.classes", "com.intel.chimera.crypto.OpensslCipher,com.intel.chimera.crypto.JceCipher");
Cipher cipher = Utils.getCipherInstance(CipherTransformation.AES_CTR_NOPADDING, properties);
properties = new Properties();
// Default value is com.intel.chimera.random.JavaSecureRandom.
properties.setProperty("chimera.crypto.secure.random.classes", "com.intel.chimera.random.OpensslSecureRandom,com.intel.chimera.random.OsSecureRandom");
//Gets the 'SecureRandom' instance.
SecureRandom secureRandom = SecureRandomFactory.getSecureRandom(properties);
// Available key length is: 16, 24, 32 bytes.
//Constructs the crypto key for cipher.
byte[] key = new byte[16];
//Generates random bytes and places them into the byte array.
secureRandom.nextBytes(key);
// IV length is always 16 bytes.
//Constructs the crypto iv for cipher.
byte[] iv = new byte[16];
//Generates random bytes and places them into the byte array.
secureRandom.nextBytes(iv);
//Closes the SecureRandom.
secureRandom.close();
|
Usage of Byte Array Encryption/Decryption
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
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
Properties properties = new Properties();
// Default value is com.intel.chimera.random.JavaSecureRandom.
properties.setProperty("chimera.crypto.secure.random.classes", "com.intel.chimera.random.OpensslSecureRandom,com.intel.chimera.random.OsSecureRandom");
//Gets the 'SecureRandom' instance.
SecureRandom secureRandom = SecureRandomFactory.getSecureRandom(properties);
// Available key length is: 16, 24, 32 bytes.
//Constructs the crypto key for cipher.
byte[] key = new byte[16];
//Generates random bytes and places them into the byte array.
secureRandom.nextBytes(key);
// IV length is always 16 bytes
//Constructs the crypto iv for cipher.
byte[] iv = new byte[16];
//Generates random bytes and places them into the byte array.
secureRandom.nextBytes(iv);
//Closes the SecureRandom.
secureRandom.close();
properties = new Properties();
// Default value is com.intel.chimera.crypto.JceCipher.
properties.setProperty("chimera.crypto.cipher.classes", "com.intel.chimera.crypto.OpensslCipher,com.intel.chimera.crypto.JceCipher");
Cipher cipher = Utils.getCipherInstance(CipherTransformation.AES_CTR_NOPADDING, properties);
int bufferSize = 4096;
String input = "hello world!";
byte[] decryptedData = new byte[1024];
//Encryption with CryptoOutputStream.
//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 CryptoOutputStream.
CryptoOutputStream cos = new CryptoOutputStream(outputStream, cipher, bufferSize, key, iv);
cos.write(input.getBytes("UTF-8"));
cos.flush();
cos.close();
// Decryption with CryptoInputStream.
//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 CryptoInputStream.
CryptoInputStream cis = new CryptoInputStream(inputStream, cipher, bufferSize, key, iv);
int decryptedLen = cis.read(decryptedData, 0, 1024);
cis.close();
|
|