The oracle.security.crypto.core.KeyPairGenerator Class The oracle.security.crypto.core.SymmetricKeyGenerator Class

3-4 Oracle Fusion Middleware Reference for Oracle Security Developer Tools

3.3.2.1 The oracle.security.crypto.core.KeyPairGenerator Class

This abstract class is used to generate key pairs such as RSA, DSA, Diffie-Hellman or ECDSA key pairs. To get a new key pair generator, create a new instance of KeyPairGenerator by calling the static getInstance method with an AlgorithmIdentifier object as a parameter. Example 3–1 shows how to create a new KeyPairGenerator instance: Example 3–1 Code Example for Creating a New KeyPairGenerator Instance KeyPairGenerator kpg = KeyPairGenerator.getInstanceAlgID.rsaEncryption; This creates a KeyPairGenerator object from one of the concrete classes: RSAKeyPairGenerator, DSAKeyPairGenerator, DHKeyPairGenerator, or ECKeyPairGenerator. Initialize the key pair generator by using one of the initialize methods. Generate the key pair with the generateKeyPair method. Example 3–2 shows how to initialize the key pair generator and then generate a key pair: Example 3–2 Code Example for Initializing and Generating a Key Pair kpg.initialize1024, RandomBitsSource.getDefault; KeyPair kp = kpg.generateKeyPair; PrivateKey privKey = kp.getPrivate; PublicKey pubKey = kp.getPublic; Save the keys using the output method, or in the case of the private key, encrypt it and save it using the PrivateKeyPKCS8 class. Example 3–3 shows how to save a key pair. Example 3–3 Code Example for Saving a Key Pair FileOutputStream pubKeyFos = new FileOutputStreammy-pub-key.der; pubKey.outputpubKeyFos; pubKeyFos.close; PrivateKeyPKCS8 privKeyPKCS8 = new PrivateKeyPKCS8privKey, myPassword; FileOutputStream privKeyFos = new FileOutputStreammy-encrypted-priv-key.der; privKeyPKCS8.outputprivKeyFos; privKeyFos.close;

3.3.2.2 The oracle.security.crypto.core.SymmetricKeyGenerator Class

This class generates symmetric key pairs such as Blowfish, DES, 3DES, RC4, RC2, AES, and HMAC keys. To get a new symmetric key generator, create a new instance of SymmetricKeyGenerator by calling the static getInstance method with an AlgorithmIdentifier object as a parameter. Example 3–4 shows how to create a new SymmetricKeyGenerator instance: Example 3–4 Code Example for Creating a New SymmetricKeyGenerator Instance SymmetricKeyGenerator skg = SymmetricKeyGenerator.getInstanceAlgID.desCBC; Oracle Crypto 3-5 Generate the key pair with the generateKey method. You can then save the key by using the getEncoded method. Example 3–5 shows how to generate and save a symmetric key pair. Example 3–5 Code Example for Generating and Saving Symmetric Keys SymmetricKey sk = skg.generateKey; FileOutputStream symKeyFos = new FileOutputStreammy-sym-key.der; symKeyFos.writesk.getEncoded; symKeyFos.close;

3.3.3 Ciphers