Here you can find the source of generateKeyPair(int keySize, String fileChavePublica, String fileChavePrivada)
public static void generateKeyPair(int keySize, String fileChavePublica, String fileChavePrivada) throws NoSuchAlgorithmException, FileNotFoundException, IOException
//package com.java2s; //License from project: Open Source License import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; public class Main { public static void generateKeyPair(int keySize, String fileChavePublica, String fileChavePrivada) throws NoSuchAlgorithmException, FileNotFoundException, IOException {//from ww w . jav a 2 s . c o m KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA"); SecureRandom random = new SecureRandom(); pairgen.initialize(keySize, random); KeyPair keyPair = pairgen.generateKeyPair(); ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(fileChavePublica)); out.writeObject(keyPair.getPublic()); out.close(); out = new ObjectOutputStream(new FileOutputStream(fileChavePrivada)); out.writeObject(keyPair.getPrivate()); out.close(); } }