public class OpenSSL extends Object
// Encrypt! byte[] encryptedData = OpenSSL.encrypt( "des3", password, data );If you want to specify a raw key and iv directly (without using PBE), use the methods that take byte[] key, byte[] iv. Those byte[] arrays can be the raw binary, or they can be ascii (hex representation: '0' - 'F'). If you want to use PBE to derive the key and iv, then use the methods that take char[] password. This class is able to decrypt files encrypted with "openssl" unix utility. The "openssl" unix utility is able to decrypt files encrypted by this class. This class is also able to encrypt and decrypt its own files.
Modifier and Type | Class and Description |
---|---|
static class |
OpenSSL.CipherInfo |
Constructor and Description |
---|
OpenSSL() |
Modifier and Type | Method and Description |
---|---|
static byte[] |
decrypt(String cipher,
byte[] key,
byte[] iv,
byte[] encrypted) |
static InputStream |
decrypt(String cipher,
byte[] key,
byte[] iv,
InputStream encrypted) |
static byte[] |
decrypt(String cipher,
char[] pwd,
byte[] encrypted)
Decrypts data using a password and an OpenSSL compatible cipher
name.
|
static InputStream |
decrypt(String cipher,
char[] pwd,
InputStream encrypted)
Decrypts data using a password and an OpenSSL compatible cipher
name.
|
static DerivedKey |
deriveKey(char[] password,
byte[] salt,
int keySize,
boolean des2) |
static DerivedKey |
deriveKey(char[] password,
byte[] salt,
int keySize,
int ivSize,
boolean des2) |
static byte[] |
encrypt(String cipher,
byte[] key,
byte[] iv,
byte[] data) |
static byte[] |
encrypt(String cipher,
byte[] key,
byte[] iv,
byte[] data,
boolean toBase64) |
static InputStream |
encrypt(String cipher,
byte[] key,
byte[] iv,
InputStream data) |
static InputStream |
encrypt(String cipher,
byte[] key,
byte[] iv,
InputStream data,
boolean toBase64) |
static byte[] |
encrypt(String cipher,
char[] pwd,
byte[] data)
Encrypts data using a password and an OpenSSL compatible cipher
name.
|
static byte[] |
encrypt(String cipher,
char[] pwd,
byte[] data,
boolean toBase64)
Encrypts data using a password and an OpenSSL compatible cipher
name.
|
static byte[] |
encrypt(String cipher,
char[] pwd,
byte[] data,
boolean toBase64,
boolean useSalt)
Encrypts data using a password and an OpenSSL compatible cipher
name.
|
static InputStream |
encrypt(String cipher,
char[] pwd,
InputStream data)
Encrypts data using a password and an OpenSSL compatible cipher
name.
|
static InputStream |
encrypt(String cipher,
char[] pwd,
InputStream data,
boolean toBase64)
Encrypts data using a password and an OpenSSL compatible cipher
name.
|
static InputStream |
encrypt(String cipher,
char[] pwd,
InputStream data,
boolean toBase64,
boolean useSalt)
Encrypts data using a password and an OpenSSL compatible cipher
name.
|
static OpenSSL.CipherInfo |
lookup(String openSSLCipher)
Converts the way OpenSSL names its ciphers into a Java-friendly naming.
|
static void |
main(String[] args) |
public OpenSSL()
public static byte[] decrypt(String cipher, char[] pwd, byte[] encrypted) throws IOException, GeneralSecurityException
cipher
- The OpenSSL compatible cipher to use (try "man enc" on a
unix box to see what's possible). Some examples:
pwd
- password to use for this PBE decryptionencrypted
- byte array to decrypt. Can be raw, or base64.IOException
- problems with encrypted bytes (unlikely!)GeneralSecurityException
- problems decryptingpublic static InputStream decrypt(String cipher, char[] pwd, InputStream encrypted) throws IOException, GeneralSecurityException
cipher
- The OpenSSL compatible cipher to use (try "man enc" on a
unix box to see what's possible). Some examples:
pwd
- password to use for this PBE decryptionencrypted
- InputStream to decrypt. Can be raw, or base64.IOException
- problems with InputStreamGeneralSecurityException
- problems decryptingpublic static byte[] encrypt(String cipher, char[] pwd, byte[] data) throws IOException, GeneralSecurityException
cipher
- The OpenSSL compatible cipher to use (try "man enc" on a
unix box to see what's possible). Some examples:
pwd
- password to use for this PBE encryptiondata
- byte array to encryptIOException
- problems with the data byte arrayGeneralSecurityException
- problems encryptingpublic static InputStream encrypt(String cipher, char[] pwd, InputStream data) throws IOException, GeneralSecurityException
cipher
- The OpenSSL compatible cipher to use (try "man enc" on a
unix box to see what's possible). Some examples:
pwd
- password to use for this PBE encryptiondata
- InputStream to encryptIOException
- problems with the data InputStreamGeneralSecurityException
- problems encryptingpublic static byte[] encrypt(String cipher, char[] pwd, byte[] data, boolean toBase64) throws IOException, GeneralSecurityException
cipher
- The OpenSSL compatible cipher to use (try "man enc" on a
unix box to see what's possible). Some examples:
pwd
- password to use for this PBE encryptiondata
- byte array to encrypttoBase64
- true if resulting InputStream should contain base64,
IOException
- problems with the data byte arrayGeneralSecurityException
- problems encryptingpublic static InputStream encrypt(String cipher, char[] pwd, InputStream data, boolean toBase64) throws IOException, GeneralSecurityException
cipher
- The OpenSSL compatible cipher to use (try "man enc" on a
unix box to see what's possible). Some examples:
pwd
- password to use for this PBE encryptiondata
- InputStream to encrypttoBase64
- true if resulting InputStream should contain base64,
IOException
- problems with the data InputStreamGeneralSecurityException
- problems encryptingpublic static byte[] encrypt(String cipher, char[] pwd, byte[] data, boolean toBase64, boolean useSalt) throws IOException, GeneralSecurityException
cipher
- The OpenSSL compatible cipher to use (try "man enc" on a
unix box to see what's possible). Some examples:
pwd
- password to use for this PBE encryptiondata
- byte array to encrypttoBase64
- true if resulting InputStream should contain base64,
useSalt
- true if a salt should be used to derive the key.
useSalt
is true.IOException
- problems with the data InputStreamGeneralSecurityException
- problems encryptingpublic static InputStream encrypt(String cipher, char[] pwd, InputStream data, boolean toBase64, boolean useSalt) throws IOException, GeneralSecurityException
cipher
- The OpenSSL compatible cipher to use (try "man enc" on a
unix box to see what's possible). Some examples:
pwd
- password to use for this PBE encryptiondata
- InputStream to encrypttoBase64
- true if resulting InputStream should contain base64,
useSalt
- true if a salt should be used to derive the key.
useSalt
is true.IOException
- problems with the data InputStreamGeneralSecurityException
- problems encryptingpublic static byte[] decrypt(String cipher, byte[] key, byte[] iv, byte[] encrypted) throws IOException, GeneralSecurityException
IOException
GeneralSecurityException
public static InputStream decrypt(String cipher, byte[] key, byte[] iv, InputStream encrypted) throws IOException, GeneralSecurityException
IOException
GeneralSecurityException
public static byte[] encrypt(String cipher, byte[] key, byte[] iv, byte[] data) throws IOException, GeneralSecurityException
IOException
GeneralSecurityException
public static byte[] encrypt(String cipher, byte[] key, byte[] iv, byte[] data, boolean toBase64) throws IOException, GeneralSecurityException
IOException
GeneralSecurityException
public static InputStream encrypt(String cipher, byte[] key, byte[] iv, InputStream data) throws IOException, GeneralSecurityException
IOException
GeneralSecurityException
public static InputStream encrypt(String cipher, byte[] key, byte[] iv, InputStream data, boolean toBase64) throws IOException, GeneralSecurityException
IOException
GeneralSecurityException
public static DerivedKey deriveKey(char[] password, byte[] salt, int keySize, boolean des2) throws NoSuchAlgorithmException
NoSuchAlgorithmException
public static DerivedKey deriveKey(char[] password, byte[] salt, int keySize, int ivSize, boolean des2) throws NoSuchAlgorithmException
NoSuchAlgorithmException
public static OpenSSL.CipherInfo lookup(String openSSLCipher)
openSSLCipher
- OpenSSL cipher name, e.g. "des3" or "des-ede3-cbc".
Try "man enc" on a unix box to see what's possible.public static void main(String[] args) throws IOException, GeneralSecurityException
args
- command line arguments: [password] [cipher] [file-to-decrypt]
IOException
- problems with the [file-to-decrypt]GeneralSecurityException
- decryption problems