[prev in list] [next in list] [prev in thread] [next in thread] 

List:       bouncycastle-crypto-dev
Subject:    Re: [dev-crypto] Exporting and Importing (Master and subkey)
From:       "Sai Pullabhotla" <sai.pullabhotla () jmethods ! com>
Date:       2008-06-25 13:17:14
Message-ID: 44ed9df20806250617y8c7f3f0y5fe4271073a7703d () mail ! gmail ! com
[Download RAW message or body]

You only need to know the ID of the master key in the entire keyring.
Then you can call PGPPublicKeyRingCollection.getPublicKeyRing(keyID)
to get the particular master key and all the subkeys. Encode it to a
file with armoring on to save the key to the file.

Regards,
Sai Pullabhotla

On Tue, Jun 24, 2008 at 5:20 PM, Anjul Narain Varma
<anjul_narain@yahoo.com> wrote:
> I have a .pkr with a collection of public keys. Most of the keys have a
> Master key/sub key combination. I need to export a particular Master key/sub
> key to a .asc file and I have the keyID of both the keys. Similarly i want
> to import a MasterKey/Subkey to the existing .pkr file.
>
>
>
> Below is the code I am using to export a Master key/Sub key, is this a good
> approach?
>
>
>
>   InputStream in = null;
>   OutputStream publicOut;
>
>
>   // key ID of the master key to export
>   String keyId = "96134d8f4f24aa04";
>
>
>
>   // key ID of the sub key to export
>   String subKeyId = "70ffd11e25596864";
>
>   // output file name
>   String keyFileLocation = "pubTest.asc";
>
>   // search for the key in the public key ring collection (.pkr)  file
>   File publicKeyRingCollectionfile = new File(publicKeyRingCollectionName);
>
>   if(publicKeyRingCollectionfile.exists()){
>    try{
>     in = PGPUtil.getDecoderStream(new
> FileInputStream(publicKeyRingCollectionfile));
>     PGPPublicKeyRingCollection pubKeyRingCollection = new
> PGPPublicKeyRingCollection(in);
>     PGPPublicKey    masterkey = null;
>     PGPPublicKey    subkey = null;
>
>           // iterate through the key rings.
>           Iterator rIt = pubKeyRingCollection.getKeyRings();
>
>           while (rIt.hasNext())
>           {
>               PGPPublicKeyRing    kRing = (PGPPublicKeyRing)rIt.next();
>
>               Iterator            kIt = kRing.getPublicKeys();
>
>
>               while (kIt.hasNext())
>               {
>                   PGPPublicKey    pgpKey = (PGPPublicKey)kIt.next();
>
>                   // check if the Master key matches
>                   if (Long.toHexString(pgpKey.getKeyID()).equals(keyId) &&
> pgpKey.isMasterKey()){
>                    masterkey = pgpKey;
>                    System.out.println("Found the master key=" + masterkey);
>                   }
>
>
>                   // check if the sub key matches
>                   if (Long.toHexString(pgpKey.getKeyID()).equals(subKeyId)
> && pgpKey.isEncryptionKey()){
>                    subkey = pgpKey;
>                    System.out.println("Found the sub key=" + subkey);
>                   }
>               }
>           }
>
>           if(masterkey!= null && subkey!=null){
>               publicOut = new FileOutputStream(keyFileLocation);
>                  publicOut = new ArmoredOutputStream(publicOut);
>                  masterkey.encode(publicOut);
>                  subkey.encode(publicOut);
>                  publicOut.close();
>              }
>
>    }
>    catch(Exception ex){
>     System.err.println("Unable to export the master key/sub key to a file"+
> ex);
>
>    }
>   }
>   else{
>    System.err.println("The public key ring collection "+
> publicKeyRingCollectionName + " does not exist!" );
>
>   }
>
>  }
>



-- 
Sai Pullabhotla
Phone: (402) 408-5753
Fax: (402) 408-6861
www.jMethods.com

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic