public static KeyczarReader exportPublicKeys(GenericKeyczar privateKey) throws KeyczarException {
  KeyMetadata kmd=privateKey.getMetadata();
  KeyMetadata publicKmd=null;
  if (kmd.getType() == DefaultKeyType.DSA_PRIV) {
    if (kmd.getPurpose() == KeyPurpose.SIGN_AND_VERIFY) {
      publicKmd=new KeyMetadata(kmd.getName(),KeyPurpose.VERIFY,DefaultKeyType.DSA_PUB);
    }
  }
 else   if (kmd.getType() == DefaultKeyType.RSA_PRIV) {
switch (kmd.getPurpose()) {
case DECRYPT_AND_ENCRYPT:
      publicKmd=new KeyMetadata(kmd.getName(),KeyPurpose.ENCRYPT,DefaultKeyType.RSA_PUB);
    break;
case SIGN_AND_VERIFY:
  publicKmd=new KeyMetadata(kmd.getName(),KeyPurpose.VERIFY,DefaultKeyType.RSA_PUB);
break;
default :
throw new IllegalArgumentException("Invalid key purpose: " + kmd.getPurpose());
}
}
if (publicKmd == null) {
throw new KeyczarException(Messages.getString("KeyczarTool.CannotExportPubKey",kmd.getType(),kmd.getPurpose()));
}
HashMap<Integer,KeyczarKey> keys=new HashMap<Integer,KeyczarKey>();
for (KeyVersion version : privateKey.getVersions()) {
try {
KeyczarKey key=privateKey.getKey(version);
KeyczarKey publicKey=(KeyczarKey)getPublicMethod.invoke(key);
publicKmd.addVersion(version);
keys.put(version.getVersionNumber(),publicKey);
}
 catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
return new MemoryKeyReader(publicKmd,keys);
}

public static KeyczarReader exportPublicKeys ( GenericKeyczar privateKey ) throws KeyczarException { KeyMetadata kmd = privateKey . getMetadata ( ) ; KeyMetadata publicKmd = null ; if ( kmd . getType ( ) == DefaultKeyType . DSA_PRIV ) { if ( kmd . getPurpose ( ) == KeyPurpose . SIGN_AND_VERIFY ) { publicKmd = new KeyMetadata ( kmd . getName ( ) , KeyPurpose . VERIFY , DefaultKeyType . DSA_PUB ) ; } } else if ( kmd . getType ( ) == DefaultKeyType . RSA_PRIV ) { switch ( kmd . getPurpose ( ) ) { case DECRYPT_AND_ENCRYPT : publicKmd = new KeyMetadata ( kmd . getName ( ) , KeyPurpose . ENCRYPT , DefaultKeyType . RSA_PUB ) ; break ; case SIGN_AND_VERIFY : publicKmd = new KeyMetadata ( kmd . getName ( ) , KeyPurpose . VERIFY , DefaultKeyType . RSA_PUB ) ; break ; default : throw new IllegalArgumentException ( "Invalid key purpose: " + kmd . getPurpose ( ) ) ; } } if ( publicKmd == null ) { throw new KeyczarException ( Messages . getString ( "KeyczarTool.CannotExportPubKey" , kmd . getType ( ) , kmd . getPurpose ( ) ) ) ; } HashMap < Integer , KeyczarKey > keys = new HashMap < Integer , KeyczarKey > ( ) ; for ( KeyVersion version : privateKey . getVersions ( ) ) { try { KeyczarKey key = privateKey . getKey ( version ) ; KeyczarKey publicKey = ( KeyczarKey ) getPublicMethod . invoke ( key ) ; publicKmd . addVersion ( version ) ; keys . put ( version . getVersionNumber ( ) , publicKey ) ; } catch ( IllegalAccessException e ) { throw new RuntimeException ( e ) ; } catch ( InvocationTargetException e ) { throw new RuntimeException ( e ) ; } } return new MemoryKeyReader ( publicKmd , keys ) ; }
returns a new keyczar with public keys in privatekey .
