Java Key Pair Create generateKeyPair(int size)

Here you can find the source of generateKeyPair(int size)

Description

This method generates a key pair for RSA encryption.

License

Open Source License

Parameter

Parameter Description
size is the size in bits of the to be generated key.

Return

A is returned created with the given size.

Declaration

public static KeyPair generateKeyPair(int size) 

Method Source Code

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

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

public class Main {
    private static final String RSA_ALGORITHM_NAME = "RSA";

    /**//from ww w  .  jav  a2  s.  c o m
     * This method generates a key pair for RSA encryption.
     * 
     * @param size
     *            is the size in bits of the to be generated key.
     * @return A {@link KeyPair} is returned created with the given size.
     */
    public static KeyPair generateKeyPair(int size) {
        try {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA_ALGORITHM_NAME);
            keyPairGenerator.initialize(size);
            return keyPairGenerator.genKeyPair();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Could not generate RSA key pair!", e);
        }
    }
}

Related

  1. generateKeyPair(int keySize)
  2. generateKeyPair(int keysize)
  3. generateKeyPair(int keySize, String fileChavePublica, String fileChavePrivada)
  4. generateKeyPair(int keySizeInBits, String algo)
  5. generateKeyPair(int len)
  6. generateKeyPair(String algo)
  7. generateKeyPair(String algo, int keyLength, String provider)
  8. generateKeyPair(String algorithm, int bits)
  9. generateKeyPair(String algorithm, int keySize)