public class PBKDF2Helper extends Object
PBKDF2
for the key generation and CipherHelper
for the encrypt/decrypt operations.Constructor and Description |
---|
PBKDF2Helper()
This will create a helper using
MacAlgorithm.HMACSHA384 , CipherTransformation.AES_CTR_NOPADDING ,
AESKeyStrength.AES256 and UTF-8 for the encoding. |
PBKDF2Helper(MacAlgorithm macAlgorithm,
CipherTransformation transformation,
AESKeyStrength keyStrength,
String encoding)
This will create a helper with the given settings for key generation and encryption.
|
Modifier and Type | Method and Description |
---|---|
byte[] |
decryptFromFormat(byte[] formattedData,
String password,
int iterationCount)
This will decrypt data that was encrypted with a PBKDF2 derived key where the data is in the following format:
salt length + salt + iv length + iv + encrypted data length + encrypted data.
|
byte[] |
encryptToFormat(byte[] data,
byte[] salt,
String password,
int iterationCount)
This will encrypt data with a PBKDF2 derived key and then format the data as follows: salt length + salt + iv
length + iv + encrypted data length + encrypted data.
|
byte[] |
encryptToFormat(byte[] data,
int saltLength,
String password,
int iterationCount)
This will encrypt data with a PBKDF2 derived key and then format the data as follows: salt length + salt + iv
length + iv + encrypted data length + encrypted data.
|
byte[] |
generateDerivedKey(String password,
byte[] salt,
int iterationCount)
Generate the derived PBKDF2 key using the password, salt and iteration count.
|
public PBKDF2Helper() throws NoSuchPaddingException, NoSuchAlgorithmException
MacAlgorithm.HMACSHA384
, CipherTransformation.AES_CTR_NOPADDING
,
AESKeyStrength.AES256
and UTF-8 for the encoding.NoSuchPaddingException
- thrown if padding not supportedNoSuchAlgorithmException
- thrown if algorithm not supportedpublic PBKDF2Helper(MacAlgorithm macAlgorithm, CipherTransformation transformation, AESKeyStrength keyStrength, String encoding) throws NoSuchAlgorithmException, NoSuchPaddingException
macAlgorithm
- the name of the MAC algorithm to usetransformation
- the transformation to use for encrypting datakeyStrength
- the key strength for determining the key size in bytesencoding
- the encoding to useNoSuchPaddingException
- thrown if padding not supportedNoSuchAlgorithmException
- thrown if algorithm not supportedpublic byte[] decryptFromFormat(byte[] formattedData, String password, int iterationCount) throws IOException, IllegalBlockSizeException, InvalidKeyException, InvalidAlgorithmParameterException, BadPaddingException
CipherHelper
for the decryption and PBKDF2
for the key generationformattedData
- the data in the following format: salt length + salt + iv length + iv + encrypted data
length + encrypted datapassword
- the password used to derive the keyiterationCount
- the iteration count used to derive the keyIOException
- thrown if there was an error reading in the formatted dataIllegalBlockSizeException
- thrown if the cipher block size is wrongInvalidKeyException
- thrown if there is an issue with the keyInvalidAlgorithmParameterException
- thrown if the cipher algorithm is not supportedBadPaddingException
- thrown if the cipher padding is not supportedpublic byte[] encryptToFormat(byte[] data, int saltLength, String password, int iterationCount) throws IOException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, InvalidKeyException
CipherHelper
for the encryption and PBKDF2
for the key generationdata
- the data to encryptsaltLength
- the length of the salt to create with random datapassword
- the password used to derive the keyiterationCount
- the iteration count used to derive the keyIOException
- thrown if there was an error reading in the formatted dataIllegalBlockSizeException
- thrown if the cipher block size is wrongInvalidKeyException
- thrown if there is an issue with the keyInvalidAlgorithmParameterException
- thrown if the cipher algorithm is not supportedBadPaddingException
- thrown if the cipher padding is not supportedpublic byte[] encryptToFormat(byte[] data, byte[] salt, String password, int iterationCount) throws IOException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, InvalidKeyException
CipherHelper
for the encryption and
PBKDF2
for the key generationdata
- the data to encryptsalt
- the salt to use when deriving the keypassword
- the password used to derive the keyiterationCount
- the iteration count used to derive the keyIOException
- thrown if there was an error reading in the formatted dataIllegalBlockSizeException
- thrown if the cipher block size is wrongInvalidKeyException
- thrown if there is an issue with the keyInvalidAlgorithmParameterException
- thrown if the cipher algorithm is not supportedBadPaddingException
- thrown if the cipher padding is not supportedpublic byte[] generateDerivedKey(String password, byte[] salt, int iterationCount) throws InvalidKeyException, UnsupportedEncodingException
PBKDF2
is used to
derive the key.password
- the passwordsalt
- the saltiterationCount
- the iteration countInvalidKeyException
- thrown if an error occurred while deriving the keyUnsupportedEncodingException
- thrown if the provided encoding is not supportedCopyright © 2021. All rights reserved.