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

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

Description

Generates a key pair of given algorithm and strength.

License

Apache License

Parameter

Parameter Description
algorithm the algorithm of the key pair.
bits the strength

Return

KeyPair the generated key pair.

Declaration

public static KeyPair generateKeyPair(String algorithm, int bits) throws GeneralSecurityException 

Method Source Code

//package com.java2s;
/*//from   w ww  . j a v a2  s  . com
 * Copyright 1999-2010 University of Chicago
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 */

import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;

public class Main {
    private static String provider;

    /**
     * Generates a key pair of given algorithm and strength.
     *
     * @param algorithm the algorithm of the key pair.
     * @param bits the strength
     * @return <code>KeyPair</code> the generated key pair.
     * @exception GeneralSecurityException if something goes wrong.
     */
    public static KeyPair generateKeyPair(String algorithm, int bits) throws GeneralSecurityException {
        KeyPairGenerator generator = null;
        if (provider == null) {
            generator = KeyPairGenerator.getInstance(algorithm);
        } else {
            generator = KeyPairGenerator.getInstance(algorithm, provider);
        }
        generator.initialize(bits);
        return generator.generateKeyPair();
    }
}

Related

  1. generateKeyPair(int keySizeInBits, String algo)
  2. generateKeyPair(int len)
  3. generateKeyPair(int size)
  4. generateKeyPair(String algo)
  5. generateKeyPair(String algo, int keyLength, String provider)
  6. generateKeyPair(String algorithm, int keySize)
  7. generateKeyPair(String algorithm, int keysize)
  8. generateKeyPair(String algorithm, int size)
  9. generateKeyPair(String keyAlgorithm, int keySize)