public class Decryptor
extends java.lang.Object
implements java.lang.Cloneable
Key
s supplied on this object's Ring
.
To turn off verification, setVerificationRequired(boolean)
to false.
To decrypt a message encrypted with a passphrase (instead of, or in addition
to, a public-key pair), use setSymmetricPassphrase(java.lang.String)
to supply
the passphrase.
Here's an example of Bob decrypting and verifying an encrypted file that was signed by Alice:
new Decryptor(
new Key(new File("path/to/my/keys/alice-pub.gpg")),
new Key(new File("path/to/my/keys/bob-sec.gpg"), "b0bru1z!")
).decrypt(
new File("path/to/ciphertext.txt.gpg"),
new File("path/back-to/plaintext.txt")
);
This is equivalent to the following `gpg` command (where Bob has a
`bob` secret key and an `alice` public key on his keyring, and enters
"b0bru1z!" when prompted for his passphrase):
gpg --decrypt --output path/back-to/plaintext.txt path/to/ciphertext.txt.gpg
Modifier and Type | Class and Description |
---|---|
static class |
Decryptor.VerificationType
Type of signature verification done by decryptor.
|
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_COPY_FILE_BUFFER_SIZE |
static boolean |
DEFAULT_LOGGING_ENABLED |
static int |
DEFAULT_MAX_FILE_BUFFER_SIZE |
static boolean |
DEFAULT_VERIFICATION_REQUIRED |
static Decryptor.VerificationType |
DEFAULT_VERIFICATION_TYPE |
Constructor and Description |
---|
Decryptor()
Constructs a decryptor with an empty key ring.
|
Decryptor(Key... keys)
Constructs a decryptor with the specified keys.
|
Decryptor(Ring ring)
Constructs a decryptor with the specified key ring.
|
Modifier and Type | Method and Description |
---|---|
void |
clearSecrets()
Zeroes-out the cached passphrase for all keys,
and releases the extracted private key material for garbage collection.
|
Decryptor |
clone() |
FileMetadata |
decrypt(java.io.File ciphertext,
java.io.File plaintext)
Decrypts the first specified file to the output location specified
by the second file, and (if
isVerificationRequired() )
verifies its signatures. |
FileMetadata |
decrypt(java.io.InputStream ciphertext,
java.io.OutputStream plaintext)
Decrypts the specified PGP message into the specified output stream,
and (if
isVerificationRequired() ) verifies the message
signatures. |
FileMetadata |
decrypt(java.nio.file.Path ciphertext,
java.nio.file.Path plaintext)
Decrypts the first specified file to the output location specified
by the second file, and (if
isVerificationRequired() )
verifies its signatures. |
DecryptionResult |
decryptWithFullDetails(java.io.InputStream ciphertext,
java.io.OutputStream plaintext)
Decrypts the specified PGP message into the specified output stream,
including the armored headers (if stream was armored and contained
any such headers).
|
byte[] |
getCopyBuffer()
Internal buffer for copying decrypted plaintext into the output stream.
|
int |
getCopyFileBufferSize() |
int |
getMaxFileBufferSize() |
Ring |
getRing() |
java.lang.String |
getSymmetricPassphrase() |
char[] |
getSymmetricPassphraseChars() |
Decryptor.VerificationType |
getVerificationType()
Type of signature verification.
|
boolean |
isLoggingEnabled() |
boolean |
isVerificationRequired() |
void |
setCopyFileBufferSize(int copyFileBufferSize) |
void |
setLoggingEnabled(boolean enabled) |
void |
setMaxFileBufferSize(int maxFileBufferSize)
Decryptor will choose the most appropriate read/write buffer size
for each file.
|
void |
setRing(Ring x) |
void |
setSymmetricPassphrase(java.lang.String x) |
void |
setSymmetricPassphraseChars(char[] x) |
void |
setVerificationRequired(boolean x) |
void |
setVerificationType(Decryptor.VerificationType x)
Type of signature verification.
|
Decryptor |
withCopyFileBufferSize(int copyFileBufferSize) |
Decryptor |
withLoggingEnabled(boolean enabled) |
Decryptor |
withMaxFileBufferSize(int maxFileBufferSize) |
Decryptor |
withRing(Ring x) |
Decryptor |
withSymmetricPassphrase(java.lang.String x) |
Decryptor |
withSymmetricPassphraseChars(char[] x) |
Decryptor |
withVerificationRequired(boolean x) |
Decryptor |
withVerificationType(Decryptor.VerificationType x)
Type of signature verification.
|
java.io.InputStream |
wrapSourceInputStream(java.io.InputStream sourceStream,
long inputSize) |
java.io.OutputStream |
wrapTargetOutputStream(java.io.OutputStream targetStream,
long inputSize) |
public static final int DEFAULT_MAX_FILE_BUFFER_SIZE
public static final Decryptor.VerificationType DEFAULT_VERIFICATION_TYPE
public static final boolean DEFAULT_VERIFICATION_REQUIRED
public static final int DEFAULT_COPY_FILE_BUFFER_SIZE
public static final boolean DEFAULT_LOGGING_ENABLED
public Decryptor()
public Decryptor(Ring ring)
public Decryptor(Key... keys)
public Decryptor.VerificationType getVerificationType()
Decryptor.VerificationType.Required
.public void setVerificationType(Decryptor.VerificationType x)
public Decryptor withVerificationType(Decryptor.VerificationType x)
public boolean isVerificationRequired()
true
to require messages be signed with
at least one key from ring. Defaults to true.public void setVerificationRequired(boolean x)
x
- true
to require messages be signed with at least
one key from ring. Defaults to true
.DEFAULT_VERIFICATION_REQUIRED
public Decryptor withVerificationRequired(boolean x)
setVerificationRequired(boolean)
public char[] getSymmetricPassphraseChars()
clearSecrets()
is called (or
setSymmetricPassphraseChars(char[])
is called again with a different
passphrase, and then the char[] will be zeroed.public void setSymmetricPassphraseChars(char[] x)
x
- Passphrase to use to decrypt with a symmetric key; or empty char[].
Note that this char[] itself (and not a copy) will be cached and used
until clearSecrets()
is called (or
setSymmetricPassphraseChars(char[])
is called again with a different
passphrase, and then the char[] will be zeroed.public Decryptor withSymmetricPassphraseChars(char[] x)
setSymmetricPassphraseChars(char[])
public java.lang.String getSymmetricPassphrase()
getSymmetricPassphraseChars()
to avoid creating extra copies
of the passphrase in memory that cannot be cleaned up.getSymmetricPassphraseChars()
public void setSymmetricPassphrase(java.lang.String x)
x
- Passphrase to use to decrypt with a symmetric key; or empty string.
Prefer setSymmetricPassphraseChars(char[])
to avoid creating extra copies
of the passphrase in memory that cannot be cleaned up.setSymmetricPassphraseChars(char[])
public Decryptor withSymmetricPassphrase(java.lang.String x)
setSymmetricPassphrase(String)
public int getMaxFileBufferSize()
public void setMaxFileBufferSize(int maxFileBufferSize)
maxFileBufferSize
- The read/write buffer sizeDEFAULT_MAX_FILE_BUFFER_SIZE
public Decryptor withMaxFileBufferSize(int maxFileBufferSize)
setMaxFileBufferSize(int)
public int getCopyFileBufferSize()
DEFAULT_COPY_FILE_BUFFER_SIZE
,
getCopyBuffer()
public void setCopyFileBufferSize(int copyFileBufferSize)
copyFileBufferSize
- Internal buffer size used to copy data from
input ciphertext stream to output plaintext stream internallyDEFAULT_COPY_FILE_BUFFER_SIZE
,
getCopyBuffer()
public Decryptor withCopyFileBufferSize(int copyFileBufferSize)
setCopyFileBufferSize(int)
public void setRing(Ring x)
x
- Keys Ring
to use for decryption and verification.public Decryptor withRing(Ring x)
setRing(Ring)
public boolean isLoggingEnabled()
true
if logging a brief summary of the execution
every time decryption is executed (e.g. file name/path, size, compression
type, etc.). Note: errors/warnings logging are not affected by
this settingpublic void setLoggingEnabled(boolean enabled)
enabled
- true
if should log a brief summary of the execution
every time decryption is executed (e.g. file name/path, size, compression
type, etc.). Note: errors/warnings logging are not affected by
this settingpublic Decryptor withLoggingEnabled(boolean enabled)
setLoggingEnabled(boolean)
public void clearSecrets()
public FileMetadata decrypt(java.io.File ciphertext, java.io.File plaintext) throws java.io.IOException, org.bouncycastle.openpgp.PGPException
isVerificationRequired()
)
verifies its signatures. If a file already exists in the output file's
location, it will be deleted. If an exception occurs during decryption,
the output file will be deleted.ciphertext
- File
containing a PGP message, in binary or
ASCII Armor format.plaintext
- File
into which to decrypt the message.Metadata
of original file, and the list of keys that signed
the message with a verified signature. The original file metadata
values are optional, and may be missing or incorrect.java.io.IOException
- if an IO error occurs reading from or writing to
the underlying input or output streams.org.bouncycastle.openpgp.PGPException
- if the PGP message is not formatted correctly.PassphraseException
- if an incorrect passphrase was supplied
for one of the decryption keys, or as the
getSymmetricPassphrase()
.DecryptionException
- if the message was not encrypted for any
of the keys supplied for decryption.VerificationException
- if isVerificationRequired()
and
the message was not signed by any of the keys supplied for verification.public FileMetadata decrypt(java.nio.file.Path ciphertext, java.nio.file.Path plaintext) throws java.io.IOException, org.bouncycastle.openpgp.PGPException
isVerificationRequired()
)
verifies its signatures. If a file already exists in the output file's
location, it will be deleted. If an exception occurs during decryption,
the output file will be deleted.ciphertext
- Path
to file containing a PGP message, in binary or
ASCII Armor format.plaintext
- Path
of the file into which to decrypt the message.Metadata
of original file, and the list of keys that signed
the message with a verified signature. The original file metadata
values are optional, and may be missing or incorrect.java.io.IOException
- if an IO error occurs reading from or writing to
the underlying input or output streams.org.bouncycastle.openpgp.PGPException
- if the PGP message is not formatted correctly.PassphraseException
- if an incorrect passphrase was supplied
for one of the decryption keys, or as the
getSymmetricPassphrase()
.DecryptionException
- if the message was not encrypted for any
of the keys supplied for decryption.VerificationException
- if isVerificationRequired()
and
the message was not signed by any of the keys supplied for verification.public java.io.InputStream wrapSourceInputStream(java.io.InputStream sourceStream, long inputSize) throws java.io.IOException
sourceStream
- Original source (ciphertext) InputStream
inputSize
- Expected input (ciphertext) sizejava.io.IOException
- If failed to generate the wrapperpublic java.io.OutputStream wrapTargetOutputStream(java.io.OutputStream targetStream, long inputSize) throws java.io.IOException
targetStream
- Original target (plaintext) OutputStream
inputSize
- Expected input (ciphertext) sizejava.io.IOException
- If failed to generate the wrapperpublic FileMetadata decrypt(java.io.InputStream ciphertext, java.io.OutputStream plaintext) throws java.io.IOException, org.bouncycastle.openpgp.PGPException
isVerificationRequired()
) verifies the message
signatures. Does not close or flush the streams.
Note that the full decrypted content will be written to the output stream before the message is verified, so you may want to buffer the content and not write it to its final destination until this method returns.
ciphertext
- PGP message, in binary or ASCII Armor format.plaintext
- Decrypted content.java.io.IOException
- if an IO error occurs reading from or writing to
the underlying input or output streams.org.bouncycastle.openpgp.PGPException
- if the PGP message is not formatted correctly.PassphraseException
- if an incorrect passphrase was supplied
for one of the decryption keys, or as the
getSymmetricPassphrase()
.DecryptionException
- if the message was not encrypted for any
of the keys supplied for decryption.VerificationException
- if isVerificationRequired()
and
the message was not signed by any of the keys supplied for verification.decryptWithFullDetails
public DecryptionResult decryptWithFullDetails(java.io.InputStream ciphertext, java.io.OutputStream plaintext) throws java.io.IOException, org.bouncycastle.openpgp.PGPException
isVerificationRequired()
also verifies
the message signatures. Note: does not close or flush the streams.ciphertext
- PGP message, in binary or ASCII Armor format.plaintext
- Decrypted content target OutputStream
DecryptionResult
containing all relevant
information that could be extracted from the encrypted data - including
metadata, armored headers (if any), etc...java.io.IOException
- if an IO error occurs reading from or writing to
the underlying input or output streams.org.bouncycastle.openpgp.PGPException
- if the PGP message is not formatted correctly.PassphraseException
- if an incorrect passphrase was supplied
for one of the decryption keys, or as the
getSymmetricPassphrase()
.DecryptionException
- if the message was not encrypted for any
of the keys supplied for decryption.VerificationException
- if isVerificationRequired()
and
the message was not signed by any of the keys supplied for verification.public byte[] getCopyBuffer()
public Decryptor clone()
clone
in class java.lang.Object