Using key generators - ivan-zaera/cipher GitHub Wiki

A KeyGenerator is an algorithm capable of generating a public/private key pair for an AsymmetricBlockCipher or a Signer.

To use a key generator you can use the following code:

    var rsapars = new RSAKeyGeneratorParameters(new BigInteger("65537"), 2048, 12);
    var params = new ParametersWithRandom(rsapars, new SecureRandom());

    var keyGenerator = new KeyGenerator("RSA")
      ..init( params )
    ;

    var keyPair = keyGenerator.generateKeyPair();
    
    outputKeyPair( keyPair );

In general, you construct the KeyGenerator with the factory constructor which receives the standard algorithm name and then init() the cipher with its corresponding CipherParameters. See Algorithm nomenclature for documentation on standard algorithm names.

The type of CipherParameters to be used depends on the algorithm. For the example (RSA) it consists of the public exponent, the key bit strength, and the certainty used when copmuting primes but, for other algorithms, it can specify different data. See the Table of provided algorithms to get information on which CipherParameters to use for the algorithm of your choice.

To generate a key pair you make a call to generateKeyPair() and a valid random key pair is returned. The structure of the key pair depends on the algorithm being used, so you need to cast the keys to their proper types. See the Table of provided algorithms to get information on key structures.