📄 keypairgenerator.java
字号:
* @see Provider */ public static KeyPairGenerator getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException { Provider p = Security.getProvider(provider); if (p == null) throw new NoSuchProviderException(provider); return getInstance(algorithm, p); } /** * Generates a <code>KeyPairGenerator</code> object implementing the specified * algorithm, as supplied from the specified provider, if such an algorithm is * available from the provider. Note: the provider doesn't have to be * registered. * * @param algorithm the standard string name of the algorithm. See Appendix A * in the Java Cryptography Architecture API Specification & Reference for * information about standard algorithm names. * @param provider the provider. * @return the new <code>KeyPairGenerator</code> object. * @throws NoSuchAlgorithmException if the <code>algorithm</code> is not * available from the <code>provider</code>. * @throws IllegalArgumentException if the <code>provider</code> is * <code>null</code>. * @since 1.4 * @see Provider */ public static KeyPairGenerator getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException { if (provider == null) throw new IllegalArgumentException("Illegal provider"); Object o = null; try { o = Engine.getInstance(KEY_PAIR_GENERATOR, algorithm, provider); } catch (java.lang.reflect.InvocationTargetException ite) { throw new NoSuchAlgorithmException(algorithm); } KeyPairGenerator result = null; if (o instanceof KeyPairGeneratorSpi) { result = new DummyKeyPairGenerator((KeyPairGeneratorSpi) o, algorithm); } else if (o instanceof KeyPairGenerator) { result = (KeyPairGenerator) o; result.algorithm = algorithm; } result.provider = provider; return result; } /** * Returns the provider of this key pair generator object. * * @return the provider of this key pair generator object. */ public final Provider getProvider() { return provider; } /** * Initializes the key pair generator for a certain keysize using a default * parameter set and the {@link SecureRandom} implementation of the * highest-priority installed provider as the source of randomness. (If none * of the installed providers supply an implementation of {@link SecureRandom}, * a system-provided source of randomness is used.) * * @param keysize the keysize. This is an algorithm-specific metric, such as * modulus length, specified in number of bits. * @throws InvalidParameterException if the keysize is not supported by this * <code>KeyPairGenerator</code> object. */ public void initialize(int keysize) { initialize(keysize, new SecureRandom()); } /** * Initializes the key pair generator for a certain keysize with the given * source of randomness (and a default parameter set). * * @param keysize the keysize. This is an algorithm-specific metric, such as * modulus length, specified in number of bits. * @param random the source of randomness. * @throws InvalidParameterException if the <code>keysize</code> is not * supported by this <code>KeyPairGenerator</code> object. * @since 1.2 */ public void initialize(int keysize, SecureRandom random) { } /** * <p>Initializes the key pair generator using the specified parameter set and * the {@link SecureRandom} implementation of the highest-priority installed * provider as the source of randomness. (If none of the installed providers * supply an implementation of {@link SecureRandom}, a system-provided source * of randomness is used.)</p> * * <p>This concrete method has been added to this previously-defined abstract * class. This method calls the * {@link KeyPairGeneratorSpi#initialize(AlgorithmParameterSpec, SecureRandom)} * initialize method, passing it <code>params</code> and a source of * randomness (obtained from the highest-priority installed provider or * system-provided if none of the installed providers supply one). That * initialize method always throws an {@link UnsupportedOperationException} * if it is not overridden by the provider.</p> * * @param params the parameter set used to generate the keys. * @throws InvalidAlgorithmParameterException if the given parameters are * inappropriate for this key pair generator. * @since 1.2 */ public void initialize(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException { initialize(params, new SecureRandom()); } /** * <p>Initializes the key pair generator with the given parameter set and * source of randomness.</p> * * <p>This concrete method has been added to this previously-defined abstract * class. This method calls the * {@link KeyPairGeneratorSpi#initialize(AlgorithmParameterSpec, SecureRandom)} * initialize method, passing it <code>params</code> and <code>random</code>. * That initialize method always throws an {@link UnsupportedOperationException} * if it is not overridden by the provider.</p> * * @param params the parameter set used to generate the keys. * @param random the source of randomness. * @throws InvalidAlgorithmParameterException if the given parameters are * inappropriate for this key pair generator. * @since 1.2 */ public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException { super.initialize(params, random); } /** * <p>Generates a key pair.</p> * * <p>If this <code>KeyPairGenerator</code> has not been initialized * explicitly, provider-specific defaults will be used for the size and other * (algorithm-specific) values of the generated keys.</p> * * <p>This will generate a new key pair every time it is called.</p> * * <p>This method is functionally equivalent to {@link #generateKeyPair()}.</p> * * @return the generated key pair. * @since 1.2 */ public final KeyPair genKeyPair() { try { return getInstance("DSA", "GNU").generateKeyPair(); } catch (Exception e) { System.err.println("genKeyPair failed: " + e); e.printStackTrace(); return null; } } /** * <p>Generates a key pair.</p> * * <p>If this <code>KeyPairGenerator</code> has not been initialized * explicitly, provider-specific defaults will be used for the size and other * (algorithm-specific) values of the generated keys.</p> * * <p>This will generate a new key pair every time it is called.</p> * * <p>This method is functionally equivalent to {@link #genKeyPair()}.</p> * * @return the generated key pair. */ public KeyPair generateKeyPair() { return genKeyPair(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -