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

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

Description

generate Key Pair

License

Open Source License

Declaration

public static KeyPair generateKeyPair(String algorithm, int keysize) throws NoSuchAlgorithmException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static KeyPair generateKeyPair(String algorithm, int keysize) throws NoSuchAlgorithmException {
        if (algorithm.equalsIgnoreCase("RSA") && keysize < 1024)
            throw new IllegalArgumentException("key size should be 1024 or higher");
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
        SecureRandom rnd = null;/*from w ww.java  2  s. c  om*/
        try {
            rnd = SecureRandom.getInstanceStrong();
        } catch (NoSuchAlgorithmException e) {
            rnd = new SecureRandom();
        }
        keyGen.initialize(keysize, rnd);
        KeyPair pair = keyGen.generateKeyPair();
        return pair;
    }

    public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
        return generateKeyPair(1.0);
    }

    public static KeyPair generateKeyPair(double multiplier) throws NoSuchAlgorithmException {
        return generateKeyPair("RSA", (int) (4096 * multiplier));
    }
}

Related

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