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.math.BigInteger;
007: import java.security.InvalidKeyException;
008: import java.security.KeyPair;
009: import java.security.KeyPairGenerator;
010: import java.security.NoSuchProviderException;
011: import java.security.SecureRandom;
012: import java.security.Security;
013: import java.security.SignatureException;
014: import java.util.Date;
015:
016: import org.bouncycastle.bcpg.ArmoredOutputStream;
017: import org.bouncycastle.jce.provider.BouncyCastleProvider;
018: import org.bouncycastle.jce.spec.ElGamalParameterSpec;
019: import org.bouncycastle.openpgp.PGPEncryptedData;
020: import org.bouncycastle.openpgp.PGPException;
021: import org.bouncycastle.openpgp.PGPKeyPair;
022: import org.bouncycastle.openpgp.PGPKeyRingGenerator;
023: import org.bouncycastle.openpgp.PGPPublicKey;
024: import org.bouncycastle.openpgp.PGPSignature;
025:
026: /**
027: * A simple utility class that generates a public/secret keyring containing a DSA signing
028: * key and an El Gamal key for encryption.
029: * <p>
030: * usage: DSAElGamalKeyRingGenerator [-a] identity passPhrase
031: * <p>
032: * Where identity is the name to be associated with the public key. The keys are placed
033: * in the files pub.[asc|bpg] and secret.[asc|bpg].
034: * <p>
035: * <b>Note</b>: this example encrypts the secret key using AES_256, many PGP products still
036: * do not support this, if you are having problems importing keys try changing the algorithm
037: * id to PGPEncryptedData.CAST5. CAST5 is more widelysupported.
038: */
039: public class DSAElGamalKeyRingGenerator {
040: private static void exportKeyPair(OutputStream secretOut,
041: OutputStream publicOut, KeyPair dsaKp, KeyPair elgKp,
042: String identity, char[] passPhrase, boolean armor)
043: throws IOException, InvalidKeyException,
044: NoSuchProviderException, SignatureException, PGPException {
045: if (armor) {
046: secretOut = new ArmoredOutputStream(secretOut);
047: }
048:
049: PGPKeyPair dsaKeyPair = new PGPKeyPair(PGPPublicKey.DSA, dsaKp,
050: new Date(), "BC");
051: PGPKeyPair elgKeyPair = new PGPKeyPair(
052: PGPPublicKey.ELGAMAL_ENCRYPT, elgKp, new Date(), "BC");
053:
054: PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(
055: PGPSignature.POSITIVE_CERTIFICATION, dsaKeyPair,
056: identity, PGPEncryptedData.AES_256, passPhrase, true,
057: null, null, new SecureRandom(), "BC");
058:
059: keyRingGen.addSubKey(elgKeyPair);
060:
061: keyRingGen.generateSecretKeyRing().encode(secretOut);
062:
063: secretOut.close();
064:
065: if (armor) {
066: publicOut = new ArmoredOutputStream(publicOut);
067: }
068:
069: keyRingGen.generatePublicKeyRing().encode(publicOut);
070:
071: publicOut.close();
072: }
073:
074: public static void main(String[] args) throws Exception {
075: Security.addProvider(new BouncyCastleProvider());
076:
077: if (args.length < 2) {
078: System.out
079: .println("DSAElGamalKeyRingGenerator [-a] identity passPhrase");
080: System.exit(0);
081: }
082:
083: KeyPairGenerator dsaKpg = KeyPairGenerator.getInstance("DSA",
084: "BC");
085:
086: dsaKpg.initialize(1024);
087:
088: //
089: // this takes a while as the key generator has to generate some DSA params
090: // before it generates the key.
091: //
092: KeyPair dsaKp = dsaKpg.generateKeyPair();
093:
094: KeyPairGenerator elgKpg = KeyPairGenerator.getInstance(
095: "ELGAMAL", "BC");
096: BigInteger g = new BigInteger(
097: "153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc",
098: 16);
099: BigInteger p = new BigInteger(
100: "9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b",
101: 16);
102:
103: ElGamalParameterSpec elParams = new ElGamalParameterSpec(p, g);
104:
105: elgKpg.initialize(elParams);
106:
107: //
108: // this is quicker because we are using pregenerated parameters.
109: //
110: KeyPair elgKp = elgKpg.generateKeyPair();
111:
112: if (args[0].equals("-a")) {
113: if (args.length < 3) {
114: System.out
115: .println("DSAElGamalKeyRingGenerator [-a] identity passPhrase");
116: System.exit(0);
117: }
118:
119: FileOutputStream out1 = new FileOutputStream("secret.asc");
120: FileOutputStream out2 = new FileOutputStream("pub.asc");
121:
122: exportKeyPair(out1, out2, dsaKp, elgKp, args[1], args[2]
123: .toCharArray(), true);
124: } else {
125: FileOutputStream out1 = new FileOutputStream("secret.bpg");
126: FileOutputStream out2 = new FileOutputStream("pub.bpg");
127:
128: exportKeyPair(out1, out2, dsaKp, elgKp, args[0], args[1]
129: .toCharArray(), false);
130: }
131: }
132: }
|