generates a key pair (private key, public key). - Java Security

Java examples for Security:Key

Description

generates a key pair (private key, public key).

Demo Code


//package com.java2s;

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

import java.security.SecureRandom;

public class Main {
    private static final int KEYSIZE = 2048;

    /**/*from w w w  .ja  va2s  . com*/
     * generates a key pair (private key, public key).
     *
     * @return  KeyPair
     *
     * @throws  NoSuchAlgorithmException
     */
    public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {

        KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA");
        SecureRandom random = new SecureRandom();
        pairgen.initialize(KEYSIZE, random);

        return pairgen.generateKeyPair();
    }
}

Related Tutorials