public class Crypto extends Object
| Modifier and Type | Field and Description |
|---|---|
static String |
CIPHER_ALGORITHM
The name of the cipher algorithm to use for symmetric cryptographic
operations.
|
static String |
CIPHER_MODE
The name of the cipher mode to use for symmetric cryptographic
operations.
|
static String |
CIPHER_NAME
The full name of the
Cipher to use for cryptographic operations,
in a format suitable for passing to the JCE. |
static String |
CIPHER_PADDING
The name of the padding type to use for symmetric cryptographic
operations.
|
| Modifier | Constructor and Description |
|---|---|
|
Crypto()
Initialises the instance by getting and caching a
Cipher instance
for "AES/CTR/NoPadding". |
protected |
Crypto(String cipherName)
This constructor is protected so that, should you need a different
algorithm (e.g.
|
| Modifier and Type | Method and Description |
|---|---|
protected byte[] |
decrypt(byte[] bytes,
SecretKey key)
This method decrypts the given bytes and returns the plain text.
|
InputStream |
decrypt(InputStream source,
SecretKey key)
This method wraps the source
InputStream with a
CipherInputStream. |
InputStream |
decrypt(InputStream source,
String password)
This method wraps the source
InputStream with a
CipherInputStream. |
String |
decrypt(String encrypted,
SecretKey key)
This method decrypts the given String and returns the plain text.
|
String |
decrypt(String encrypted,
String password)
This method decrypts the given String and returns the plain text.
|
protected byte[] |
encrypt(byte[] bytes,
SecretKey key)
This method encrypts a byte array.
|
InputStream |
encrypt(InputStream source,
SecretKey key)
This method wraps the source
InputStream with a
CipherInputStream. |
OutputStream |
encrypt(OutputStream destination,
SecretKey key)
This method wraps the destination
OutputStream with a
CipherOutputStream. |
OutputStream |
encrypt(OutputStream destination,
String password)
This method wraps the destination
OutputStream with a
CipherOutputStream. |
String |
encrypt(String string,
SecretKey key)
This method encrypts the given String, returning a base-64 encoded
String.
|
String |
encrypt(String string,
String password)
This method encrypts the given String, returning a base-64 encoded
String.
|
int |
getIvSize() |
public static final String CIPHER_ALGORITHM
public static final String CIPHER_MODE
public static final String CIPHER_PADDING
public static final String CIPHER_NAME
Cipher to use for cryptographic operations,
in a format suitable for passing to the JCE.public Crypto()
Cipher instance
for "AES/CTR/NoPadding".protected Crypto(String cipherName)
cipherName - This should normally be "AES/CTR/NoPadding".public String encrypt(String string, String password)
string - The input String.password - A password to use as the basis for generating an encryption
key. This method calls
Keys.generateSecretKey(String, String)IllegalArgumentException - If the given key is not a valid "AES"
key.decrypt(String, String)public String encrypt(String string, SecretKey key)
string - The input String.key - The key to be used to encrypt the String.IllegalArgumentException - If the given key is not a valid "AES"
key.decrypt(String, SecretKey)protected byte[] encrypt(byte[] bytes,
SecretKey key)
Crypto to expose this method.
This is the bread-and-butter of most encryption operations, but
ultimately is a less common application-level use-case, because binary
data is usually stream-based and the rest of the time it tends to be
Strings you need to deal with. This is why it isn't exposed by default.bytes - The input data.key - The key to be used to encrypt the data.IllegalArgumentException - If the given key is not a valid "AES"
key.decrypt(byte[], SecretKey)public String decrypt(String encrypted, String password)
encrypted - The encrypted String, base-64 encoded, as returned by
encrypt(String, SecretKey).password - The password used for encryption. This will be used to
generate the correct key by calling
Keys.generateSecretKey(String, String)IllegalArgumentException - If the given key is not a valid "AES"
key.encrypt(String, SecretKey)public String decrypt(String encrypted, SecretKey key)
encrypted - The encrypted String, base-64 encoded, as returned by
encrypt(String, SecretKey).key - The key to be used for decryption.IllegalArgumentException - If the given key is not a valid "AES"
key.encrypt(String, SecretKey)protected byte[] decrypt(byte[] bytes,
SecretKey key)
Crypto to expose this method.
This is the bread-and-butter of most encryption operations, but
ultimately is a less common application-level use-case, because binary
data is usually stream-based and the rest of the time it tends to be
Strings you need to deal with. This is why it isn't exposed by default.bytes - The encrypted data.key - The key to be used for decryption.IllegalArgumentException - If the given key is not a valid "AES"
key.encrypt(byte[], SecretKey)public OutputStream encrypt(OutputStream destination, String password) throws IOException
OutputStream with a
CipherOutputStream.
Typical usage is when you have an InputStream for a source of unencrypted
data, such as a user-uploaded file, and an OutputStream to write the
input to disk. You would call this method to wrap the OutputStream and
use the returned CipherOutputStream instead to write the data to,
so that it is encrypted as it is written to disk.
Note that this method writes a salt value and an initialisation vector to
the destination OutputStream, so the destination parameter will have some
bytes written to it before this method returns. These bytes are necessary
for decryption and a corresponding call to
decrypt(InputStream, String) will read and filter them out from
the underlying InputStream before returning it.destination - The output stream to be wrapped with a
CipherOutputStream.password - The password to be used to generate a key to encrypt data
written to the returned CipherOutputStream.CipherOutputStream, which wraps the given
OutputStream.IOException - If an error occurs in writing the initialisation vector to
the destination stream.IllegalArgumentException - If the given key is not a valid "AES"
key.decrypt(InputStream, String)public OutputStream encrypt(OutputStream destination, SecretKey key) throws IOException
OutputStream with a
CipherOutputStream.
Typical usage is when you have an InputStream for a source of unencrypted
data, such as a user-uploaded file, and an OutputStream to write the
input to disk. You would call this method to wrap the OutputStream and
use the returned CipherOutputStream instead to write the data to,
so that it is encrypted as it is written to disk.
Note that this method writes an initialisation vector to the destination
OutputStream, so the destination parameter will have some bytes written
to it before this method returns. These bytes are necessary for
decryption and a corresponding call to
decrypt(InputStream, SecretKey) will read and filter them out
from the underlying InputStream before returning it.destination - The output stream to be wrapped with a
CipherOutputStream.key - The key to be used to encrypt data written to the returned
CipherOutputStream.CipherOutputStream, which wraps the given
OutputStream.IOException - If an error occurs in writing the initialisation vector to
the destination stream.IllegalArgumentException - If the given key is not a valid "AES"
key.decrypt(InputStream, SecretKey)public InputStream encrypt(InputStream source, SecretKey key) throws IOException
InputStream with a
CipherInputStream.
Typical usage is when you have an InputStream for a source of encrypted
data on disk, and an OutputStream to send the file to an HTTP response.
You would call this method to wrap the InputStream and use the returned
CipherInputStream to read the data from instead so that it is
decrypted as it is read and can be written to the response unencrypted.
Note that this method reads and discards the random initialisation vector
from the source InputStream, so the source parameter will have some bytes
read from it before this method returns. These bytes are necessary for
decryption and the call to encrypt(OutputStream, SecretKey) will
have added these to the start of the underlying data automatically.source - The source InputStream, containing encrypted data.key - The key to be used for decryption.CipherInputStream, which wraps the given source stream
and will decrypt the data as they are read.IOException - If an error occurs in reading the initialisation vector from
the source stream.IllegalArgumentException - If the given key is not a valid "AES"
key.encrypt(OutputStream, SecretKey)public InputStream decrypt(InputStream source, String password) throws IOException
InputStream with a
CipherInputStream.
Typical usage is when you have an InputStream for a source of encrypted
data on disk, and an OutputStream to send the file to an HTTP response.
You would call this method to wrap the InputStream and use the returned
CipherInputStream to read the data from instead so that it is
decrypted as it is read and can be written to the response unencrypted.
Note that this method reads and discards a salt value and the random
initialisation vector from the source InputStream, so the source
parameter will have some bytes read from it before this method returns.
These bytes are necessary for decryption and the call to
encrypt(OutputStream, String) will have added these to the start
of the underlying data automatically.source - The source InputStream, containing encrypted data.password - The password to be used for decryption.CipherInputStream, which wraps the given source stream
and will decrypt the data as they are read.IOException - If an error occurs in reading the initialisation vector from
the source stream.IllegalArgumentException - If the given key is not a valid "AES"
key.encrypt(OutputStream, String)public InputStream decrypt(InputStream source, SecretKey key) throws IOException
InputStream with a
CipherInputStream.
Typical usage is when you have an InputStream for a source of encrypted
data on disk, and an OutputStream to send the file to an HTTP response.
You would call this method to wrap the InputStream and use the returned
CipherInputStream to read the data from instead so that it is
decrypted as it is read and can be written to the response unencrypted.
Note that this method reads and discards the random initialisation vector
from the source InputStream, so the source parameter will have some bytes
read from it before this method returns. These bytes are necessary for
decryption and the call to encrypt(OutputStream, SecretKey) will
have added these to the start of the underlying data automatically.source - The source InputStream, containing encrypted data.key - The key to be used for decryption.CipherInputStream, which wraps the given source stream
and will decrypt the data as they are read.IOException - If an error occurs in reading the initialisation vector from
the source stream.IllegalArgumentException - If the given key is not a valid "AES"
key.encrypt(OutputStream, SecretKey)public int getIvSize()
Copyright © 2015 Carboni. All rights reserved.