Java Generate Random Bytes using Apache Commons Crypto

Tags: Apache Commons Crypto

In this Java tutorial we learn how to generate a random byte[] array in Java application using Apache Commons Crypto library.

Table of contents

  1. Add Apache Commons Crypto dependency to Java project
  2. How to generate random byte[] array in Java
  3. Implement RandomBytesGenerator class for re-use in Java project

Add Apache Commons Crypto dependency to Java project

To use the Apache Commons Crypto library in the Gradle build project, add the following Apache Commons Crypto dependency into the build.gradle file.

implementation group: 'org.apache.commons', name: 'commons-crypto', version: '1.1.0'

To use the Apache Commons Crypto library in the Maven build project, add the following Apache Commons Crypto dependency into the pom.xml file.

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

To have more information about the Apache Commons Crypto Java library you can visit the project home page at commons.apache.org/proper/commons-crypto

How to generate random byte[] array in Java

To generate a random byte[] array we can use CryptoRandom class which can be created by CryptoRandomFactory.getCryptoRandom() method.

The Apache Commons Crypto library provide option to create CryptoRandom with different provider, such as OpenSSL as below.

Properties properties = new Properties();
properties.put(CryptoRandomFactory.CLASSES_KEY, CryptoRandomFactory.RandomProvider.OPENSSL.getClassName());
try(CryptoRandom cryptoRandom = CryptoRandomFactory.getCryptoRandom(properties)) {
}

or JVM implementation as following code

Properties properties = new Properties();
properties.put(CryptoRandomFactory.CLASSES_KEY, CryptoRandomFactory.RandomProvider.JAVA.getClassName());
try(CryptoRandom cryptoRandom = CryptoRandomFactory.getCryptoRandom(properties)) {
}

The following Java program to show you in detail how to generate random byte[] array.

GenerateRandomExample1.java

import org.apache.commons.crypto.random.CryptoRandom;
import org.apache.commons.crypto.random.CryptoRandomFactory;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.Properties;

public class GenerateRandomExample1 {
    public static void main(String... args) throws GeneralSecurityException, IOException {
        int length = 32;
        byte[] randomBytes = new byte[length];

        Properties properties = new Properties();
        properties.put(CryptoRandomFactory.CLASSES_KEY, CryptoRandomFactory.RandomProvider.OPENSSL.getClassName());

        try(CryptoRandom cryptoRandom = CryptoRandomFactory.getCryptoRandom(properties)) {
            cryptoRandom.nextBytes(randomBytes);
        }

        System.out.println("Random bytes:");
        System.out.println(Arrays.toString(randomBytes));
    }
}
The output as below.
Random bytes:
[-46, 98, 37, 66, 30, 59, 49, -126, 112, 115, -65, -60, 100, 99, -58, -102, -114, 23, -46, 81, 12, -4, 117, -118, 84, 0, -109, -88, -14, -51, 53, -87]

Implement RandomBytesGenerator class for re-use in Java project

From the previous step we can create a reusable class named RandomBytesGenerator to generate random byte[] array in Java project.

RandomBytesGenerator.java

import org.apache.commons.crypto.random.CryptoRandom;
import org.apache.commons.crypto.random.CryptoRandomFactory;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Properties;

public class RandomBytesGenerator {

    public byte[] generateRandomBytes(int length) throws GeneralSecurityException, IOException {
        byte[] randomBytes = new byte[length];

        Properties properties = new Properties();
        properties.put(CryptoRandomFactory.CLASSES_KEY, CryptoRandomFactory.RandomProvider.OPENSSL.getClassName());

        try(CryptoRandom cryptoRandom = CryptoRandomFactory.getCryptoRandom(properties)) {
            cryptoRandom.nextBytes(randomBytes);
        }

        return randomBytes;
    }

}

The following Java example code to show you how to use the above RandomBytesGenerator class.

GenerateRandomExample2.java

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;

public class GenerateRandomExample2 {
    public static void main(String... args) throws GeneralSecurityException, IOException {
        RandomBytesGenerator randomBytesGenerator = new RandomBytesGenerator();
        byte[] randomBytes = randomBytesGenerator.generateRandomBytes(16);

        System.out.println("Random bytes:");
        System.out.println(Arrays.toString(randomBytes));
    }
}
The output as below.
Random bytes:
[120, 32, 13, 73, 124, 26, 11, -86, 14, -105, -122, 98, -75, -93, 75, 27]

Happy Coding 😊