@SuppressWarnings("unchecked")
  public static boolean isCertificationKey(PGPPublicKey key) {
    if (key.getVersion() <= 3) {
      return true;
    }

    for (PGPSignature sig : new IterableIterator<PGPSignature>(key.getSignatures())) {
      if (key.isMasterKey() && sig.getKeyID() != key.getKeyID()) {
        continue;
      }
      PGPSignatureSubpacketVector hashed = sig.getHashedSubPackets();

      if (hashed != null && (hashed.getKeyFlags() & KeyFlags.CERTIFY_OTHER) != 0) {
        return true;
      }

      PGPSignatureSubpacketVector unhashed = sig.getUnhashedSubPackets();

      if (unhashed != null && (unhashed.getKeyFlags() & KeyFlags.CERTIFY_OTHER) != 0) {
        return true;
      }
    }

    return false;
  }
  @SuppressWarnings("unchecked")
  public static boolean isSigningKey(PGPPublicKey key) {
    if (key.getVersion() <= 3) {
      return true;
    }

    // special case
    if (key.getAlgorithm() == PGPPublicKey.RSA_SIGN) {
      return true;
    }

    for (PGPSignature sig : new IterableIterator<PGPSignature>(key.getSignatures())) {
      if (key.isMasterKey() && sig.getKeyID() != key.getKeyID()) {
        continue;
      }
      PGPSignatureSubpacketVector hashed = sig.getHashedSubPackets();

      if (hashed != null && (hashed.getKeyFlags() & KeyFlags.SIGN_DATA) != 0) {
        return true;
      }

      PGPSignatureSubpacketVector unhashed = sig.getUnhashedSubPackets();

      if (unhashed != null && (unhashed.getKeyFlags() & KeyFlags.SIGN_DATA) != 0) {
        return true;
      }
    }

    return false;
  }
  @SuppressWarnings("unchecked")
  public static PGPPublicKey getMasterKey(PGPPublicKeyRing keyRing) {
    if (keyRing == null) {
      return null;
    }
    for (PGPPublicKey key : new IterableIterator<PGPPublicKey>(keyRing.getPublicKeys())) {
      if (key.isMasterKey()) {
        return key;
      }
    }

    return null;
  }
 public static Vector<PGPPublicKey> getUsableEncryptKeys(PGPPublicKeyRing keyRing) {
   Vector<PGPPublicKey> usableKeys = new Vector<PGPPublicKey>();
   Vector<PGPPublicKey> encryptKeys = getEncryptKeys(keyRing);
   PGPPublicKey masterKey = null;
   for (int i = 0; i < encryptKeys.size(); ++i) {
     PGPPublicKey key = encryptKeys.get(i);
     if (!isExpired(key) && !key.isRevoked()) {
       if (key.isMasterKey()) {
         masterKey = key;
       } else {
         usableKeys.add(key);
       }
     }
   }
   if (masterKey != null) {
     usableKeys.add(masterKey);
   }
   return usableKeys;
 }
  @SuppressWarnings("unchecked")
  public static boolean isEncryptionKey(PGPPublicKey key) {
    if (!key.isEncryptionKey()) {
      return false;
    }

    if (key.getVersion() <= 3) {
      // this must be true now
      return key.isEncryptionKey();
    }

    // special cases
    if (key.getAlgorithm() == PGPPublicKey.ELGAMAL_ENCRYPT) {
      return true;
    }

    if (key.getAlgorithm() == PGPPublicKey.RSA_ENCRYPT) {
      return true;
    }

    for (PGPSignature sig : new IterableIterator<PGPSignature>(key.getSignatures())) {
      if (key.isMasterKey() && sig.getKeyID() != key.getKeyID()) {
        continue;
      }
      PGPSignatureSubpacketVector hashed = sig.getHashedSubPackets();

      if (hashed != null
          && (hashed.getKeyFlags() & (KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE)) != 0) {
        return true;
      }

      PGPSignatureSubpacketVector unhashed = sig.getUnhashedSubPackets();

      if (unhashed != null
          && (unhashed.getKeyFlags() & (KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE)) != 0) {
        return true;
      }
    }
    return false;
  }