Java Key Pair Create generateKeyPair()

Here you can find the source of generateKeyPair()

Description

generate Key Pair

License

Open Source License

Declaration

public static KeyPair generateKeyPair()
            throws NoSuchAlgorithmException,
            InvalidAlgorithmParameterException 

Method Source Code

//package com.java2s;
/*//from  w w  w. j a  va  2  s.c  o m
 * SafeOnline project.
 *
 * Copyright 2005-2006 Frank Cornelis.
 * Copyright 2006-2007 Lin.k N.V. All rights reserved.
 * Lin.k N.V. proprietary/confidential. Use is subject to license terms.
 */

import java.security.*;

import java.security.interfaces.DSAKeyPairGenerator;
import java.security.spec.RSAKeyGenParameterSpec;

public class Main {
    protected static final int RSA_KEYSIZE = 1024;
    protected static final int DSA_MODLEN = 512;

    public static KeyPair generateKeyPair()
            throws NoSuchAlgorithmException,
            InvalidAlgorithmParameterException {

        return generateKeyPair("RSA");
    }

    public static KeyPair generateKeyPair(String algorithm)
            throws NoSuchAlgorithmException,
            InvalidAlgorithmParameterException {

        KeyPairGenerator keyPairGenerator = KeyPairGenerator
                .getInstance(algorithm);
        SecureRandom random = new SecureRandom();
        if ("RSA".equals(keyPairGenerator.getAlgorithm()))
            keyPairGenerator.initialize(new RSAKeyGenParameterSpec(
                    RSA_KEYSIZE, RSAKeyGenParameterSpec.F4), random);
        else if (keyPairGenerator instanceof DSAKeyPairGenerator) {
            DSAKeyPairGenerator dsaKeyPairGenerator = (DSAKeyPairGenerator) keyPairGenerator;
            dsaKeyPairGenerator.initialize(DSA_MODLEN, false, random);
        }
        return keyPairGenerator.generateKeyPair();
    }
}

Related

  1. generateKeyDigest(int keyLength, String password)
  2. generateKeyFromString(final String secKey, String algorithm)
  3. generateKeyPair()
  4. generateKeyPair()
  5. generateKeyPair()
  6. generateKeyPair()
  7. generateKeyPair()
  8. generateKeyPair()
  9. generateKeyPair()