public class Cryptor extends Object implements Function<String,String>
@Service(Cryptor.class)
public class MyCryptor extends Cryptor {
public MyCryptor() {
...
}
}
Cryptor also implements a Function<String,String> to encrypt strings like passwords to base64
and thus can directly be used by the tentackle-maven-plugin to generate properties for filtered resources.
IMPORTANT: at least the passphrase should *NOT* be part of the application! If an attacker gets access to the
jars, it's a no-brainer to write a small java program that happily decrypts all your data. Instead, provide the
passphrase via some external media, a mounted USB-stick, manual input, PGP keyring, whatever.
For a secure client/server communication, it's better to use TLS/SSL anyway.
| Constructor and Description |
|---|
Cryptor(byte[] salt,
char[] passphrase)
Creates a cryptor with 1024 iterations and a key strength of 256.
Notice that salt and passphrase will be scratched for security reasons. |
Cryptor(byte[] salt,
char[] passphrase,
int iterations,
int keyStrength)
Creates a cryptor.
Notice that salt and passphrase will be scratched for security reasons. |
Cryptor(String salt,
String passphrase)
Creates a cryptor with 1024 iterations and a key strength of 256.
This is just a convenience method. |
| Modifier and Type | Method and Description |
|---|---|
String |
apply(String s)
Encrypts a string.
Provided for the tentackle-maven-plugin. |
protected SecretKeySpec |
createSecretKeySpec(byte[] key)
Creates the key spec.
The default implementation returns an AES spec. |
byte[] |
decrypt(byte[] encryptedData)
Decrypts the data.
|
String |
decrypt64(String encryptedText)
Decrypts a base64 encoded string.
|
byte[] |
decrypt64ToBytes(String encryptedText)
Decrypts a base64 encoded string.
|
char[] |
decrypt64ToChars(String encryptedText)
Decrypts a base64 encoded string.
The method clears all traces in memory. |
byte[] |
encrypt(byte[] data)
Encrypts the data.
|
byte[] |
encrypt(byte[] data,
int offset,
int length)
Encrypts the data.
|
String |
encrypt64(byte[] data)
Encrypts data to base64 encoding.
|
String |
encrypt64(char[] chars)
Encrypts a char array to base64 encoding.
The method clears all traces in memory, including the passed char array. |
String |
encrypt64(String text)
Encrypts a string to base64 encoding.
|
protected Cipher |
getCipher()
Gets the cipher instance.
The default implementation returns an AES cipher. |
static Cryptor |
getInstance()
Gets the optional application specific cryptor singleton.
|
protected SecretKeyFactory |
getSecretKeyFactory()
Gets the key factory.
The default implementation returns an instance of PBKDF2WithHmacSHA1. |
public Cryptor(byte[] salt,
char[] passphrase,
int iterations,
int keyStrength)
salt - the saltpassphrase - the passphraseiterations - number of iterations for key generationkeyStrength - the key strengthpublic Cryptor(byte[] salt,
char[] passphrase)
salt - the saltpassphrase - the passphrasepublic Cryptor(String salt, String passphrase)
Cryptor(byte[], char[]) instead.salt - the saltpassphrase - the passphrasepublic static Cryptor getInstance()
@Service(Cryptor.class) configuredpublic byte[] encrypt(byte[] data)
data - the byte array to encryptpublic byte[] encrypt(byte[] data,
int offset,
int length)
data - the byte array to encryptoffset - the offset in datalength - the number of bytespublic byte[] decrypt(byte[] encryptedData)
encryptedData - the encrypted byte arraypublic String encrypt64(byte[] data)
data - the byte array to encryptpublic String encrypt64(char[] chars)
chars - the char array to encryptpublic String encrypt64(String text)
text - the text to encryptpublic byte[] decrypt64ToBytes(String encryptedText)
encryptedText - the encrypted text in base64 encodingpublic char[] decrypt64ToChars(String encryptedText)
encryptedText - the encrypted text in base64 encodingpublic String decrypt64(String encryptedText)
encryptedText - the encrypted text in base64 encodingprotected SecretKeyFactory getSecretKeyFactory()
PBKDF2WithHmacSHA1.protected SecretKeySpec createSecretKeySpec(byte[] key)
AES spec.key - the keyprotected Cipher getCipher()
AES cipher.Tentackle - distributed, domain- and model-driven