Java Key Pair Create generateKey(String publicKeyFilename, String privateKeyFilename, String password)

Here you can find the source of generateKey(String publicKeyFilename, String privateKeyFilename, String password)

Description

generate Key

License

Apache License

Declaration

public void generateKey(String publicKeyFilename, String privateKeyFilename, String password)
        throws IOException, NoSuchAlgorithmException 

Method Source Code

//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;
    }
}

Related

  1. generateKey(String algorithm, int size)
  2. generateKey(String cipherAlgorithm)
  3. generateKey(String keyPhrase)
  4. generateKey(String password)
  5. generateKey(String PRIVATE_KEY_FILE, String PUBLIC_KEY_FILE)
  6. generateKey(String secretKey)
  7. generateKey(String seed)
  8. generateKey(String type)
  9. generateKey(String userKey, String masterKey)