public class EncryptionUtil
extends java.lang.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 java.lang.String |
DEFAULT_ALGORITHM |
static java.lang.String |
DEFAULT_VERSION |
Constructor and Description |
---|
EncryptionUtil(java.lang.String encryptionKey)
Constructs class configured with the provided password, and set up to use the default encryption
algorithm PBEWithMD5AndDES.
|
EncryptionUtil(java.lang.String encryptionKey,
java.lang.String algorithm,
java.lang.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(java.io.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.
|
javax.crypto.CipherInputStream |
decrypt(java.io.InputStream is)
Wraps an input stream in an decrypting cipher stream.
|
javax.crypto.CipherOutputStream |
decrypt(java.io.OutputStream os)
Wraps an output stream in a decrypting cipher stream.
|
java.lang.String |
decryptString(byte[] data)
Decrypts byte data to a UTF-8 string.
|
java.lang.String |
decryptString(byte[] data,
int startIndex,
int endIndex)
Decrypts a UTF-8 string.
|
byte[] |
encrypt(byte[] data)
Encrypts byte data to bytes.
|
javax.crypto.CipherInputStream |
encrypt(java.io.InputStream is)
Wraps an input stream in an encrypting cipher stream.
|
javax.crypto.CipherOutputStream |
encrypt(java.io.OutputStream os)
Wraps an output stream in an encrypting cipher stream.
|
byte[] |
encrypt(java.lang.String data)
Encrypts a UTF-8 string to byte data.
|
java.lang.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 javax.crypto.Cipher |
initDecryptModeCipher() |
protected javax.crypto.Cipher |
initEncryptModeCipher() |
static boolean |
isCipherAvailableForUse(java.lang.String cipher)
Returns true if the given cipher is available and can be used by this encryption
utility.
|
static java.lang.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 java.security.Provider[] |
listAvailableProviders() |
static void |
main(java.lang.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 java.lang.String DEFAULT_VERSION
public static final java.lang.String DEFAULT_ALGORITHM
public EncryptionUtil(java.lang.String encryptionKey, java.lang.String algorithm, java.lang.String version) throws java.security.InvalidKeyException, java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.spec.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
java.security.InvalidKeyException
java.security.NoSuchAlgorithmException
javax.crypto.NoSuchPaddingException
java.security.spec.InvalidKeySpecException
public EncryptionUtil(java.lang.String encryptionKey) throws java.security.InvalidKeyException, java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.spec.InvalidKeySpecException
encryptionKey
- the password to use for encryption/decryption.java.security.InvalidKeyException
java.security.NoSuchAlgorithmException
javax.crypto.NoSuchPaddingException
java.security.spec.InvalidKeySpecException
protected javax.crypto.Cipher initEncryptModeCipher() throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
java.security.NoSuchAlgorithmException
javax.crypto.NoSuchPaddingException
java.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException
protected javax.crypto.Cipher initDecryptModeCipher() throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
java.security.NoSuchAlgorithmException
javax.crypto.NoSuchPaddingException
java.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException
public byte[] encrypt(java.lang.String data) throws java.lang.IllegalStateException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException, java.io.UnsupportedEncodingException, java.security.spec.InvalidKeySpecException, java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException, java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException
data
- data to encrypt.java.lang.IllegalStateException
javax.crypto.IllegalBlockSizeException
javax.crypto.BadPaddingException
java.io.UnsupportedEncodingException
java.security.spec.InvalidKeySpecException
java.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException
java.security.NoSuchAlgorithmException
javax.crypto.NoSuchPaddingException
public java.lang.String decryptString(byte[] data) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException, java.io.UnsupportedEncodingException, java.lang.IllegalStateException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException, java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException
data
- data to decrypt.java.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException
java.io.UnsupportedEncodingException
java.lang.IllegalStateException
javax.crypto.IllegalBlockSizeException
javax.crypto.BadPaddingException
java.security.NoSuchAlgorithmException
javax.crypto.NoSuchPaddingException
public java.lang.String decryptString(byte[] data, int startIndex, int endIndex) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException, java.io.UnsupportedEncodingException, java.lang.IllegalStateException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException, java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException
data
- data to decrypt.startIndex
- start index of data to decrypt.endIndex
- end index of data to decrypt.java.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException
java.io.UnsupportedEncodingException
java.lang.IllegalStateException
javax.crypto.IllegalBlockSizeException
javax.crypto.BadPaddingException
java.security.NoSuchAlgorithmException
javax.crypto.NoSuchPaddingException
public byte[] encrypt(byte[] data) throws java.lang.IllegalStateException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException, java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException, java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException
data
- data to encrypt.java.lang.IllegalStateException
javax.crypto.IllegalBlockSizeException
javax.crypto.BadPaddingException
java.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException
java.security.NoSuchAlgorithmException
javax.crypto.NoSuchPaddingException
public byte[] decrypt(byte[] data) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException, java.lang.IllegalStateException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException, java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException
data
- data to decryptjava.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException
java.lang.IllegalStateException
javax.crypto.IllegalBlockSizeException
javax.crypto.BadPaddingException
java.security.NoSuchAlgorithmException
javax.crypto.NoSuchPaddingException
public byte[] decrypt(byte[] data, int startIndex, int endIndex) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException, java.lang.IllegalStateException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException, java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException
data
- startIndex
- endIndex
- java.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException
java.lang.IllegalStateException
javax.crypto.IllegalBlockSizeException
javax.crypto.BadPaddingException
java.security.NoSuchAlgorithmException
javax.crypto.NoSuchPaddingException
public javax.crypto.CipherInputStream encrypt(java.io.InputStream is) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException, java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException
is
- java.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException
java.security.NoSuchAlgorithmException
javax.crypto.NoSuchPaddingException
public javax.crypto.CipherInputStream decrypt(java.io.InputStream is) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException, java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException
is
- java.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException
java.security.NoSuchAlgorithmException
javax.crypto.NoSuchPaddingException
public javax.crypto.CipherOutputStream encrypt(java.io.OutputStream os) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException, java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException
os
- java.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException
java.security.NoSuchAlgorithmException
javax.crypto.NoSuchPaddingException
public javax.crypto.CipherOutputStream decrypt(java.io.OutputStream os) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException, java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException
os
- java.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException
java.security.NoSuchAlgorithmException
javax.crypto.NoSuchPaddingException
public long getEncryptedOutputSize(long inputSize) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException, java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException
inputSize
- The number of bytes you intend to encrypt.java.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException
java.security.NoSuchAlgorithmException
javax.crypto.NoSuchPaddingException
public java.lang.String getAlgorithm()
public static boolean isCipherAvailableForUse(java.lang.String cipher)
cipher
- public static java.lang.String[] listAvailablePbeCiphers(boolean testAvailability)
testAvailability
- if true each apparently available cipher is tested and only those that pass
isCipherAvailableForUse(String)
are returned.public static java.security.Provider[] listAvailableProviders()
public static byte[] signWithRsaSha1(byte[] derPrivateKeyBytes, byte[] dataToSign) throws java.security.NoSuchAlgorithmException, java.security.InvalidKeyException, java.security.SignatureException, java.security.spec.InvalidKeySpecException, java.security.NoSuchProviderException
java.security.NoSuchAlgorithmException
java.security.InvalidKeyException
java.security.SignatureException
java.security.spec.InvalidKeySpecException
java.security.NoSuchProviderException
public static byte[] convertRsaPemToDer(java.io.InputStream is) throws java.io.IOException
is
- Input stream for PEM encoded RSA certificate data.java.io.IOException
public static void main(java.lang.String[] args) throws java.lang.Exception
java.lang.Exception