Package org.craftercms.commons.crypto
Class PGPUtils
java.lang.Object
org.craftercms.commons.crypto.PGPUtils
Utility class to perform encryption and decryption using PGP keys.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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
getPublicKey
(InputStream content) Extracts the PGP public key from an encoded stream.
-
Field Details
-
ALGORITHM
- See Also:
-
PROVIDER
- See Also:
-
-
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 keysidentity
- name used for the keyspassword
- passphrase used for the private keyprivateKeyStream
- stream to receive the encoded private keypublicKeyStream
- stream to receive the encoded public key- Throws:
NoSuchProviderException
- if there is an error with the security providerNoSuchAlgorithmException
- is there is an error with the security providerorg.bouncycastle.openpgp.PGPException
- if there is an error creating the keysIOException
- if there is an error writing to the streamsException
-
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 streamorg.bouncycastle.openpgp.PGPException
- if the public key cannot be extractedException
-
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 encryptedpublicKeyStream
- stream providing the encoded public keytargetStream
- stream to receive the encrypted data- Throws:
IOException
- if there is an error reading or writing from the streamsorg.bouncycastle.openpgp.PGPException
- if the encryption process failsException
-
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 datatargetStream
- stream to receive the decrypted dataprivateKeyStream
- stream providing the encoded PGP private keypassword
- passphrase for the private key- Throws:
IOException
- if there is an error reading or writing from the streamsorg.bouncycastle.openpgp.PGPException
- if the decryption process failsException
-
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 keykeyId
- id of the secret key to extractpassword
- passphrase for the secret key- Returns:
- the private key object
- Throws:
IOException
- if there is an error reading from the streamorg.bouncycastle.openpgp.PGPException
- if the secret key cannot be extractedException
-
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 decryptdata
- encrypted datacalculator
- instance ofBcKeyFingerprintCalculator
targetStream
- stream to receive the decrypted data- Throws:
org.bouncycastle.openpgp.PGPException
- if the decryption process failsIOException
- if the stream write operation fails
-