public class EncryptionUtil extends Object
This class uses properties obtained through Jets3tProperties
.
For more information on these properties please refer to
JetS3t Configuration
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_ALGORITHM |
static String |
DEFAULT_VERSION |
Constructor and Description |
---|
EncryptionUtil(String encryptionKey)
Constructs class configured with the provided password, and set up to use the default encryption
algorithm PBEWithMD5AndDES.
|
EncryptionUtil(String encryptionKey,
String algorithm,
String version)
Constructs class configured with the provided password, and set up to use the encryption
method specified.
|
Modifier and Type | Method and Description |
---|---|
static byte[] |
convertRsaPemToDer(InputStream is)
Convert a PEM encoded RSA certificate file into a DER format byte array.
|
byte[] |
decrypt(byte[] data)
Decrypts byte data to bytes.
|
byte[] |
decrypt(byte[] data,
int startIndex,
int endIndex)
Decrypts a byte data range to bytes.
|
CipherInputStream |
decrypt(InputStream is)
Wraps an input stream in an decrypting cipher stream.
|
CipherOutputStream |
decrypt(OutputStream os)
Wraps an output stream in a decrypting cipher stream.
|
String |
decryptString(byte[] data)
Decrypts byte data to a UTF-8 string.
|
String |
decryptString(byte[] data,
int startIndex,
int endIndex)
Decrypts a UTF-8 string.
|
byte[] |
encrypt(byte[] data)
Encrypts byte data to bytes.
|
CipherInputStream |
encrypt(InputStream is)
Wraps an input stream in an encrypting cipher stream.
|
CipherOutputStream |
encrypt(OutputStream os)
Wraps an output stream in an encrypting cipher stream.
|
byte[] |
encrypt(String data)
Encrypts a UTF-8 string to byte data.
|
String |
getAlgorithm() |
long |
getEncryptedOutputSize(long inputSize)
Returns an estimate of the number of bytes that will result when data
of the given length is encrypted.
|
protected Cipher |
initDecryptModeCipher() |
protected Cipher |
initEncryptModeCipher() |
static boolean |
isCipherAvailableForUse(String cipher)
Returns true if the given cipher is available and can be used by this encryption
utility.
|
static String[] |
listAvailablePbeCiphers(boolean testAvailability)
Lists the PBE ciphers available on the system, optionally eliminating those
ciphers that are apparently available but cannot actually be used (perhaps due to
the lack of export-grade JCE settings).
|
static Provider[] |
listAvailableProviders() |
static void |
main(String[] args) |
static byte[] |
signWithRsaSha1(byte[] derPrivateKeyBytes,
byte[] dataToSign)
Generate an RSA SHA1 signature of the given data using the given private
key DER certificate.
|
public static final String DEFAULT_VERSION
public static final String DEFAULT_ALGORITHM
public EncryptionUtil(String encryptionKey, String algorithm, String version) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException
encryptionKey
- the password to use for encryption/decryption.algorithm
- the Java name of an encryption algorithm to use, eg PBEWithMD5AndDESversion
- the version of encyption to use, for historic and future compatibility.
Unless using an historic version, this should always be
DEFAULT_VERSION
InvalidKeyException
NoSuchAlgorithmException
NoSuchPaddingException
InvalidKeySpecException
public EncryptionUtil(String encryptionKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException
encryptionKey
- the password to use for encryption/decryption.InvalidKeyException
NoSuchAlgorithmException
NoSuchPaddingException
InvalidKeySpecException
protected Cipher initEncryptModeCipher() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException
protected Cipher initDecryptModeCipher() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException
public byte[] encrypt(String data) throws IllegalStateException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidKeySpecException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException
data
- data to encrypt.IllegalStateException
IllegalBlockSizeException
BadPaddingException
UnsupportedEncodingException
InvalidKeySpecException
InvalidKeyException
InvalidAlgorithmParameterException
NoSuchAlgorithmException
NoSuchPaddingException
public String decryptString(byte[] data) throws InvalidKeyException, InvalidAlgorithmParameterException, UnsupportedEncodingException, IllegalStateException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException
data
- data to decrypt.InvalidKeyException
InvalidAlgorithmParameterException
UnsupportedEncodingException
IllegalStateException
IllegalBlockSizeException
BadPaddingException
NoSuchAlgorithmException
NoSuchPaddingException
public String decryptString(byte[] data, int startIndex, int endIndex) throws InvalidKeyException, InvalidAlgorithmParameterException, UnsupportedEncodingException, IllegalStateException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException
data
- data to decrypt.startIndex
- start index of data to decrypt.endIndex
- end index of data to decrypt.InvalidKeyException
InvalidAlgorithmParameterException
UnsupportedEncodingException
IllegalStateException
IllegalBlockSizeException
BadPaddingException
NoSuchAlgorithmException
NoSuchPaddingException
public byte[] encrypt(byte[] data) throws IllegalStateException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException
data
- data to encrypt.IllegalStateException
IllegalBlockSizeException
BadPaddingException
InvalidKeyException
InvalidAlgorithmParameterException
NoSuchAlgorithmException
NoSuchPaddingException
public byte[] decrypt(byte[] data) throws InvalidKeyException, InvalidAlgorithmParameterException, IllegalStateException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException
data
- data to decryptInvalidKeyException
InvalidAlgorithmParameterException
IllegalStateException
IllegalBlockSizeException
BadPaddingException
NoSuchAlgorithmException
NoSuchPaddingException
public byte[] decrypt(byte[] data, int startIndex, int endIndex) throws InvalidKeyException, InvalidAlgorithmParameterException, IllegalStateException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException
data
- startIndex
- endIndex
- InvalidKeyException
InvalidAlgorithmParameterException
IllegalStateException
IllegalBlockSizeException
BadPaddingException
NoSuchAlgorithmException
NoSuchPaddingException
public CipherInputStream encrypt(InputStream is) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException
is
- InvalidKeyException
InvalidAlgorithmParameterException
NoSuchAlgorithmException
NoSuchPaddingException
public CipherInputStream decrypt(InputStream is) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException
is
- InvalidKeyException
InvalidAlgorithmParameterException
NoSuchAlgorithmException
NoSuchPaddingException
public CipherOutputStream encrypt(OutputStream os) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException
os
- InvalidKeyException
InvalidAlgorithmParameterException
NoSuchAlgorithmException
NoSuchPaddingException
public CipherOutputStream decrypt(OutputStream os) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException
os
- InvalidKeyException
InvalidAlgorithmParameterException
NoSuchAlgorithmException
NoSuchPaddingException
public long getEncryptedOutputSize(long inputSize) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException
inputSize
- The number of bytes you intend to encrypt.InvalidKeyException
InvalidAlgorithmParameterException
NoSuchAlgorithmException
NoSuchPaddingException
public String getAlgorithm()
public static boolean isCipherAvailableForUse(String cipher)
cipher
- public static String[] listAvailablePbeCiphers(boolean testAvailability)
testAvailability
- if true each apparently available cipher is tested and only those that pass
isCipherAvailableForUse(String)
are returned.public static Provider[] listAvailableProviders()
public static byte[] signWithRsaSha1(byte[] derPrivateKeyBytes, byte[] dataToSign) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, InvalidKeySpecException, NoSuchProviderException
public static byte[] convertRsaPemToDer(InputStream is) throws IOException
is
- Input stream for PEM encoded RSA certificate data.IOException
Copyright © 2006–2018. All rights reserved.