package org.bouncycastle.openpgp.examples;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.SignatureException;
import java.util.Date;

import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSignature;

/**
 * A simple utility class that generates a RSA PGPPublicKey/PGPSecretKey pair.
 * <p>
 * usage: RSAKeyPairGenerator [-a] identity passPhrase
 * <p>
 * Where identity is the name to be associated with the public key. The keys are placed 
 * in the files pub.[asc|bpg] and secret.[asc|bpg].
 */
public class RSAKeyPairGenerator
{
	private static void exportKeyPair(
		OutputStream	secretOut,
		OutputStream	publicOut,
		PublicKey			publicKey,
		PrivateKey			privateKey,
		String				identity,
		char[]				passPhrase,
		boolean			armor)
		throws IOException, InvalidKeyException, NoSuchProviderException, SignatureException, PGPException
	{	
		if (armor)
		{
			secretOut = new ArmoredOutputStream(secretOut);
		}

		PGPSecretKey	secretKey = new PGPSecretKey(PGPSignature.DEFAULT_CERTIFICATION, PublicKeyAlgorithmTags.RSA_GENERAL, publicKey, privateKey, new Date(), identity, SymmetricKeyAlgorithmTags.CAST5, passPhrase, null, null, new SecureRandom(), "BC");
		
		secretKey.encode(secretOut);
		
		secretOut.close();
		
		if (armor)
		{
			publicOut = new ArmoredOutputStream(publicOut);
		}

		PGPPublicKey	key = secretKey.getPublicKey();
		
		key.encode(publicOut);
		
		publicOut.close();
	}
	
    public static void main(
    	String[] args)
    	throws Exception
    {
        Security.addProvider(new BouncyCastleProvider());

		KeyPairGenerator	kpg = KeyPairGenerator.getInstance("RSA", "BC");
		
		kpg.initialize(1024);
		
		KeyPair					kp = kpg.generateKeyPair();
		
		if (args.length < 2)
		{
			System.out.println("RSAKeyPairGenerator [-a] identity passPhrase");
			System.exit(0);
		}
		
		if (args[0].equals("-a"))
		{
			if (args.length < 3)
			{
				System.out.println("RSAKeyPairGenerator [-a] identity passPhrase");
				System.exit(0);
			}
			
			FileOutputStream	out1 = new FileOutputStream("secret.asc");
			FileOutputStream	out2 = new FileOutputStream("pub.asc");
			
			exportKeyPair(out1, out2, kp.getPublic(), kp.getPrivate(), args[1], args[2].toCharArray(), true);
		}
		else
		{
			FileOutputStream	out1 = new FileOutputStream("secret.bpg");
			FileOutputStream	out2 = new FileOutputStream("pub.bpg");
			
			exportKeyPair(out1, out2, kp.getPublic(), kp.getPrivate(), args[0], args[1].toCharArray(), false);
		}
    }
}
