# Class KeyGenerator

java.lang.Object
com.codename1.security.KeyGenerator

public final class KeyGenerator extends java.lang.Object

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

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static SecretKey`
  `aes(int bits)`
  Generates a fresh AES key of the given bit length.
  `static SecretKey`
  `hmac(int bits)`
  Generates a fresh HMAC key of the given bit length.
  `static KeyPair`
  `rsa(int bits)`
  Generates a fresh RSA key pair of the given size in bits.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## 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.