Java Key Pair Create generateKeyPair(String algorithm, int size)

Here you can find the source of generateKeyPair(String algorithm, int size)

Description

generate Key Pair

License

Open Source License

Declaration

public static KeyPair generateKeyPair(String algorithm, int size) throws KeyException 

Method Source Code


//package com.java2s;
/*/* w w w  .java 2s .  c  o m*/
 * ProActive Parallel Suite(TM):
 * The Open Source library for parallel and distributed
 * Workflows & Scheduling, Orchestration, Cloud Automation
 * and Big Data Analysis on Enterprise Grids & Clouds.
 *
 * Copyright (c) 2007 - 2017 ActiveEon
 * Contact: contact@activeeon.com
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation: version 3 of
 * the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 */

import java.io.File;
import java.io.FileOutputStream;
import java.security.KeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;

public class Main {
    /**
     * Generates a pair of public and private keys
     * 
     * @param algorithm algorithm used for key generation, ie RSA
     * @param size size of the generated key, must be power of 2 and greater than 512
     * @param privPath path to file to which the generated private key will be saved
     * @param pubPath path to file to which the generated public key will be saved
     * @throws KeyException key generation or saving failed
     */
    public static void generateKeyPair(String algorithm, int size, String privPath, String pubPath)
            throws KeyException {
        KeyPair keyPair = generateKeyPair(algorithm, size);

        PrivateKey privKey = keyPair.getPrivate();
        PublicKey pubKey = keyPair.getPublic();

        FileOutputStream out = null;

        try {
            out = new FileOutputStream(new File(privPath));
            out.write(privKey.getEncoded());
            out.close();
        } catch (Exception e) {
            throw new KeyException("Cannot write private key to disk", e);
        }

        try {
            out = new FileOutputStream(new File(pubPath));
            out.write((algorithm + "\n").getBytes());
            out.write((size + "\n").getBytes());
            out.write(pubKey.getEncoded());
            out.close();
        } catch (Exception e) {
            throw new KeyException("Cannot write public key to disk", e);
        }
    }

    public static KeyPair generateKeyPair(String algorithm, int size) throws KeyException {
        KeyPairGenerator keyGen = null;
        try {
            keyGen = KeyPairGenerator.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new KeyException("Cannot initialize keypair generator", e);
        }

        SecureRandom random = new SecureRandom();
        keyGen.initialize(size, random);

        return keyGen.generateKeyPair();
    }
}

Related

  1. generateKeyPair(String algo)
  2. generateKeyPair(String algo, int keyLength, String provider)
  3. generateKeyPair(String algorithm, int bits)
  4. generateKeyPair(String algorithm, int keySize)
  5. generateKeyPair(String algorithm, int keysize)
  6. generateKeyPair(String keyAlgorithm, int keySize)
  7. generateKeyPair(String type, int size)
  8. generateKeys(int size)
  9. generateKeyStoreFromPEM(File certFile, File keyFile)