Key Factories

There is another way to generate keys, and that is through the use of key factories. Key factories are most often used to import and export keys; the factory translates between an external format of the key that is easily transportable and the internal implementation of the key. There are certain types of keys, however, that can only be obtained via a key factory since there is no key generator to produce them (e.g., a password-based encryption key).

There are two external representations by which a key may be transmitted -- by its encoded format or by the parameters that were used to generate the key. Either of these representations may be encapsulated in a key specification, which is used to interact with the KeyFactory class (java.security.KeyFactory) and the SecretKeyFactory class (javax.crypto.SecretKeyFactory).

The KeyFactory Class

Converting asymmetric keys is done with this class:

public class KeyFactory

Provide an infrastructure for importing and exporting keys according to the specific encoding format or parameters of the key.

Using the KeyFactory class

The KeyFactory class is an engine class, which provides the typical method of instantiating itself:

public static final KeyFactory getInstance(String alg) public static final KeyFactory getInstance(String alg, String provider)

Create a key factory capable of importing and exporting keys that were generated with the given algorithm. The class that implements the key factory comes from the named provider or is ...

Get Java Security, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.