Class PGPUtils

java.lang.Object
org.craftercms.commons.crypto.PGPUtils

public abstract class PGPUtils extends Object
Utility class to perform encryption and decryption using PGP keys.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
    static final String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    createKeyPair(int length, String identity, char[] password, OutputStream privateKeyStream, OutputStream publicKeyStream)
    Creates a private/public PGP key pair.
    static void
    decrypt(InputStream encryptedStream, OutputStream targetStream, InputStream privateKeyStream, char[] password)
    Performs decryption of a given stream using a PGP private key.
    protected static void
    decryptData(org.bouncycastle.openpgp.PGPPrivateKey privateKey, org.bouncycastle.openpgp.PGPPublicKeyEncryptedData data, org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator calculator, OutputStream targetStream)
    Performs the decryption of the given data.
    static void
    encrypt(Path path, InputStream publicKeyStream, OutputStream targetStream)
    Performs encryption on a single file using a PGP public key.
    protected static org.bouncycastle.openpgp.PGPPrivateKey
    findSecretKey(InputStream keyStream, long keyId, char[] password)
    Extracts the PGP private key from an encoded stream.
    static org.bouncycastle.openpgp.PGPPublicKey
    Extracts the PGP public key from an encoded stream.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • PGPUtils

      public PGPUtils()
  • Method Details

    • createKeyPair

      public static void createKeyPair(int length, String identity, char[] password, OutputStream privateKeyStream, OutputStream publicKeyStream) throws Exception
      Creates a private/public PGP key pair.
      Parameters:
      length - length in bytes for the keys
      identity - name used for the keys
      password - passphrase used for the private key
      privateKeyStream - stream to receive the encoded private key
      publicKeyStream - stream to receive the encoded public key
      Throws:
      NoSuchProviderException - if there is an error with the security provider
      NoSuchAlgorithmException - is there is an error with the security provider
      org.bouncycastle.openpgp.PGPException - if there is an error creating the keys
      IOException - if there is an error writing to the streams
      Exception
    • getPublicKey

      public static org.bouncycastle.openpgp.PGPPublicKey getPublicKey(InputStream content) throws Exception
      Extracts the PGP public key from an encoded stream.
      Parameters:
      content - stream to extract the key
      Returns:
      key object
      Throws:
      IOException - if there is an error reading the stream
      org.bouncycastle.openpgp.PGPException - if the public key cannot be extracted
      Exception
    • encrypt

      public static void encrypt(Path path, InputStream publicKeyStream, OutputStream targetStream) throws Exception
      Performs encryption on a single file using a PGP public key.
      Parameters:
      path - file to be encrypted
      publicKeyStream - stream providing the encoded public key
      targetStream - stream to receive the encrypted data
      Throws:
      IOException - if there is an error reading or writing from the streams
      org.bouncycastle.openpgp.PGPException - if the encryption process fails
      Exception
    • decrypt

      public static void decrypt(InputStream encryptedStream, OutputStream targetStream, InputStream privateKeyStream, char[] password) throws Exception
      Performs decryption of a given stream using a PGP private key.
      Parameters:
      encryptedStream - stream providing the encrypted data
      targetStream - stream to receive the decrypted data
      privateKeyStream - stream providing the encoded PGP private key
      password - passphrase for the private key
      Throws:
      IOException - if there is an error reading or writing from the streams
      org.bouncycastle.openpgp.PGPException - if the decryption process fails
      Exception
    • findSecretKey

      protected static org.bouncycastle.openpgp.PGPPrivateKey findSecretKey(InputStream keyStream, long keyId, char[] password) throws Exception
      Extracts the PGP private key from an encoded stream.
      Parameters:
      keyStream - stream providing the encoded private key
      keyId - id of the secret key to extract
      password - passphrase for the secret key
      Returns:
      the private key object
      Throws:
      IOException - if there is an error reading from the stream
      org.bouncycastle.openpgp.PGPException - if the secret key cannot be extracted
      Exception
    • decryptData

      protected static void decryptData(org.bouncycastle.openpgp.PGPPrivateKey privateKey, org.bouncycastle.openpgp.PGPPublicKeyEncryptedData data, org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator calculator, OutputStream targetStream) throws org.bouncycastle.openpgp.PGPException, IOException
      Performs the decryption of the given data.
      Parameters:
      privateKey - PGP Private Key to decrypt
      data - encrypted data
      calculator - instance of BcKeyFingerprintCalculator
      targetStream - stream to receive the decrypted data
      Throws:
      org.bouncycastle.openpgp.PGPException - if the decryption process fails
      IOException - if the stream write operation fails