001: package org.bouncycastle.openpgp.examples;
002:
003: import java.io.FileOutputStream;
004: import java.io.IOException;
005: import java.io.OutputStream;
006: import java.security.InvalidKeyException;
007: import java.security.KeyPair;
008: import java.security.KeyPairGenerator;
009: import java.security.NoSuchProviderException;
010: import java.security.PrivateKey;
011: import java.security.PublicKey;
012: import java.security.SecureRandom;
013: import java.security.Security;
014: import java.security.SignatureException;
015: import java.util.Date;
016:
017: import org.bouncycastle.bcpg.ArmoredOutputStream;
018: import org.bouncycastle.jce.provider.BouncyCastleProvider;
019: import org.bouncycastle.openpgp.PGPEncryptedData;
020: import org.bouncycastle.openpgp.PGPException;
021: import org.bouncycastle.openpgp.PGPPublicKey;
022: import org.bouncycastle.openpgp.PGPSecretKey;
023: import org.bouncycastle.openpgp.PGPSignature;
024:
025: /**
026: * A simple utility class that generates a RSA PGPPublicKey/PGPSecretKey pair.
027: * <p>
028: * usage: RSAKeyPairGenerator [-a] identity passPhrase
029: * <p>
030: * Where identity is the name to be associated with the public key. The keys are placed
031: * in the files pub.[asc|bpg] and secret.[asc|bpg].
032: */
033: public class RSAKeyPairGenerator {
034: private static void exportKeyPair(OutputStream secretOut,
035: OutputStream publicOut, PublicKey publicKey,
036: PrivateKey privateKey, String identity, char[] passPhrase,
037: boolean armor) throws IOException, InvalidKeyException,
038: NoSuchProviderException, SignatureException, PGPException {
039: if (armor) {
040: secretOut = new ArmoredOutputStream(secretOut);
041: }
042:
043: PGPSecretKey secretKey = new PGPSecretKey(
044: PGPSignature.DEFAULT_CERTIFICATION,
045: PGPPublicKey.RSA_GENERAL, publicKey, privateKey,
046: new Date(), identity, PGPEncryptedData.CAST5,
047: passPhrase, null, null, new SecureRandom(), "BC");
048:
049: secretKey.encode(secretOut);
050:
051: secretOut.close();
052:
053: if (armor) {
054: publicOut = new ArmoredOutputStream(publicOut);
055: }
056:
057: PGPPublicKey key = secretKey.getPublicKey();
058:
059: key.encode(publicOut);
060:
061: publicOut.close();
062: }
063:
064: public static void main(String[] args) throws Exception {
065: Security.addProvider(new BouncyCastleProvider());
066:
067: KeyPairGenerator kpg = KeyPairGenerator
068: .getInstance("RSA", "BC");
069:
070: kpg.initialize(1024);
071:
072: KeyPair kp = kpg.generateKeyPair();
073:
074: if (args.length < 2) {
075: System.out
076: .println("RSAKeyPairGenerator [-a] identity passPhrase");
077: System.exit(0);
078: }
079:
080: if (args[0].equals("-a")) {
081: if (args.length < 3) {
082: System.out
083: .println("RSAKeyPairGenerator [-a] identity passPhrase");
084: System.exit(0);
085: }
086:
087: FileOutputStream out1 = new FileOutputStream("secret.asc");
088: FileOutputStream out2 = new FileOutputStream("pub.asc");
089:
090: exportKeyPair(out1, out2, kp.getPublic(), kp.getPrivate(),
091: args[1], args[2].toCharArray(), true);
092: } else {
093: FileOutputStream out1 = new FileOutputStream("secret.bpg");
094: FileOutputStream out2 = new FileOutputStream("pub.bpg");
095:
096: exportKeyPair(out1, out2, kp.getPublic(), kp.getPrivate(),
097: args[0], args[1].toCharArray(), false);
098: }
099: }
100: }
|