Here you can find the source of generateKeyPair()
Parameter | Description |
---|---|
NoSuchProviderException | an exception |
NoSuchAlgorithmException | an exception |
public static KeyPair generateKeyPair() throws NoSuchProviderException, NoSuchAlgorithmException
//package com.java2s; //License from project: Apache License import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; public class Main { /**/* w w w .j av a2 s . co m*/ * The default key length to use when generating a keypair. */ public static final int DEFAULT_KEY_LENGTH = 4096; /** * Create new public & private keys with length 4096. * * @return A new pair of public & private keys * @throws NoSuchProviderException * @throws NoSuchAlgorithmException * @see #generateKeyPair(int) */ public static KeyPair generateKeyPair() throws NoSuchProviderException, NoSuchAlgorithmException { return generateKeyPair(DEFAULT_KEY_LENGTH); } /** * Create new public & private keys of the provided length. * * @return A new pair of public & private keys * @throws NoSuchProviderException * @throws NoSuchAlgorithmException * @see #generateKeyPair() */ public static KeyPair generateKeyPair(int keyLength) throws NoSuchProviderException, NoSuchAlgorithmException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(keyLength); return keyGen.generateKeyPair(); } }