public class CipherHelper extends Object
Cipher
class to
accomplish most of these operations. The helper works with a set CipherTransformation
which is defined at
creation.Constructor and Description |
---|
CipherHelper()
Creates a helper with
CipherTransformation.DEFAULT as the transformation. |
CipherHelper(CipherTransformation transformation)
Create a helper with the given transformation.
|
Modifier and Type | Method and Description |
---|---|
byte[] |
decrypt(byte[] key,
byte[] iv,
byte[] ciphertext)
This will decrypt the ciphertext using the given key and iv.
|
EncryptionResults |
encrypt(byte[] plaintext)
This will encrypt the plaintext using the given key and iv.
|
byte[] |
encrypt(byte[] key,
byte[] iv,
byte[] plaintext)
This will encrypt the plaintext using the given key and iv.
|
byte[] |
generateIV()
Using the block size of the cipher and the mode of the transformation this will generate an initialization
vector.
|
byte[] |
generateKey()
This will generate a symmetric key using
AESKeyStrength.DEFAULT for determining the size. |
byte[] |
generateKey(AESKeyStrength keyStrength)
This will generate a symmetric key using the given
AESKeyStrength for determining the size. |
byte[] |
generateKey(int keySize)
This will generate a symmetric key of the given size using the algorithm of the helper's defined
CipherTransformation . |
public CipherHelper() throws NoSuchAlgorithmException, NoSuchPaddingException
CipherTransformation.DEFAULT
as the transformation.NoSuchAlgorithmException
- thrown if the algorithm in the transformation is not supportedNoSuchPaddingException
- thrown if the padding in the transformation is not supportedpublic CipherHelper(CipherTransformation transformation) throws NoSuchPaddingException, NoSuchAlgorithmException
transformation
- the transformation to use in this helperNoSuchAlgorithmException
- thrown if the algorithm in the transformation is not supportedNoSuchPaddingException
- thrown if the padding in the transformation is not supportedpublic byte[] decrypt(byte[] key, byte[] iv, byte[] ciphertext) throws BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidKeyException
Cipher
created using the information
from the CipherTransformation
is used for the decryption.key
- the key to use for decrypting the dataiv
- the iv to use for decrypting the dataciphertext
- the data to decryptBadPaddingException
- thrown if the padding in the transformation is not supportedIllegalBlockSizeException
- thrown if the block size in the transformation is not supportedInvalidAlgorithmParameterException
- thrown if the algorithm in the transformation is not supportedInvalidKeyException
- thrown if the key is not validpublic byte[] encrypt(byte[] key, byte[] iv, byte[] plaintext) throws InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException
Cipher
created using the information
from the CipherTransformation
is used for the encryption.key
- the key to use for encrypting the dataiv
- the iv to use for encrypting the dataplaintext
- the data to encryptInvalidAlgorithmParameterException
- thrown if the algorithm in the transformation is not supportedInvalidKeyException
- thrown if the key is not validBadPaddingException
- thrown if the padding in the transformation is not supportedIllegalBlockSizeException
- thrown if the block size in the transformation is not supportedpublic EncryptionResults encrypt(byte[] plaintext) throws InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException
Cipher
created using the information
from the CipherTransformation
is used for the encryption.plaintext
- the data to encryptInvalidAlgorithmParameterException
- thrown if the algorithm in the transformation is not supportedInvalidKeyException
- thrown if the key is not validBadPaddingException
- thrown if the padding in the transformation is not supportedIllegalBlockSizeException
- thrown if the block size in the transformation is not supportedNoSuchAlgorithmException
- thrown if the algorithm of the transformation is not supportedpublic byte[] generateIV()
public byte[] generateKey() throws NoSuchAlgorithmException
AESKeyStrength.DEFAULT
for determining the size.
Note: this will only work if the helper's transformation has an algorithm of AES.
NoSuchAlgorithmException
- thrown if the algorithm of the transformation is not supportedpublic byte[] generateKey(int keySize) throws NoSuchAlgorithmException
CipherTransformation
.keySize
- the symmetric key size in bitsNoSuchAlgorithmException
- thrown if the algorithm of the transformation is not supportedpublic byte[] generateKey(AESKeyStrength keyStrength) throws NoSuchAlgorithmException
AESKeyStrength
for determining the size.
Note: this will only work if the helper's transformation has an algorithm of AES.
keyStrength
- the AESKeyStrength
to use when generating the key - used to get the key size in bitsNoSuchAlgorithmException
- thrown if the algorithm of the transformation is not supportedCopyright © 2021. All rights reserved.