/**
   * Replace the passed the public key on the passed in secret key.
   *
   * @param secretKey secret key to change
   * @param publicKey new public key.
   * @return a new secret key.
   * @throws IllegalArgumentException if keyIDs do not match.
   */
  public static PGPSecretKey replacePublicKey(PGPSecretKey secretKey, PGPPublicKey publicKey) {
    if (publicKey.getKeyID() != secretKey.getKeyID()) {
      throw new IllegalArgumentException("keyIDs do not match");
    }

    return new PGPSecretKey(secretKey.secret, publicKey);
  }
Exemple #2
0
  /**
   * Returns the secret key matching the specified identifier.
   *
   * @param input the input stream containing the keyring collection
   * @param keyId the 4 bytes identifier of the key
   */
  private static PGPSecretKey getSecretKey(InputStream input, String keyId)
      throws IOException, PGPException {
    PGPSecretKeyRingCollection keyrings =
        new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(input));

    Iterator rIt = keyrings.getKeyRings();

    while (rIt.hasNext()) {
      PGPSecretKeyRing kRing = (PGPSecretKeyRing) rIt.next();
      Iterator kIt = kRing.getSecretKeys();

      while (kIt.hasNext()) {
        PGPSecretKey key = (PGPSecretKey) kIt.next();

        if (key.isSigningKey()
            && Long.toHexString(key.getKeyID() & 0xFFFFFFFFL).equals(keyId.toLowerCase())) {
          return key;
        }
      }
    }

    return null;
  }