Generating an Encryption Key

The JCE represents keys two different ways. First, there is a class called SecretKey that is an opaque version of the key. That is, although the SecretKey class represents a key, you don't have access to the individual bits in the key. The other representation is called a key specification. A key specification, or key spec, is represented by the SecretKeySpec class and by algorithm-specific subclasses such as DESKeySpec or PBEKeySpec.

The easiest way to generate a random key is just to use a KeyGenerator class. Use the getInstance method to get a key generator for the particular algorithm you want to use. For example, to generate a random key for DESede, you first call

 KeyGenerator keyGen = KeyGenerator.getInstance("DESede"); ...

Get Special Edition Using Java™ 2 Enterprise 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.