public PrivateKey getPrivateKey(PGPPrivateKey privKey) throws PGPException {
    PublicKeyPacket pubPk = privKey.getPublicKeyPacket();
    BCPGKey privPk = privKey.getPrivateKeyDataPacket();

    try {
      KeyFactory fact;

      switch (pubPk.getAlgorithm()) {
        case PGPPublicKey.RSA_ENCRYPT:
        case PGPPublicKey.RSA_GENERAL:
        case PGPPublicKey.RSA_SIGN:
          RSAPublicBCPGKey rsaPub = (RSAPublicBCPGKey) pubPk.getKey();
          RSASecretBCPGKey rsaPriv = (RSASecretBCPGKey) privPk;
          RSAPrivateCrtKeySpec rsaPrivSpec =
              new RSAPrivateCrtKeySpec(
                  rsaPriv.getModulus(),
                  rsaPub.getPublicExponent(),
                  rsaPriv.getPrivateExponent(),
                  rsaPriv.getPrimeP(),
                  rsaPriv.getPrimeQ(),
                  rsaPriv.getPrimeExponentP(),
                  rsaPriv.getPrimeExponentQ(),
                  rsaPriv.getCrtCoefficient());

          fact = helper.createKeyFactory("RSA");

          return fact.generatePrivate(rsaPrivSpec);
        case PGPPublicKey.DSA:
          DSAPublicBCPGKey dsaPub = (DSAPublicBCPGKey) pubPk.getKey();
          DSASecretBCPGKey dsaPriv = (DSASecretBCPGKey) privPk;
          DSAPrivateKeySpec dsaPrivSpec =
              new DSAPrivateKeySpec(dsaPriv.getX(), dsaPub.getP(), dsaPub.getQ(), dsaPub.getG());

          fact = helper.createKeyFactory("DSA");

          return fact.generatePrivate(dsaPrivSpec);
        case PGPPublicKey.ELGAMAL_ENCRYPT:
        case PGPPublicKey.ELGAMAL_GENERAL:
          ElGamalPublicBCPGKey elPub = (ElGamalPublicBCPGKey) pubPk.getKey();
          ElGamalSecretBCPGKey elPriv = (ElGamalSecretBCPGKey) privPk;
          ElGamalPrivateKeySpec elSpec =
              new ElGamalPrivateKeySpec(
                  elPriv.getX(), new ElGamalParameterSpec(elPub.getP(), elPub.getG()));

          fact = helper.createKeyFactory("ElGamal");

          return fact.generatePrivate(elSpec);
        default:
          throw new PGPException("unknown public key algorithm encountered");
      }
    } catch (PGPException e) {
      throw e;
    } catch (Exception e) {
      throw new PGPException("Exception constructing key", e);
    }
  }
  public PublicKey getPublicKey(PGPPublicKey publicKey) throws PGPException {
    KeyFactory fact;

    PublicKeyPacket publicPk = publicKey.getPublicKeyPacket();

    try {
      switch (publicPk.getAlgorithm()) {
        case PublicKeyAlgorithmTags.RSA_ENCRYPT:
        case PublicKeyAlgorithmTags.RSA_GENERAL:
        case PublicKeyAlgorithmTags.RSA_SIGN:
          RSAPublicBCPGKey rsaK = (RSAPublicBCPGKey) publicPk.getKey();
          RSAPublicKeySpec rsaSpec =
              new RSAPublicKeySpec(rsaK.getModulus(), rsaK.getPublicExponent());

          fact = helper.createKeyFactory("RSA");

          return fact.generatePublic(rsaSpec);
        case PublicKeyAlgorithmTags.DSA:
          DSAPublicBCPGKey dsaK = (DSAPublicBCPGKey) publicPk.getKey();
          DSAPublicKeySpec dsaSpec =
              new DSAPublicKeySpec(dsaK.getY(), dsaK.getP(), dsaK.getQ(), dsaK.getG());

          fact = helper.createKeyFactory("DSA");

          return fact.generatePublic(dsaSpec);
        case PublicKeyAlgorithmTags.ELGAMAL_ENCRYPT:
        case PublicKeyAlgorithmTags.ELGAMAL_GENERAL:
          ElGamalPublicBCPGKey elK = (ElGamalPublicBCPGKey) publicPk.getKey();
          ElGamalPublicKeySpec elSpec =
              new ElGamalPublicKeySpec(
                  elK.getY(), new ElGamalParameterSpec(elK.getP(), elK.getG()));

          fact = helper.createKeyFactory("ElGamal");

          return fact.generatePublic(elSpec);
        default:
          throw new PGPException("unknown public key algorithm encountered");
      }
    } catch (PGPException e) {
      throw e;
    } catch (Exception e) {
      throw new PGPException("exception constructing public key", e);
    }
  }
Beispiel #3
0
  /**
   * Extract a PGPPrivate key from the SecretKey's encrypted contents.
   *
   * @param decryptorFactory factory to use to generate a decryptor for the passed in secretKey.
   * @return PGPPrivateKey the unencrypted private key.
   * @throws PGPException on failure.
   */
  public PGPPrivateKey extractPrivateKey(PBESecretKeyDecryptor decryptorFactory)
      throws PGPException {
    if (isPrivateKeyEmpty()) {
      return null;
    }

    PublicKeyPacket pubPk = secret.getPublicKeyPacket();

    try {
      byte[] data = extractKeyData(decryptorFactory);
      BCPGInputStream in = new BCPGInputStream(new ByteArrayInputStream(data));

      switch (pubPk.getAlgorithm()) {
        case PGPPublicKey.RSA_ENCRYPT:
        case PGPPublicKey.RSA_GENERAL:
        case PGPPublicKey.RSA_SIGN:
          RSASecretBCPGKey rsaPriv = new RSASecretBCPGKey(in);

          return new PGPPrivateKey(this.getKeyID(), pubPk, rsaPriv);
        case PGPPublicKey.DSA:
          DSASecretBCPGKey dsaPriv = new DSASecretBCPGKey(in);

          return new PGPPrivateKey(this.getKeyID(), pubPk, dsaPriv);
        case PGPPublicKey.ELGAMAL_ENCRYPT:
        case PGPPublicKey.ELGAMAL_GENERAL:
          ElGamalSecretBCPGKey elPriv = new ElGamalSecretBCPGKey(in);

          return new PGPPrivateKey(this.getKeyID(), pubPk, elPriv);
        case PGPPublicKey.ECDH:
        case PGPPublicKey.ECDSA:
          ECSecretBCPGKey ecPriv = new ECSecretBCPGKey(in);

          return new PGPPrivateKey(this.getKeyID(), pubPk, ecPriv);
        default:
          throw new PGPException("unknown public key algorithm encountered");
      }
    } catch (PGPException e) {
      throw e;
    } catch (Exception e) {
      throw new PGPException("Exception constructing key", e);
    }
  }