public class Encryptor
extends java.lang.Object
Encryptor class that greatly simplifies strong encryption and decryption.
Supports both encryption/decryption of single messages as well as streaming encryption.
Handling of initialization vectors (IVs) is done transparently. You can alter this behavior
by calling setPrependIV(boolean)
.
Constructor and Description |
---|
Encryptor(java.security.Key key)
Constructs an
Encryptor instance. |
Encryptor(java.security.spec.KeySpec keySpec,
javax.crypto.SecretKeyFactory secretKeyFactory)
Constructs an
Encryptor instance. |
Encryptor(java.security.spec.KeySpec keySpec,
javax.crypto.SecretKeyFactory secretKeyFactory,
java.lang.String algorithm,
int ivLength)
Constructs an
Encryptor instance. |
Encryptor(java.security.spec.KeySpec keySpec,
javax.crypto.SecretKeyFactory secretKeyFactory,
java.lang.String algorithm,
int ivLength,
int tLen)
Constructs an
Encryptor instance. |
Encryptor(java.security.Key key,
java.lang.String algorithm)
Constructs an
Encryptor instance. |
Encryptor(java.security.Key key,
java.lang.String algorithm,
int ivLength)
Constructs an
Encryptor instance. |
Encryptor(java.security.Key key,
java.lang.String algorithm,
int ivLength,
int tLen)
Constructs an
Encryptor instance. |
Modifier and Type | Method and Description |
---|---|
byte[] |
decrypt(byte[] message)
Decrypts a byte array and returns the decrypted message.
|
byte[] |
decrypt(byte[] message,
byte[] aad)
Decrypts a byte array and returns the decrypted message.
|
byte[] |
decrypt(byte[] message,
byte[] aad,
byte[] iv)
Decrypts a byte array and returns the decrypted message.
|
byte[] |
encrypt(byte[] message)
Encrypts a byte array and returns the encrypted message.
|
byte[] |
encrypt(byte[] message,
byte[] aad)
Encrypts a byte array and returns the encrypted message.
|
byte[] |
encrypt(byte[] message,
byte[] aad,
byte[] iv)
Encrypts a byte array and returns the encrypted message.
|
java.lang.String |
getAlgorithm()
Returns the algorithm.
|
javax.crypto.Cipher |
getCipher()
Returns the thread local cipher.
|
byte[] |
getIV()
Returns the last used thread-local initialization vector that was either passed as argument or generated during the last encryption operation.
|
java.security.Key |
getKey()
Returns the key.
|
void |
setAlgorithmProvider(java.lang.String algorithmProvider)
Sets the algorithm provider.
|
void |
setGenerateIV(boolean generateIV)
Sets whether the initialization vector should be generated by this
Encryptor instance. |
void |
setPrependIV(boolean prependIV)
Sets whether the initialization vector should be prepended to the encrypted output and can be shifted
off the start of during decryption.
|
javax.crypto.CipherInputStream |
wrapInputStream(java.io.InputStream is)
Wraps an
InputStream with a CipherInputStream using this encryptor's cipher. |
javax.crypto.CipherInputStream |
wrapInputStream(java.io.InputStream is,
byte[] iv)
Wraps an
InputStream with a CipherInputStream using this encryptor's cipher. |
javax.crypto.CipherOutputStream |
wrapOutputStream(java.io.OutputStream os)
Wraps an
OutputStream with a CipherOutputStream using this encryptor's cipher. |
javax.crypto.CipherOutputStream |
wrapOutputStream(java.io.OutputStream os,
byte[] iv)
Wraps an
OutputStream with a CipherOutputStream using this encryptor's cipher. |
public Encryptor(java.security.Key key)
Constructs an Encryptor
instance.
key
- public Encryptor(java.security.Key key, java.lang.String algorithm)
Constructs an Encryptor
instance.
key
- algorithm
- public Encryptor(java.security.Key key, java.lang.String algorithm, int ivLength)
Constructs an Encryptor
instance.
key
- algorithm
- ivLength
- public Encryptor(java.security.Key key, java.lang.String algorithm, int ivLength, int tLen)
Constructs an Encryptor
instance.
Use this constructor when using GCM block mode and specify the tLen
value.
key
- algorithm
- ivLength
- tLen
- public Encryptor(java.security.spec.KeySpec keySpec, javax.crypto.SecretKeyFactory secretKeyFactory)
Constructs an Encryptor
instance.
keySpec
- secretKeyFactory
- public Encryptor(java.security.spec.KeySpec keySpec, javax.crypto.SecretKeyFactory secretKeyFactory, java.lang.String algorithm, int ivLength)
Constructs an Encryptor
instance.
keySpec
- secretKeyFactory
- algorithm
- ivLength
- public Encryptor(java.security.spec.KeySpec keySpec, javax.crypto.SecretKeyFactory secretKeyFactory, java.lang.String algorithm, int ivLength, int tLen)
Constructs an Encryptor
instance.
Use this constructor when using GCM block mode and specify the tLen
value.
keySpec
- secretKeyFactory
- algorithm
- ivLength
- tLen
- public byte[] encrypt(byte[] message) throws java.security.GeneralSecurityException
Encrypts a byte array and returns the encrypted message.
message
- java.security.GeneralSecurityException
public byte[] encrypt(byte[] message, byte[] aad) throws java.security.GeneralSecurityException
Encrypts a byte array and returns the encrypted message.
message
- aad
- java.security.GeneralSecurityException
public byte[] encrypt(byte[] message, byte[] aad, byte[] iv) throws java.security.GeneralSecurityException
Encrypts a byte array and returns the encrypted message.
message
- aad
- iv
- java.security.GeneralSecurityException
public byte[] decrypt(byte[] message) throws java.security.GeneralSecurityException
Decrypts a byte array and returns the decrypted message.
message
- java.security.GeneralSecurityException
public byte[] decrypt(byte[] message, byte[] aad) throws java.security.GeneralSecurityException
Decrypts a byte array and returns the decrypted message.
message
- aad
- java.security.GeneralSecurityException
public byte[] decrypt(byte[] message, byte[] aad, byte[] iv) throws java.security.GeneralSecurityException
Decrypts a byte array and returns the decrypted message.
message
- aad
- iv
- java.security.GeneralSecurityException
public byte[] getIV()
Returns the last used thread-local initialization vector that was either passed as argument or generated during the last encryption operation.
public void setPrependIV(boolean prependIV)
Sets whether the initialization vector should be prepended to the encrypted output and can be shifted off the start of during decryption.
Defaults to true
when constructed with an explicit IV length or false
when constructed with prespecified IV.
Note: This setting also applies when in streaming mode using wrapInputStream(InputStream)
and wrapOutputStream(OutputStream)
.
prependIV
- public void setGenerateIV(boolean generateIV)
Sets whether the initialization vector should be generated by this Encryptor
instance.
generateIV
- public java.lang.String getAlgorithm()
Returns the algorithm.
public void setAlgorithmProvider(java.lang.String algorithmProvider)
Sets the algorithm provider.
algorithmProvider
- public java.security.Key getKey()
Returns the key. This is either the key that this Encryptor
has been constructed with or
a key generated by a SecretKeyFactory
according to a KeySpec
.
public javax.crypto.CipherInputStream wrapInputStream(java.io.InputStream is) throws java.security.GeneralSecurityException, java.io.IOException
Wraps an InputStream
with a CipherInputStream
using this encryptor's cipher.
If an ivLength
has been specified and prependIV
is set to true
this method
will try to read and consume an IV from the InputStream
before wrapping it.
is
- java.security.GeneralSecurityException
java.io.IOException
public javax.crypto.CipherInputStream wrapInputStream(java.io.InputStream is, byte[] iv) throws java.security.GeneralSecurityException, java.io.IOException
Wraps an InputStream
with a CipherInputStream
using this encryptor's cipher.
If an ivLength
has been specified and prependIV
is set to true
this method
will try to read and consume an IV from the InputStream
before wrapping it.
is
- iv
- java.security.GeneralSecurityException
java.io.IOException
public javax.crypto.CipherOutputStream wrapOutputStream(java.io.OutputStream os) throws java.security.GeneralSecurityException, java.io.IOException
Wraps an OutputStream
with a CipherOutputStream
using this encryptor's cipher.
If an ivLength
has been specified or an explicit IV has been set during construction
and prependIV
is set to true
this method will write an IV to the OutputStream
before wrapping it.
os
- java.security.GeneralSecurityException
java.io.IOException
public javax.crypto.CipherOutputStream wrapOutputStream(java.io.OutputStream os, byte[] iv) throws java.security.GeneralSecurityException, java.io.IOException
Wraps an OutputStream
with a CipherOutputStream
using this encryptor's cipher.
If an ivLength
has been specified or an explicit IV has been set during construction
and prependIV
is set to true
this method will write an IV to the OutputStream
before wrapping it.
os
- java.security.GeneralSecurityException
java.io.IOException
public javax.crypto.Cipher getCipher() throws java.security.GeneralSecurityException
Returns the thread local cipher.
java.security.GeneralSecurityException