Top Description Inners Fields Constructors Methods
org.bouncycastle.openpgp

public Class PGPEncryptedDataGenerator

extends Object
implements SymmetricKeyAlgorithmTags, StreamGenerator
Class Inheritance
All Implemented Interfaces
org.bouncycastle.openpgp.StreamGenerator, org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags
Imports
java.io.IOException, .OutputStream, java.security.SecureRandom, java.util.ArrayList, .List, org.bouncycastle.bcpg.BCPGOutputStream, .HashAlgorithmTags, .PacketTags, .SymmetricKeyAlgorithmTags, org.bouncycastle.openpgp.operator.PBEKeyEncryptionMethodGenerator, .PGPAEADDataEncryptor, .PGPDataEncryptor, .PGPDataEncryptorBuilder, .PGPDigestCalculator, .PGPKeyEncryptionMethodGenerator, org.bouncycastle.util.io.TeeOutputStream

Generator for encrypted objects.

A PGPEncryptedDataGenerator is used by configuring one or more encryption methods, and then invoking one of the open functions to create an OutputStream that raw data can be supplied to for encryption:

Raw data is not typically written directly to the OutputStream obtained from a PGPEncryptedDataGenerator. The OutputStream is usually wrapped by a PGPLiteralDataGenerator, and often with a PGPCompressedDataGenerator between.

Once plaintext data for encryption has been written to the constructed OutputStream, writing of the encrypted object stream is completed by closing the OutputStream obtained from the open() method, or equivalently invoking close() on this generator.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private OutputStream
private PGPDataEncryptorBuilder
private int
private PGPDigestCalculator
private OutputStream
private List<E>
private boolean
private BCPGOutputStream
private SecureRandom
public static final int
S2K_SHA1

Specifier for SHA-1 S2K PBE generator.

public static final int
S2K_SHA224

Specifier for SHA-224 S2K PBE generator.

public static final int
S2K_SHA256

Specifier for SHA-256 S2K PBE generator.

public static final int
S2K_SHA384

Specifier for SHA-384 S2K PBE generator.

public static final int
S2K_SHA512

Specifier for SHA-512 S2K PBE generator.

Constructor Summary

AccessConstructor and Description
public
PGPEncryptedDataGenerator(PGPDataEncryptorBuilder
builder to create actual data encryptor.
encryptorBuilder
)

Base constructor.

public
PGPEncryptedDataGenerator(PGPDataEncryptorBuilder
builder to create actual data encryptor.
encryptorBuilder
,
boolean
PGP 2.6.x compatibility required.
oldFormat
)

Base constructor with the option to turn on formatting for PGP 2.6.x compatibility.

Method Summary

Modifier and TypeMethod and Description
private void
addCheckSum(byte[] sessionInfo)

public void
addMethod(PGPKeyEncryptionMethodGenerator
key encryption method to use.
method
)

Add a key encryption method to be used to encrypt the session data associated with this encrypted data.

public void
close()

Implements org.bouncycastle.openpgp.StreamGenerator.close.

Close off the encrypted object - this is equivalent to calling close on the stream returned by the open() methods.
private byte[]
createSessionInfo(int algorithm, byte[] keyBytes)

private OutputStream

Returns:

the generator's output stream.
open
(OutputStream
the stream to write encrypted packets to.
out
,
long
the length of the data to be encrypted. Ignored if buffer is non null.
length
,
byte[]
a buffer to use to buffer and write partial packets.
buffer
)

Create an OutputStream based on the configured methods.

public OutputStream

Returns:

the output stream to write data to for encryption.
open
(OutputStream
the stream to write encrypted packets to.
out
,
long
the length of the data to be encrypted.
length
)

Create an OutputStream based on the configured methods to write a single encrypted object of known length.

public OutputStream

Returns:

the output stream to write data to for encryption.
open
(OutputStream
the stream to write encrypted packets to.
out
,
byte[]
a buffer to use to buffer and write partial packets. The returned stream takes ownership of the buffer and will use it to buffer plaintext data for encryption.
buffer
)

Create an OutputStream which will encrypt the data as it is written to it.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cOutback to summary
private OutputStream cOut
dataEncryptorBuilderback to summary
private PGPDataEncryptorBuilder dataEncryptorBuilder
defAlgorithmback to summary
private int defAlgorithm
digestCalcback to summary
private PGPDigestCalculator digestCalc
genOutback to summary
private OutputStream genOut
methodsback to summary
private List<E> methods
oldFormatback to summary
private boolean oldFormat
pOutback to summary
private BCPGOutputStream pOut
randback to summary
private SecureRandom rand
S2K_SHA1back to summary
public static final int S2K_SHA1

Specifier for SHA-1 S2K PBE generator.

S2K_SHA224back to summary
public static final int S2K_SHA224

Specifier for SHA-224 S2K PBE generator.

S2K_SHA256back to summary
public static final int S2K_SHA256

Specifier for SHA-256 S2K PBE generator.

S2K_SHA384back to summary
public static final int S2K_SHA384

Specifier for SHA-384 S2K PBE generator.

S2K_SHA512back to summary
public static final int S2K_SHA512

Specifier for SHA-512 S2K PBE generator.

Constructor Detail

PGPEncryptedDataGeneratorback to summary
public PGPEncryptedDataGenerator(PGPDataEncryptorBuilder encryptorBuilder)

Base constructor.

Parameters
encryptorBuilder:PGPDataEncryptorBuilder

builder to create actual data encryptor.

PGPEncryptedDataGeneratorback to summary
public PGPEncryptedDataGenerator(PGPDataEncryptorBuilder encryptorBuilder, boolean oldFormat)

Base constructor with the option to turn on formatting for PGP 2.6.x compatibility.

Parameters
encryptorBuilder:PGPDataEncryptorBuilder

builder to create actual data encryptor.

oldFormat:boolean

PGP 2.6.x compatibility required.

Method Detail

addCheckSumback to summary
private void addCheckSum(byte[] sessionInfo)
addMethodback to summary
public void addMethod(PGPKeyEncryptionMethodGenerator method)

Add a key encryption method to be used to encrypt the session data associated with this encrypted data.

Parameters
method:PGPKeyEncryptionMethodGenerator

key encryption method to use.

closeback to summary
public void close() throws IOException

Implements org.bouncycastle.openpgp.StreamGenerator.close.

Close off the encrypted object - this is equivalent to calling close on the stream returned by the open() methods.

Note

This does not close the underlying output stream, only the stream on top of it created by the open() method.

Exceptions
IOException:
if an error occurs writing trailing information (such as integrity check information) to the underlying stream.
createSessionInfoback to summary
private byte[] createSessionInfo(int algorithm, byte[] keyBytes)
openback to summary
private OutputStream open(OutputStream out, long length, byte[] buffer) throws IOException, PGPException, IllegalStateException

Create an OutputStream based on the configured methods.

If the supplied buffer is non null the stream returned will write a sequence of partial packets, otherwise the length will be used to output a fixed length packet.

The stream created can be closed off by either calling close() on the stream or close() on the generator. Closing the returned stream does not close off the OutputStream parameter out.

Parameters
out:OutputStream

the stream to write encrypted packets to.

length:long

the length of the data to be encrypted. Ignored if buffer is non null.

buffer:byte[]

a buffer to use to buffer and write partial packets.

Returns:OutputStream

the generator's output stream.

Exceptions
IOException:
if an error occurs writing stream header information to the provider output stream.
PGPException:
if an error occurs initialising PGP encryption for the configured encryption methods.
IllegalStateException:
if this generator already has an open OutputStream, or no encryption methods are configured.
openback to summary
public OutputStream open(OutputStream out, long length) throws IOException, PGPException

Create an OutputStream based on the configured methods to write a single encrypted object of known length.

The stream created can be closed off by either calling close() on the stream or close() on the generator. Closing the returned stream does not close off the OutputStream parameter out.

Parameters
out:OutputStream

the stream to write encrypted packets to.

length:long

the length of the data to be encrypted.

Returns:OutputStream

the output stream to write data to for encryption.

Exceptions
IOException:
if an error occurs writing stream header information to the provider output stream.
PGPException:
if an error occurs initialising PGP encryption for the configured encryption methods.
IllegalStateException:
if this generator already has an open OutputStream, or no encryption methods are configured.
openback to summary
public OutputStream open(OutputStream out, byte[] buffer) throws IOException, PGPException

Create an OutputStream which will encrypt the data as it is written to it. The stream of encrypted data will be written out in chunks (partial packets) according to the size of the passed in buffer.

The stream created can be closed off by either calling close() on the stream or close() on the generator. Closing the returned stream does not close off the OutputStream parameter out.

Note

if the buffer is not a power of 2 in length only the largest power of 2 bytes worth of the buffer will be used.

Parameters
out:OutputStream

the stream to write encrypted packets to.

buffer:byte[]

a buffer to use to buffer and write partial packets. The returned stream takes ownership of the buffer and will use it to buffer plaintext data for encryption.

Returns:OutputStream

the output stream to write data to for encryption.

Exceptions
IOException:
if an error occurs writing stream header information to the provider output stream.
PGPException:
if an error occurs initialising PGP encryption for the configured encryption methods.
IllegalStateException:
if this generator already has an open OutputStream, or no encryption methods are configured.
org.bouncycastle.openpgp back to summary

private Class PGPEncryptedDataGenerator.ClosableBCPGOutputStream

extends BCPGOutputStream
Class Inheritance

Constructor Summary

AccessConstructor and Description
public
ClosableBCPGOutputStream(OutputStream out, int symmetricKeyEnc, byte[] buffer)

public
ClosableBCPGOutputStream(OutputStream out, int symmetricKeyEnc, long length, boolean oldFormat)

public
ClosableBCPGOutputStream(OutputStream out, int symEncIntegrityPro, long length)

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides org.bouncycastle.bcpg.BCPGOutputStream.close.

Implements java.io.Closeable.close.

Closes this output stream and releases any system resources associated with this stream.
Inherited from org.bouncycastle.bcpg.BCPGOutputStream:
finishflushwritewritewriteObjectwritePacket

Constructor Detail

ClosableBCPGOutputStreamback to summary
public ClosableBCPGOutputStream(OutputStream out, int symmetricKeyEnc, byte[] buffer) throws IOException
ClosableBCPGOutputStreamback to summary
public ClosableBCPGOutputStream(OutputStream out, int symmetricKeyEnc, long length, boolean oldFormat) throws IOException
ClosableBCPGOutputStreamback to summary
public ClosableBCPGOutputStream(OutputStream out, int symEncIntegrityPro, long length) throws IOException

Method Detail

closeback to summary
public void close() throws IOException

Overrides org.bouncycastle.bcpg.BCPGOutputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.OutputStream.close.

Closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.

Exceptions
IOException:
if an I/O error occurs.