public final class KeyGenerator

  1. Object
  2. KeyGenerator

Generates fresh cryptographic key material. AES keys are pulled directly from SecureRandom; RSA key pairs are generated by the platform’s native crypto provider.

Example

SecretKey aes = KeyGenerator.aes(256);   // AES-256
KeyPair   rsa = KeyGenerator.rsa(2048);  // RSA-2048

Methods

public static SecretKey aes(int bits)Generates a fresh AES key of the given bit length.
public static SecretKey hmac(int bits)Generates a fresh HMAC key of the given bit length.
public static KeyPair rsa(int bits)Generates a fresh RSA key pair of the given size in bits.

Inherited methods

Method details

aes

public static SecretKey aes(int bits)
Generates a fresh AES key of the given bit length. Valid lengths are 128, 192 and 256 bits.

hmac

public static SecretKey hmac(int bits)
Generates a fresh HMAC key of the given bit length. By convention, HMAC keys should be at least as long as the hash output.

rsa

public static KeyPair rsa(int bits)
Generates a fresh RSA key pair of the given size in bits. 2048 is the modern minimum; 3072 / 4096 for higher security margins. Generating a 4096-bit RSA key can take several seconds – call from a background thread.