Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

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
KeyPair   ec  = KeyGenerator.ec(256);    // P-256, for ECDSA and ECDH

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.
public static KeyPair ec(int bits)Generates a fresh elliptic-curve key pair on NIST P-256 (secp256r1), the curve every platform provider implements.

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.

ec

public static KeyPair ec(int bits)
Generates a fresh elliptic-curve key pair on NIST P-256 (secp256r1), the curve every platform provider implements. The pair signs with Signature.SHA256_WITH_ECDSA and agrees secrets with KeyAgreement.ecdh(PrivateKey, PublicKey).

Parameters

bits int
the curve size; only 256 is supported