Java Key Pair Create generateKeyPair()

Here you can find the source of generateKeyPair()

Description

Generates key-pair with KeyUtils#DEFAULT_KEY_ALGORITHM algorithm and KeyUtils#DEFAULT_KEY_SIZE key size.

License

Open Source License

Exception

Parameter Description
NoSuchAlgorithmException an exception

Return

generated key-pair

Declaration

public static KeyPair generateKeyPair() throws NoSuchAlgorithmException 

Method Source Code

//package com.java2s;
/*/*from   w  w  w. j  a  va2  s  .  c om*/
 * JBoss, Home of Professional Open Source.
 * Copyright 2017, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;

public class Main {
    private static final String DEFAULT_KEY_ALGORITHM = "RSA";
    private static final int DEFAULT_KEY_SIZE = 2048;

    /**
     * Generates key-pair with {@link KeyUtils#DEFAULT_KEY_ALGORITHM} algorithm and {@link KeyUtils#DEFAULT_KEY_SIZE}
     * key size.
     *
     * @return generated key-pair
     * @throws NoSuchAlgorithmException
     */
    public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
        return generateKeyPair(DEFAULT_KEY_ALGORITHM, DEFAULT_KEY_SIZE);
    }

    /**
     * Generates key-pair with given algorithm and key size.
     *
     * @param algorithm
     * @param keySize
     * @return generated key-pair
     * @throws NoSuchAlgorithmException
     */
    public static KeyPair generateKeyPair(String algorithm, int keySize) throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
        keyPairGenerator.initialize(keySize);
        return keyPairGenerator.generateKeyPair();
    }
}

Related

  1. generateKeyPair()
  2. generateKeyPair()
  3. generateKeyPair()
  4. generateKeyPair()
  5. generateKeyPair()
  6. generateKeyPair()
  7. generateKeyPair()
  8. generateKeyPair()
  9. generateKeyPair(final Provider provider, final String keyPairAlgorithm, final String secureAlgorithm)