/** * Generate key pair and saves it to specified files. * * @param privateKeyLocation the private key location * @param publicKeyLocation the public key location * @return the key pair */ public static KeyPair generateKeyPair(String privateKeyLocation, String publicKeyLocation) { try { KeyPair clientKeyPair = generateKeyPair(); saveKeyPair(clientKeyPair, privateKeyLocation, publicKeyLocation); return clientKeyPair; } catch (Exception ex) { LOG.error("Error generating client key pair", ex); } return null; }
/** * Generate key pair and saves it to specified streams. * * @param privateKeyOutput the private key output stream * @param publicKeyOutput the public key output stream * @return the key pair */ public static KeyPair generateKeyPair( OutputStream privateKeyOutput, OutputStream publicKeyOutput) { try { KeyPair clientKeyPair = generateKeyPair(); saveKeyPair(clientKeyPair, privateKeyOutput, publicKeyOutput); return clientKeyPair; } catch (Exception ex) { LOG.error("Error generating client key pair", ex); } return null; }
/** * Saves public and private keys to specified files. * * @param keyPair the key pair * @param privateKeyFile the private key file * @param publicKeyFile the public key file * @throws IOException Signals that an I/O exception has occurred. */ public static void saveKeyPair(KeyPair keyPair, String privateKeyFile, String publicKeyFile) throws IOException { File privateFile = makeDirs(privateKeyFile); File publicFile = makeDirs(publicKeyFile); OutputStream privateKeyOutput = null; OutputStream publicKeyOutput = null; try { privateKeyOutput = new FileOutputStream(privateFile); publicKeyOutput = new FileOutputStream(publicFile); saveKeyPair(keyPair, privateKeyOutput, publicKeyOutput); } finally { IOUtils.closeQuietly(privateKeyOutput); IOUtils.closeQuietly(publicKeyOutput); } }