java.lang.Object
org.tentackle.common.Cryptor
A simple en- and decryptor.
Each application should provide a concrete instance with a confidential salt and passphase and a no-arg constructor. If provided, it is used to encrypt passwords in memory, transmission during client/server login, or decrypt passwords stored in backend.properties for database connections.
Example:
Each application should provide a concrete instance with a confidential salt and passphase and a no-arg constructor. If provided, it is used to encrypt passwords in memory, transmission during client/server login, or decrypt passwords stored in backend.properties for database connections.
Example:
@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.
Notice: the security of symmetric encryption algorithms in general depends on the confidentiality of the passphrase. Thus, the passphrase should ideally not be part of the application, but provided via some external media, a mounted USB-stick, manual input, PGP keyring, whatever. However, in practice this isn't always feasible...
-
Constructor Summary
ConstructorsConstructorDescriptionCryptor
(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.Creates a cryptor with 1024 iterations and a key strength of 256.
This is just a convenience method. -
Method Summary
Modifier and TypeMethodDescriptionEncrypts a string.
Provided for the tentackle-maven-plugin.protected SecretKeySpec
createSecretKeySpec
(byte[] key) Creates the key spec.
The default implementation returns anAES
spec.byte[]
decrypt
(byte[] encryptedData) Decrypts the data.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.char[]
decryptToChars
(byte[] encryptedData) Decrypts encrypted data to chars.Derive the unencrypted URL.
The URL is considered to be encrypted, if started with a fake protocol unsupported by the application in the given context.byte[]
encrypt
(byte[] data) Encrypts the data.byte[]
encrypt
(byte[] data, int offset, int length) Encrypts the data.byte[]
encrypt
(char[] chars) Encrypts a char array.
The method clears all traces in memory, including the passed char array.encrypt64
(byte[] data) Encrypts data to base64 encoding.encrypt64
(char[] chars) Encrypts a char array to base64 encoding.
The method clears all traces in memory, including the passed char array.Encrypts a string to base64 encoding.protected Cipher
Gets the cipher instance.
The default implementation returns anAES
cipher.static Cryptor
Gets the optional application specific cryptor singleton.static Cryptor
Gets the application specific cryptor singleton.
Throws aTentackleRuntimeException
if no cryptor defined.protected SecretKeyFactory
Gets the key factory.
The default implementation returns an instance ofPBKDF2WithHmacSHA1
.
-
Constructor Details
-
Cryptor
public Cryptor(byte[] salt, char[] passphrase, int iterations, int keyStrength) Creates a cryptor.
Notice that salt and passphrase will be scratched for security reasons.- Parameters:
salt
- the saltpassphrase
- the passphraseiterations
- number of iterations for key generationkeyStrength
- the key strength
-
Cryptor
public 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.- Parameters:
salt
- the saltpassphrase
- the passphrase
-
Cryptor
Creates a cryptor with 1024 iterations and a key strength of 256.
This is just a convenience method. Consider usingCryptor(byte[], char[])
instead.- Parameters:
salt
- the saltpassphrase
- the passphrase
-
-
Method Details
-
getInstance
Gets the optional application specific cryptor singleton.- Returns:
- the cryptor, null if no
@Service(Cryptor.class)
configured
-
getInstanceSafely
Gets the application specific cryptor singleton.
Throws aTentackleRuntimeException
if no cryptor defined.- Returns:
- the cryptor, never null
-
encrypt
public byte[] encrypt(byte[] data) Encrypts the data.- Parameters:
data
- the byte array to encrypt- Returns:
- the encrypted byte array
-
encrypt
public byte[] encrypt(byte[] data, int offset, int length) Encrypts the data.- Parameters:
data
- the byte array to encryptoffset
- the offset in datalength
- the number of bytes- Returns:
- the encrypted byte array
-
decrypt
public byte[] decrypt(byte[] encryptedData) Decrypts the data.- Parameters:
encryptedData
- the encrypted byte array- Returns:
- the decrypted data
-
encrypt64
Encrypts data to base64 encoding.- Parameters:
data
- the byte array to encrypt- Returns:
- the encrypted string in base64 encoding
-
encrypt
public byte[] encrypt(char[] chars) Encrypts a char array.
The method clears all traces in memory, including the passed char array.- Parameters:
chars
- the char array to encrypt- Returns:
- the encrypted bytes
-
encrypt64
Encrypts a char array to base64 encoding.
The method clears all traces in memory, including the passed char array.- Parameters:
chars
- the char array to encrypt- Returns:
- the encrypted string in base64 encoding
-
encrypt64
Encrypts a string to base64 encoding.- Parameters:
text
- the text to encrypt- Returns:
- the encrypted string in base64 encoding
-
decryptToChars
public char[] decryptToChars(byte[] encryptedData) Decrypts encrypted data to chars.- Parameters:
encryptedData
- the encrypted data- Returns:
- the char array
-
decrypt64ToBytes
Decrypts a base64 encoded string.- Parameters:
encryptedText
- the encrypted text in base64 encoding- Returns:
- the decrypted data
-
decrypt64ToChars
Decrypts a base64 encoded string.
The method clears all traces in memory.- Parameters:
encryptedText
- the encrypted text in base64 encoding- Returns:
- the decrypted data
-
decrypt64
Decrypts a base64 encoded string.- Parameters:
encryptedText
- the encrypted text in base64 encoding- Returns:
- the decrypted text
-
apply
Encrypts a string.
Provided for the tentackle-maven-plugin. -
deriveURL
Derive the unencrypted URL.
The URL is considered to be encrypted, if started with a fake protocol unsupported by the application in the given context. The first word after =~ is taken as the encrypted URL. Example:https://somehost.somedomain.org/login?id=~GK+AG1QIjpBaD51HP/kw9HzpdKZLt2FrInFxd1jtPWvGzaw5lcLcHy5RB/q9yEKQ&user=100
Hidden gem... ;)- Parameters:
url
- the probably encrypted URLprotocols
- the fake protocols not used by the application, such as "http:" or "https:"- Returns:
- the decrypted URL or the unchanged url, if no encryption pattern found
-
getSecretKeyFactory
Gets the key factory.
The default implementation returns an instance ofPBKDF2WithHmacSHA1
.- Returns:
- the factory
-
createSecretKeySpec
Creates the key spec.
The default implementation returns anAES
spec.- Parameters:
key
- the key- Returns:
- the spec
-
getCipher
Gets the cipher instance.
The default implementation returns anAES
cipher.- Returns:
- the cipher
-