Class AESCounterRNG
- All Implemented Interfaces:
Serializable
,RandomGenerator
,RepeatableRNG
Non-linear random number generator based on the AES block cipher in counter mode. Uses the seed as a key to encrypt a 128-bit counter using AES(Rijndael).
By default, we only use a 128-bit key for the cipher because any larger key requires
the inconvenience of installing the unlimited strength cryptography policy
files for the Java platform. Larger keys may be used (192 or 256 bits) but if the
cryptography policy files are not installed, a
GeneralSecurityException
will be thrown.
NOTE: Because instances of this class require 128-bit seeds, it is not
possible to seed this RNG using the Random.setSeed(long)
method inherited
from Random
. Calls to this method will have no effect.
Instead the seed must be set by a constructor.
NOTE: THIS CLASS IS NOT SERIALIZABLE
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
Trivial key implementation for use with AES cipher.Nested classes/interfaces inherited from interface java.util.random.RandomGenerator
RandomGenerator.ArbitrarilyJumpableGenerator, RandomGenerator.JumpableGenerator, RandomGenerator.LeapableGenerator, RandomGenerator.SplittableGenerator, RandomGenerator.StreamableGenerator
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Cipher
private final byte[]
private byte[]
private static final int
private int
private final ReentrantLock
private final byte[]
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new RNG and seeds it using 128 bits from the default seeding strategy.AESCounterRNG
(byte[] seed) Creates an RNG and seeds it with the specified seed data.AESCounterRNG
(int seedSizeBytes) Seed the RNG using the default seed generation strategy to create a seed of the specified size.AESCounterRNG
(SeedGenerator seedGenerator) Seed the RNG using the provided seed generation strategy to create a 128-bit seed. -
Method Summary
Methods inherited from class java.util.Random
doubles, doubles, doubles, doubles, from, ints, ints, ints, ints, longs, longs, longs, longs, nextBoolean, nextBytes, nextDouble, nextFloat, nextGaussian, nextInt, nextInt, nextLong, setSeed
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.random.RandomGenerator
isDeprecated, nextDouble, nextDouble, nextExponential, nextFloat, nextFloat, nextGaussian, nextInt, nextLong, nextLong
-
Field Details
-
DEFAULT_SEED_SIZE_BYTES
private static final int DEFAULT_SEED_SIZE_BYTES- See Also:
-
seed
private final byte[] seed -
cipher
-
counter
private final byte[] counter -
lock
-
currentBlock
private byte[] currentBlock -
index
private int index
-
-
Constructor Details
-
AESCounterRNG
Creates a new RNG and seeds it using 128 bits from the default seeding strategy.- Throws:
GeneralSecurityException
- If there is a problem initialising the AES cipher.
-
AESCounterRNG
Seed the RNG using the provided seed generation strategy to create a 128-bit seed.- Parameters:
seedGenerator
- The seed generation strategy that will provide the seed value for this RNG.- Throws:
SeedException
- If there is a problem generating a seed.GeneralSecurityException
- If there is a problem initialising the AES cipher.
-
AESCounterRNG
Seed the RNG using the default seed generation strategy to create a seed of the specified size.- Parameters:
seedSizeBytes
- The number of bytes to use for seed data. Valid values are 16 (128 bits), 24 (192 bits) and 32 (256 bits). Any other values will result in an exception from the AES implementation.- Throws:
GeneralSecurityException
- If there is a problem initialising the AES cipher.- Since:
- 1.0.2
-
AESCounterRNG
Creates an RNG and seeds it with the specified seed data.- Parameters:
seed
- The seed data used to initialise the RNG.- Throws:
GeneralSecurityException
- If there is a problem initialising the AES cipher.
-
-
Method Details
-
getSeed
public byte[] getSeed()- Specified by:
getSeed
in interfaceRepeatableRNG
- Returns:
- The seed data used to initialise this pseudo-random number generator.
-
incrementCounter
private void incrementCounter() -
nextBlock
Generates a single 128-bit block (16 bytes).- Returns:
- A 16-byte block of random data.
- Throws:
GeneralSecurityException
- If there is a problem with the cipher that generates the random data.
-
next
protected final int next(int bits)
-