Here you can find the source of generateKey(String publicKeyFilename, String privateKeyFilename, String password)
public void generateKey(String publicKeyFilename, String privateKeyFilename, String password) throws IOException, NoSuchAlgorithmException
//package com.java2s; //License from project: Apache License import java.io.FileOutputStream; import java.io.IOException; import java.security.*; import java.util.HashMap; import java.util.Map; public class Main { public void generateKey(String publicKeyFilename, String privateKeyFilename, String password) throws IOException, NoSuchAlgorithmException { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); SecureRandom secureRandom = new SecureRandom(password.getBytes()); keyPairGenerator.initialize(1024, secureRandom); KeyPair keyPair = keyPairGenerator.genKeyPair(); byte[] publicKeyBytes = keyPair.getPublic().getEncoded(); FileOutputStream fos = new FileOutputStream(publicKeyFilename); fos.write(publicKeyBytes);/*www. ja v a 2 s . co m*/ fos.close(); byte[] privateKeyBytes = keyPair.getPrivate().getEncoded(); fos = new FileOutputStream(privateKeyFilename); fos.write(privateKeyBytes); fos.close(); } public static Map<String, byte[]> generateKey(String password) throws IOException, NoSuchAlgorithmException { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); SecureRandom secureRandom = new SecureRandom(password.getBytes()); keyPairGenerator.initialize(1024, secureRandom); KeyPair keyPair = keyPairGenerator.genKeyPair(); byte[] publicKeyBytes = keyPair.getPublic().getEncoded(); byte[] privateKeyBytes = keyPair.getPrivate().getEncoded(); Map<String, byte[]> map = new HashMap<String, byte[]>(); map.put("pub", publicKeyBytes); map.put("pri", privateKeyBytes); return map; } }