public class AESCrypt extends Object
Requires Java 6 and Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files.
Thread-safety and sharing: this class is not thread-safe.
AESCrypt objects can be used as Commands (create, use once and dispose),
or reused to perform multiple operations (not concurrently though).
Modifier and Type | Field and Description |
---|---|
private SecretKeySpec |
aesKey1 |
private SecretKeySpec |
aesKey2 |
private static int |
BLOCK_SIZE |
private Cipher |
cipher |
private static String |
CRYPT_ALG |
private static String |
CRYPT_TRANS |
private boolean |
DEBUG |
private static byte[] |
DEFAULT_MAC |
private MessageDigest |
digest |
static String |
DIGEST_ALG_1024 |
static String |
DIGEST_ALG_256 |
static String |
DIGEST_ALG_512 |
private String |
disgestAlg |
private Mac |
hmac |
private static String |
HMAC_ALG |
private IvParameterSpec |
ivSpec1 |
private IvParameterSpec |
ivSpec2 |
private static String |
JCE_EXCEPTION_MESSAGE |
private static int |
KEY_SIZE |
private byte[] |
password |
private SecureRandom |
random |
private static String |
RANDOM_ALG |
private static int |
SHA_SIZE |
Constructor and Description |
---|
AESCrypt(boolean debug,
String password)
Builds an object to encrypt or decrypt files with the given password.
|
AESCrypt(boolean debug,
String password,
String digestSize)
Builds an object to encrypt or decrypt files with the given password.
|
AESCrypt(String password)
Builds an object to encrypt or decrypt files with the given password.
|
Modifier and Type | Method and Description |
---|---|
protected void |
debug(String message)
Prints a debug message on standard output if DEBUG mode is turned on.
|
protected void |
debug(String message,
byte[] bytes)
Prints a debug message on standard output if DEBUG mode is turned on.
|
void |
decrypt(long inSize,
InputStream in,
OutputStream out)
The input stream is decrypted and saved to the output stream.
|
void |
decrypt(String fromPath,
String toPath)
The file at fromPath is decrypted and saved at toPath location.
|
protected void |
digestRandomBytes(byte[] bytes,
int num)
SHA256 digest over given byte array and random bytes.
|
void |
encrypt(int version,
InputStream in,
OutputStream out)
The input stream is encrypted and saved to the output stream.
|
void |
encrypt(int version,
String fromPath,
String toPath)
The file at fromPath is encrypted and saved at toPath location.
|
protected byte[] |
generateAESKey1(byte[] iv,
byte[] password)
Generates an AES key starting with an IV and applying the supplied user password.
|
protected byte[] |
generateAESKey2()
Generates the random AES key used to crypt file contents.
|
protected byte[] |
generateIv1()
Generates a pseudo-random IV based on time and this computer's MAC.
|
protected byte[] |
generateIV2()
Generates the random IV used to crypt file contents.
|
protected byte[] |
generateRandomBytes(int len)
Generates a pseudo-random byte array.
|
static void |
main(String[] args) |
protected void |
readBytes(InputStream in,
byte[] bytes)
Utility method to read bytes from a stream until the given array is fully filled.
|
void |
setPassword(String password)
Changes the password this object uses to encrypt and decrypt.
|
private static final String JCE_EXCEPTION_MESSAGE
private static final String RANDOM_ALG
public static final String DIGEST_ALG_256
public static final String DIGEST_ALG_512
public static final String DIGEST_ALG_1024
private static final String HMAC_ALG
private static final String CRYPT_ALG
private static final String CRYPT_TRANS
private static final byte[] DEFAULT_MAC
private static final int KEY_SIZE
private static final int BLOCK_SIZE
private static final int SHA_SIZE
private final boolean DEBUG
private byte[] password
private SecureRandom random
private MessageDigest digest
private IvParameterSpec ivSpec1
private SecretKeySpec aesKey1
private IvParameterSpec ivSpec2
private SecretKeySpec aesKey2
private String disgestAlg
public AESCrypt(String password) throws GeneralSecurityException, UnsupportedEncodingException
GeneralSecurityException
- if the platform does not support the required cryptographic methods.UnsupportedEncodingException
- if UTF-16 encoding is not supported.public AESCrypt(boolean debug, String password) throws GeneralSecurityException, UnsupportedEncodingException
GeneralSecurityException
- if the platform does not support the required cryptographic methods.UnsupportedEncodingException
- if UTF-16 encoding is not supported.public AESCrypt(boolean debug, String password, String digestSize) throws GeneralSecurityException, UnsupportedEncodingException
GeneralSecurityException
- if the platform does not support the required cryptographic methods.UnsupportedEncodingException
- if UTF-16 encoding is not supported.protected void debug(String message)
protected void debug(String message, byte[] bytes)
protected byte[] generateRandomBytes(int len)
protected void digestRandomBytes(byte[] bytes, int num)
The generated hash is saved back to the original byte array.
Maximum array size is SHA_SIZE
bytes.
protected byte[] generateIv1()
This IV is used to crypt IV 2 and AES key 2 in the file.
protected byte[] generateAESKey1(byte[] iv, byte[] password)
This AES key is used to crypt IV 2 and AES key 2.
KEY_SIZE
bytes.protected byte[] generateIV2()
protected byte[] generateAESKey2()
KEY_SIZE
bytes.protected void readBytes(InputStream in, byte[] bytes) throws IOException
IOException
- if the array can't be filled.public void setPassword(String password) throws UnsupportedEncodingException
UnsupportedEncodingException
- if UTF-16 encoding is not supported.public void encrypt(int version, String fromPath, String toPath) throws IOException, GeneralSecurityException
version can be either 1 or 2.
IOException
- when there are I/O errors.GeneralSecurityException
- if the platform does not support the required cryptographic methods.public void encrypt(int version, InputStream in, OutputStream out) throws IOException, GeneralSecurityException
version can be either 1 or 2.
None of the streams are closed.
IOException
- when there are I/O errors.GeneralSecurityException
- if the platform does not support the required cryptographic methods.public void decrypt(String fromPath, String toPath) throws IOException, GeneralSecurityException
The input file can be encrypted using version 1 or 2 of aescrypt.
IOException
- when there are I/O errors.GeneralSecurityException
- if the platform does not support the required cryptographic methods.public void decrypt(long inSize, InputStream in, OutputStream out) throws IOException, GeneralSecurityException
The input file size is needed in advance.
The input stream can be encrypted using version 1 or 2 of aescrypt.
None of the streams are closed.
IOException
- when there are I/O errors.GeneralSecurityException
- if the platform does not support the required cryptographic methods.WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses