001: package org.bouncycastle.openpgp.examples;
002:
003: import java.io.*;
004:
005: import java.security.PrivateKey;
006: import java.security.PublicKey;
007: import java.security.Security;
008: import java.util.Iterator;
009:
010: import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
011: import org.bouncycastle.jce.provider.BouncyCastleProvider;
012:
013: import org.bouncycastle.openpgp.PGPPublicKey;
014: import org.bouncycastle.openpgp.PGPPublicKeyRing;
015: import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
016: import org.bouncycastle.openpgp.PGPUtil;
017:
018: import org.bouncycastle.util.encoders.Hex;
019:
020: /**
021: * Basic class which just lists the contents of the public key file passed
022: * as an argument. If the file contains more than one "key ring" they are
023: * listed in the order found.
024: */
025: public class PubringDump {
026: public static String getAlgorithm(int algId) {
027: switch (algId) {
028: case PublicKeyAlgorithmTags.RSA_GENERAL:
029: return "RSA_GENERAL";
030: case PublicKeyAlgorithmTags.RSA_ENCRYPT:
031: return "RSA_ENCRYPT";
032: case PublicKeyAlgorithmTags.RSA_SIGN:
033: return "RSA_SIGN";
034: case PublicKeyAlgorithmTags.ELGAMAL_ENCRYPT:
035: return "ELGAMAL_ENCRYPT";
036: case PublicKeyAlgorithmTags.DSA:
037: return "DSA";
038: case PublicKeyAlgorithmTags.EC:
039: return "EC";
040: case PublicKeyAlgorithmTags.ECDSA:
041: return "ECDSA";
042: case PublicKeyAlgorithmTags.ELGAMAL_GENERAL:
043: return "ELGAMAL_GENERAL";
044: case PublicKeyAlgorithmTags.DIFFIE_HELLMAN:
045: return "DIFFIE_HELLMAN";
046: }
047:
048: return "unknown";
049: }
050:
051: public static void main(String[] args) throws Exception {
052: Security.addProvider(new BouncyCastleProvider());
053:
054: PGPPublicKey pubKey = null;
055: PrivateKey privKey = null;
056:
057: PGPUtil.setDefaultProvider("BC");
058:
059: //
060: // Read the public key rings
061: //
062: PGPPublicKeyRingCollection pubRings = new PGPPublicKeyRingCollection(
063: PGPUtil.getDecoderStream(new FileInputStream(args[0])));
064:
065: Iterator rIt = pubRings.getKeyRings();
066:
067: while (rIt.hasNext()) {
068: PGPPublicKeyRing pgpPub = (PGPPublicKeyRing) rIt.next();
069:
070: try {
071: pubKey = pgpPub.getPublicKey();
072: } catch (Exception e) {
073: e.printStackTrace();
074: continue;
075: }
076:
077: long pgpKeyID = 0;
078: PublicKey pKey = null;
079:
080: Iterator it = pgpPub.getPublicKeys();
081: boolean first = true;
082: while (it.hasNext()) {
083: PGPPublicKey pgpKey = (PGPPublicKey) it.next();
084:
085: if (first) {
086: System.out.println("Key ID: "
087: + Long.toHexString(pgpKey.getKeyID()));
088: first = false;
089: } else {
090: System.out.println("Key ID: "
091: + Long.toHexString(pgpKey.getKeyID())
092: + " (subkey)");
093: }
094: System.out.println(" Algorithm: "
095: + getAlgorithm(pgpKey.getAlgorithm()));
096: System.out.println(" Fingerprint: "
097: + new String(Hex
098: .encode(pgpKey.getFingerprint())));
099: }
100: }
101: }
102: }
|