Here you can find the source of generateKeyPair()
public static void generateKeyPair()
//package com.java2s; //License from project: Apache License import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.SecureRandom; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; public class Main { public static void generateKeyPair() { try {/*ww w . j av a 2 s . co m*/ KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); SecureRandom random = new SecureRandom(); random.setSeed(System.currentTimeMillis()); generator.initialize(1024, random); KeyPair keyPair = generator.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // System.out.println(publicKey.getModulus()); // System.out.println(publicKey.getPublicExponent()); // System.out.println(); // System.out.println(publicKey.getModulus().toString(16)); // System.out.println(publicKey.getPublicExponent().toString(16)); // System.out.println(); // System.out.println(privateKey.getPrivateExponent()); // // System.out.println(); System.out.println("Public:\r\n" + bytes2Hex(publicKey.getEncoded())); System.out.println(); System.out.println("Private:\r\n" + bytes2Hex(privateKey.getEncoded())); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } private static String bytes2Hex(byte[] bytes) { StringBuilder buff = new StringBuilder(); for (byte b : bytes) { String hex = Integer.toHexString(0xff & b); if (hex.length() < 2) { buff.append('0'); } buff.append(hex); } return buff.toString(); } }