public class Encryptor extends Object implements DataProcessor
Modifier and Type | Field and Description |
---|---|
static int |
ENCRYPT_WITHOUT_COMPRESSION |
protected boolean |
isDecryptor |
Constructor and Description |
---|
Encryptor(byte[] key) |
Encryptor(String key) |
Modifier and Type | Method and Description |
---|---|
void |
closeProcess()
This aborts any on-going processing and frees resources associated with the processor.
|
byte[] |
encrypt(byte[] plaintext,
int offset,
int length)
Encrypt a "salted" version of the provided plaintext data
without compressing the data before encryption and without signing the salted data.
|
ByteArray |
encrypt(ByteArray dest,
byte[] plaintext,
int offset,
int length)
Encrypt a "salted" version of the provided plaintext data
without compressing the data before encryption and optionally signing the salted data.
|
ByteArray |
encrypt(ByteArray dest,
byte[] plaintext,
int offset,
int length,
int options,
Signature signature)
Encrypt a "salted" version of the provided plaintext data
optionally compressing the data before encryption and optionally signing the salted/compressed
data.
|
static ByteArray |
encrypt(DataProcessor processor,
ByteArray dest,
byte[] plaintext,
int offset,
int length,
int options,
Signature signature)
Using a particular DataProcessor, encrypt a "salted" version of the provided plaintext data
optionally compressing the data before encryption and optionally signing the salted/compressed
data.
|
int |
getBlockSize()
This returns the size of a block to be processed - any data blocks
presented for processing must be multiples of this size.
|
int |
getMaxBlockSize()
This returns the largest block that can be processed.
|
static ByteArray |
makeEncryptorTest(DataProcessor encryptor,
int minSize,
ByteArray out)
Produce a random block of bytes that may be used to later test a decryptor.
|
ByteArray |
processBlock(byte[] source,
int offset,
int length,
boolean last,
ByteArray dest)
This processes a block of data and places the output in the provided ByteArray.
|
protected void |
reset() |
static byte[] |
stringToKey(String sPassword) |
static boolean |
testDecryptor(DataProcessor decryptor,
byte[] src,
int offset,
int length)
Test a decryptor to see if it is able to decrypt data encrypted by an encryptor that
generated the source test block of data.
|
protected boolean isDecryptor
public static final int ENCRYPT_WITHOUT_COMPRESSION
public Encryptor(String key)
public Encryptor(byte[] key)
public static byte[] stringToKey(String sPassword)
protected void reset()
public int getBlockSize()
DataProcessor
getBlockSize
in interface DataProcessor
public int getMaxBlockSize()
DataProcessor
getMaxBlockSize
in interface DataProcessor
public void closeProcess()
DataProcessor
closeProcess
in interface DataProcessor
public ByteArray processBlock(byte[] source, int offset, int length, boolean last, ByteArray dest)
DataProcessor
isLastBlock should be set true if this is the last set of data to be processed. If you want to end a processing run, but have no more data to provide - you can set length to be zero (you can then also set inputData to be null) and set isLastBlock true.
If isLastBlock is true the DataProcessor should accept any input data, complete processing and output ALL of any remaining processed data. It should then reset itself so that the next call of processBlock is considered to be the start of a new sequence of data.
If there is an error processing the data, an IOException should be thrown.
processBlock
in interface DataProcessor
public static ByteArray makeEncryptorTest(DataProcessor encryptor, int minSize, ByteArray out) throws IOException
encryptor
- the encryptor to use.minSize
- a minimum number of bytes to use. By default it is 8.out
- an optional output ByteArray.IOException
- if there is an error processing the data.public static boolean testDecryptor(DataProcessor decryptor, byte[] src, int offset, int length)
Note that if a decryptor fails this test then it is defintiely unable to decrypt the encryptor's data, but a return of true indicates only that it is highly likely that it will.
decryptor
- the decryptor.src
- the data bytes as produced by makeEncryptorTest().offset
- the offset of the data bytes.length
- the number of data bytes.public byte[] encrypt(byte[] plaintext, int offset, int length) throws IOException
The data is first salted to add some randomness to it. The data is the processed using this Encryptor object and the resulting encrypted data is returned in the provided destination ByteArray (or a new one if it is null).
plaintext
- The data to be encrypted.offset
- The offset of the data in the array.length
- The number of bytes to be encrypted.IOException
- if an error occurs processing the data.public ByteArray encrypt(ByteArray dest, byte[] plaintext, int offset, int length) throws IOException
The data is first salted to add some randomness to it. If the signature parameter is not null the salted version is then signed. The salted version is signed instead of the plaintext version to reduce the chance of the source data being guessed from the signature, based on past signatures.
The data is the processed using this Encryptor object and the resulting encrypted data is returned in the provided destination ByteArray (or a new one if it is null).
dest
- The destination ByteArray or null to get a new one.plaintext
- The data to be encrypted.offset
- The offset of the data in the array.length
- The number of bytes to be encrypted.IOException
- if an error occurs processing the data.public ByteArray encrypt(ByteArray dest, byte[] plaintext, int offset, int length, int options, Signature signature) throws IOException
The data is first compressed if ENCRYPT_WITHOUT_COMPRESSION is not specified as an option. The data (which may now be compressed) is then salted to add some randomness to it. If the signature parameter is not null the salted version is then signed. The salted version is signed instead of the plaintext version to reduce the chance of the source data being guessed from the signature, based on past signatures.
The data is then processed using this Encryptor object and the resulting encrypted data is returned in the provided destination ByteArray (or a new one if it is null).
dest
- The destination ByteArray or null to get a new one.plaintext
- The data to be encrypted.offset
- The offset of the data in the array.length
- The number of bytes to be encrypted.options
- This can be ENCRYPT_WITHOUT_COMPRESSION or 0.signature
- This should be a Signature() object with a valid private key. After this method
returns, the signature bytes of this object will hold the correct signature for the data.IOException
- if an error occurs processing the data.public static ByteArray encrypt(DataProcessor processor, ByteArray dest, byte[] plaintext, int offset, int length, int options, Signature signature) throws IOException
The data is first compressed if ENCRYPT_WITHOUT_COMPRESSION is not specified as an option. The data (which may now be compressed) is then salted to add some randomness to it. If the signature parameter is not null the salted version is then signed. The salted version is signed instead of the plaintext version to reduce the chance of the source data being guessed from the signature, based on past signatures.
The data is the processed using the provided DataProcessor (which is assumed to be some kind of encryption algorithm) and the resulting encrypted data is returned in the provided destination ByteArray (or a new one if it is null).
processor
- The DataProcessor to be used for encryption (e.g. an instance of an Encryption object).dest
- The destination ByteArray or null to get a new one.plaintext
- The data to be encrypted.offset
- The offset of the data in the array.length
- The number of bytes to be encrypted.options
- This can be ENCRYPT_WITHOUT_COMPRESSION or 0.signature
- This should be a Signature() object with a valid private key. After this method
returns, the signature bytes of this object will hold the correct signature for the data.IOException
- if an error occurs processing the data.