Module org.refcodes.forwardsecrecy
Package edu.vt.middleware.crypt
Class AbstractEncryptionAlgorithm
java.lang.Object
edu.vt.middleware.crypt.AbstractAlgorithm
edu.vt.middleware.crypt.AbstractEncryptionAlgorithm
- All Implemented Interfaces:
Algorithm
,EncryptionAlgorithm
- Direct Known Subclasses:
AsymmetricAlgorithm
public abstract class AbstractEncryptionAlgorithm
extends AbstractAlgorithm
implements EncryptionAlgorithm
Base class for symmetric and asymmetric encryption algorithms. This class is
essentially a wrapper for the
Cipher
class.- Version:
- $Revision: 2745 $
- Author:
- Middleware Services
-
Field Summary
Modifier and TypeFieldDescriptionprotected Cipher
Encryption/decription cipher.protected int
EitherCipher.ENCRYPT_MODE
orCipher.DECRYPT_MODE
.protected Key
Key used for encryption or decryption.protected String
Mode used for encryption and decryption.protected String
Padding used for encryption and decryption.Fields inherited from class edu.vt.middleware.crypt.AbstractAlgorithm
algorithm, randomByteSize, randomProvider
-
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractEncryptionAlgorithm
(String cipherAlgorithm, String cipherModeName, String cipherPadding) Creates a new encryption algorithm that uses a cipher of the given name. -
Method Summary
Modifier and TypeMethodDescriptionprotected byte[]
crypt
(byte[] in) Based on current cipher mode, encrypts or decrypts the given input data.protected void
crypt
(InputStream in, OutputStream out) Based on current cipher mode, encrypts or decrypts the data in the given input stream into resulting data in the output stream.byte[]
decrypt
(byte[] ciphertext) Decrypts the given ciphertext bytes into a byte array of plaintext using the decryption key.void
decrypt
(InputStream in, OutputStream out) Decrypts the data in the given ciphertext input stream into plaintext in the output stream.byte[]
Decrypts a string representation of ciphertext bytes into a byte array of plaintext using the decryption key.byte[]
encrypt
(byte[] plaintext) Encrypts the given plaintext bytes into a byte array of ciphertext using the encryption key.Encrypts the given plaintext bytes into a ciphertext string using the conversion strategy provided by the given converter.void
encrypt
(InputStream in, OutputStream out) Encrypts the data in the given plaintext input stream into ciphertext in the output stream.protected abstract AlgorithmParameterSpec
Gets the algorithm parameter specification for this algorithm.int
Gets the block size of the encryption algorithm cipher in bytes.protected abstract int
Gets the chunk size for buffers using in stream-based encryption and decryption operations.int
Gets the cipher mode indicating whether this instance is currently initialized for encryption or decryption.getMode()
Gets the encryption mode.Gets the encryption padding method.protected void
init
(int encryptOrDecrypt) Initializescipher
for either encryption or decryption.protected void
void
Initializes this instance for decryption operations.void
Initializes this instance for encryption operations.void
Sets the key used for encryption/decryption.toString()
Methods inherited from class edu.vt.middleware.crypt.AbstractAlgorithm
getAlgorithm, getRandomData, setRandomProvider
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface edu.vt.middleware.crypt.Algorithm
getAlgorithm, getRandomData, setRandomProvider
-
Field Details
-
cipher
Encryption/decription cipher. -
mode
Mode used for encryption and decryption. -
padding
Padding used for encryption and decryption. -
cipherMode
protected int cipherModeEitherCipher.ENCRYPT_MODE
orCipher.DECRYPT_MODE
. -
key
Key used for encryption or decryption.
-
-
Constructor Details
-
AbstractEncryptionAlgorithm
protected AbstractEncryptionAlgorithm(String cipherAlgorithm, String cipherModeName, String cipherPadding) Creates a new encryption algorithm that uses a cipher of the given name.- Parameters:
cipherAlgorithm
- Cipher algorithm name.cipherModeName
- Cipher mode.cipherPadding
- Cipher padding method.
-
-
Method Details
-
getMode
Gets the encryption mode.- Specified by:
getMode
in interfaceEncryptionAlgorithm
- Returns:
- Name of an encryption mode, e.g. CBC.
-
getPadding
Gets the encryption padding method.- Specified by:
getPadding
in interfaceEncryptionAlgorithm
- Returns:
- Name of a padding method, e.g. PKCS5Padding.
-
setKey
Sets the key used for encryption/decryption.- Specified by:
setKey
in interfaceEncryptionAlgorithm
- Parameters:
k
- Public, private, or secret key used for encryption or decryption.
-
getCipherMode
public int getCipherMode()Gets the cipher mode indicating whether this instance is currently initialized for encryption or decryption.- Specified by:
getCipherMode
in interfaceEncryptionAlgorithm
- Returns:
- Cipher.ENCRYPT_MODE,
Cipher.DECRYPT_MODE,
or 0 if the cipher mode has not been initialized by calling
either
EncryptionAlgorithm.initEncrypt()
orEncryptionAlgorithm.initDecrypt()
.
-
getBlockSize
public int getBlockSize()Gets the block size of the encryption algorithm cipher in bytes.- Specified by:
getBlockSize
in interfaceEncryptionAlgorithm
- Returns:
- Block size of cipher in bytes, or 0 if the cipher is not a block cipher.
-
initEncrypt
Initializes this instance for encryption operations.- Specified by:
initEncrypt
in interfaceEncryptionAlgorithm
- Throws:
CryptException
- On cryptographic configuration errors.
-
initDecrypt
Initializes this instance for decryption operations.- Specified by:
initDecrypt
in interfaceEncryptionAlgorithm
- Throws:
CryptException
- On cryptographic configuration errors.
-
encrypt
Encrypts the given plaintext bytes into a byte array of ciphertext using the encryption key.- Specified by:
encrypt
in interfaceEncryptionAlgorithm
- Parameters:
plaintext
- Input plaintext bytes.- Returns:
- Ciphertext resulting from plaintext encryption.
- Throws:
CryptException
- On encryption errors.
-
encrypt
Encrypts the given plaintext bytes into a ciphertext string using the conversion strategy provided by the given converter.- Specified by:
encrypt
in interfaceEncryptionAlgorithm
- Parameters:
plaintext
- Input plaintext bytes.converter
- Converter that converts ciphertext output bytes to a string representation.- Returns:
- Ciphertext string resulting from plaintext encryption.
- Throws:
CryptException
- On encryption errors.
-
encrypt
Encrypts the data in the given plaintext input stream into ciphertext in the output stream. UseBase64FilterOutputStream
orHexFilterOutputStream
to produce ciphertext in the output stream in an encoded string repreprestation.- Specified by:
encrypt
in interfaceEncryptionAlgorithm
- Parameters:
in
- Input stream of plaintext.out
- Output stream of ciphertext.- Throws:
CryptException
- On encryption errors.IOException
- On stream read/write errors.
-
decrypt
Decrypts the given ciphertext bytes into a byte array of plaintext using the decryption key.- Specified by:
decrypt
in interfaceEncryptionAlgorithm
- Parameters:
ciphertext
- Input ciphertext bytes.- Returns:
- Plaintext resulting from ciphertext decryption.
- Throws:
CryptException
- On decryption errors.
-
decrypt
Decrypts a string representation of ciphertext bytes into a byte array of plaintext using the decryption key.- Specified by:
decrypt
in interfaceEncryptionAlgorithm
- Parameters:
ciphertext
- Input ciphertext bytes.converter
- Converter that converts the ciphertext input string into raw bytes required by the cipher algorithm.- Returns:
- Plaintext resulting from ciphertext decryption.
- Throws:
CryptException
- On decryption errors.
-
decrypt
Decrypts the data in the given ciphertext input stream into plaintext in the output stream. UseBase64FilterInputStream
orHexFilterInputStream
to consume ciphertext in an encoded string representation.- Specified by:
decrypt
in interfaceEncryptionAlgorithm
- Parameters:
in
- Input stream of ciphertext.out
- Output stream of plaintext.- Throws:
CryptException
- On decryption errors.IOException
- On stream read/write errors.
-
toString
- Overrides:
toString
in classAbstractAlgorithm
-
initCipher
- Throws:
CryptException
- if the algorithm is not available from any provider or if the provider is not available in the environment.
-
init
Initializescipher
for either encryption or decryption.- Parameters:
encryptOrDecrypt
- Either Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE.- Throws:
CryptException
- On cryptographic configuration errors.
-
crypt
Based on current cipher mode, encrypts or decrypts the given input data.- Parameters:
in
- Cipher input data.- Returns:
- Cipher output data.
- Throws:
CryptException
- On encryption errors.
-
crypt
Based on current cipher mode, encrypts or decrypts the data in the given input stream into resulting data in the output stream.- Parameters:
in
- Input stream.out
- Output stream.- Throws:
CryptException
- On encryption errors.IOException
- On stream read/write errors.
-
getAlgorithmParameterSpec
Gets the algorithm parameter specification for this algorithm.- Returns:
- Algorithm parameter specification specific to this algorithm.
-
getChunkSize
protected abstract int getChunkSize()Gets the chunk size for buffers using in stream-based encryption and decryption operations.- Returns:
- Stream chunk size.
-