Here you can find the source of generateKeyPair(String algorithm, int bits)
Parameter | Description |
---|---|
algorithm | the algorithm of the key pair. |
bits | the strength |
KeyPair
the generated key pair.
public static KeyPair generateKeyPair(String algorithm, int bits) throws GeneralSecurityException
//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(); } }