Package overit.geocall.util
Class CryptoUtil
java.lang.Object
overit.geocall.util.CryptoUtil
This class contains a whole series of utility methods for encryption.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanIntroduces the carriage return every 76 characters to match the base64 coding standard.protected static Clockprotected booleanNormalizes the password's length to adapt it to the crypt algorithm. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaesCBCDecrypt(String key, int keyLength, String value) Decrypts the value using the "AES/CBC/PKCS5Padding" transform and the key passed as parameter.aesCBCEncrypt(String key, int keyLength, String value) Encrypts the value using the "AES/CBC/PKCS5Padding" transform and the key passed as parameter.aesDecrypt(String key, int keyLength, String value) Decrypts the value using the Advanced Encryption Standard algorithm and the key passed as parameter.static StringaesDecrypt(String key, String value) Decrypts the value using the Advanced Encryption Standard cipher algorithm and the key passed as parameter.aesECBDecrypt(String key, int keyLength, String value) Decrypts the value using the "AES/ECB,/PKCS5Padding" transform and the key passed as parameter.aesECBEncrypt(String key, int keyLength, String value) Encrypts the value using the "AES/ECB/PKCS5Padding" transform and the key passed as parameter.aesEncrypt(String key, int keyLength, String value) Encrypts the value using the Advanced Encryption Standard algorithm and the key passed as parameter.static StringaesEncrypt(String key, String value) Encrypts the value using the Advanced Encryption Standard cipher algorithm and the key passed as parameter.static StringaesKeyGenerate(int keySize) Generates the key for the Advanced Encryption Standard cipher algorithm.blowfishDecrypt(String key, int keyLength, String value) Decrypts the value using the Blowfish algorithm and the key passed as parameter.static StringblowfishDecrypt(String key, String value) Decrypts the value using the Blowfish algorithm and the key passed as parameter.blowfishEncrypt(String key, int keyLength, String value) Encrypts the value using the Blowfish algorithm and the key passed as parameter.static StringblowfishEncrypt(String key, String value) Encrypts the value using the Blowfish cipher algorithm and the key passed as parameter.static StringblowfishKeyGenerate(int keySize) Generates the key for the Blowfish cipher algorithm.protected StringReturns aStringabout the providedvalueonce decrypted, using the provided parameters.protected Stringprotected StringReturns aStringabout the providedvalueonce encrypted, using the provided parameters.protected Stringstatic StringGenerates a new authentication token for the successfully logged user.static Stringhash(DigestAlgorithm digestAlgorithm, String salt, String message) Returns a string that corresponds to the hashed message.voidsetB64Return(boolean b64Return) Sets the value of the attribute that defines if is introduced the carriage return every 76 character to match te requirements of the base 64 coding standard.voidsetCompression(boolean compression) Sets the value of the attribute that defines if is active the gzip compression.voidsetPasswordPadding(boolean pp) Sets the value of the attribute that defines if the password length will be normalized to adapt it to the crypt algorithm.static StringtripleDesDecrypt(String key, String value) Decrypts the value using the Triple DES cipher algorithm and the key passed as parameter.tripleDESDecrypt(String key, String value) Decrypts the value using the Triple DES cipher algorithm and the key passed as parameter.static StringtripleDesEncrypt(String key, String value) Encrypts the value using the Triple DES cipher algorithm and the key passed as parameter.tripleDESEncrypt(String key, String value) Encrypts the value using the Triple DES cipher algorithm and the key passed as parameter.static StringtripleDesKeyGenerate(int keySize) Generates the key for the Triple DES cipher algorithm.
-
Field Details
-
clock
-
passwordPadding
protected boolean passwordPaddingNormalizes the password's length to adapt it to the crypt algorithm. -
b64Return
protected boolean b64ReturnIntroduces the carriage return every 76 characters to match the base64 coding standard.
-
-
Constructor Details
-
CryptoUtil
public CryptoUtil()
-
-
Method Details
-
setCompression
public void setCompression(boolean compression) Sets the value of the attribute that defines if is active the gzip compression.- Parameters:
compression-trueto activate the compression,false(default value) otherwise.
-
setB64Return
public void setB64Return(boolean b64Return) Sets the value of the attribute that defines if is introduced the carriage return every 76 character to match te requirements of the base 64 coding standard.- Parameters:
b64Return-true(default value) to activate the carriage return,falseotherwise.
-
setPasswordPadding
public void setPasswordPadding(boolean pp) Sets the value of the attribute that defines if the password length will be normalized to adapt it to the crypt algorithm.- Parameters:
pp-true(default value) to activate the normalization,falseotherwise.
-
blowfishKeyGenerate
Generates the key for the Blowfish cipher algorithm.- Parameters:
keySize- The key size (from 32 to 448)- Returns:
- The key or
nullif there is an exception during the key creation. - Throws:
CryptoException- exception thrown in case of error occurred during the blowfish key generation.
-
blowfishEncrypt
Encrypts the value using the Blowfish cipher algorithm and the key passed as parameter.- Parameters:
key- The key used to encrypt.value- The value to encrypt.- Returns:
- The
Stringthat represents the encrypted value. - Throws:
CryptoException- exception thrown in case of error occurred during the encryption.
-
blowfishDecrypt
Decrypts the value using the Blowfish algorithm and the key passed as parameter.- Parameters:
key- The key used to decrypt.value- The value to decrypt.- Returns:
- The
Stringthat represents the decrypted value. - Throws:
CryptoException- exception thrown in case of error occurred during the decryption.
-
blowfishEncrypt
Encrypts the value using the Blowfish algorithm and the key passed as parameter. If required (seesetPasswordPadding(boolean)) it normalizes the key to the dimension passed as parameter.- Parameters:
key- The key used to encrypt.keyLength- the to-be-derived key length (it can be from 32 to 448).value- The value to encrypt.- Returns:
- The
Stringthat represents the encrypted value. - Throws:
CryptoException- exception thrown in case of error occurred during the encryption.
-
blowfishDecrypt
Decrypts the value using the Blowfish algorithm and the key passed as parameter. If required (seesetPasswordPadding(boolean)) it normalizes the key to the dimension passed as parameter.- Parameters:
key- The key used to decrypt.keyLength- the to-be-derived key length (it can be from 32 to 448).value- The value to decrypt.- Returns:
- The
Stringthat represents the decrypted value. - Throws:
CryptoException- exception thrown in case of error occurred during the decryption.
-
tripleDesKeyGenerate
Generates the key for the Triple DES cipher algorithm.- Parameters:
keySize- the key size. This is an algorithm-specific metric, specified in number of bits. (it can be 112 or 168).- Returns:
- The key once generated.
- Throws:
CryptoException- exception thrown in case of error occurred during the Triple DES key generation.
-
tripleDesEncrypt
Encrypts the value using the Triple DES cipher algorithm and the key passed as parameter.- Parameters:
key- The key used to encrypt.value- The value to encrypt.- Returns:
- The
Stringthat represents the encrypted value. - Throws:
CryptoException- exception thrown in case of error occurred during the encryption.
-
tripleDesDecrypt
Decrypts the value using the Triple DES cipher algorithm and the key passed as parameter.- Parameters:
key- The key used to decrypt.value- The value to decrypt.- Returns:
- The
Stringthat represents the decrypted value. - Throws:
CryptoException- exception thrown in case of error occurred during the decryption.
-
tripleDESEncrypt
Encrypts the value using the Triple DES cipher algorithm and the key passed as parameter.- Parameters:
key- The key used to encrypt.value- The value to encrypt.- Returns:
- The
Stringthat represents the encrypted value. - Throws:
CryptoException- exception thrown in case of error occurred during the encryption.
-
tripleDESDecrypt
Decrypts the value using the Triple DES cipher algorithm and the key passed as parameter.- Parameters:
key- The key used to decrypt.value- The value to decrypt.- Returns:
- The
Stringthat represents the decrypted value. - Throws:
CryptoException- exception thrown in case of error occurred during the decryption.
-
aesKeyGenerate
Generates the key for the Advanced Encryption Standard cipher algorithm.- Parameters:
keySize- the key size. This is an algorithm-specific metric, specified in number of bits. (it can be 128, 192 or 256).- Returns:
- The key once generated.
- Throws:
CryptoException- exception thrown in case of error occurred during the Advanced Encryption Standard key generation.
-
aesEncrypt
Encrypts the value using the Advanced Encryption Standard cipher algorithm and the key passed as parameter.- Parameters:
key- The key used to encrypt.value- The value to encrypt.- Returns:
- The
Stringthat represents the encrypted value. - Throws:
CryptoException- exception thrown in case of error occurred during the encryption.
-
aesDecrypt
Decrypts the value using the Advanced Encryption Standard cipher algorithm and the key passed as parameter.- Parameters:
key- The key used to decrypt.value- The value to decrypt.- Returns:
- The
Stringthat represents the decrypted value. - Throws:
CryptoException- exception thrown in case of error occurred during the decryption.
-
aesEncrypt
Encrypts the value using the Advanced Encryption Standard algorithm and the key passed as parameter. If required (seesetPasswordPadding(boolean)) it normalizes the key to the dimension passed as parameter.- Parameters:
key- The key used to encrypt.keyLength- The to-be-derived key length (it can be 128, 192 or 256).value- The value to encrypt.- Returns:
- The
Stringthat represents the encrypted value. - Throws:
CryptoException- exception thrown in case of error occurred during the encryption.
-
aesDecrypt
Decrypts the value using the Advanced Encryption Standard algorithm and the key passed as parameter. If required (seesetPasswordPadding(boolean)) it normalizes the key to the dimension passed as parameter.- Parameters:
key- The key used to decrypt.keyLength- The to-be-derived key length (it can be 128, 192 or 256).value- The value to decrypt.- Returns:
- The
Stringthat represents the decrypted value. - Throws:
CryptoException- exception thrown in case of error occurred during the decryption.
-
aesCBCEncrypt
Encrypts the value using the "AES/CBC/PKCS5Padding" transform and the key passed as parameter. If required (seesetPasswordPadding(boolean)) it normalizes the key to the dimension passed as parameter.- Parameters:
key- The key used to encrypt.keyLength- The to-be-derived key length (it can be 128, 192 or 256).value- The value to encrypt.- Returns:
- The
Stringthat represents the encrypted value. - Throws:
CryptoException- exception thrown in case of error occurred during the encryption.
-
aesCBCDecrypt
Decrypts the value using the "AES/CBC/PKCS5Padding" transform and the key passed as parameter. If required (seesetPasswordPadding(boolean)) it normalizes the key to the dimension passed as parameter.- Parameters:
key- The key used to decrypt.keyLength- The to-be-derived key length (it can be 128, 192 or 256).value- The value to decrypt.- Returns:
- The
Stringthat represents the decrypted value. - Throws:
CryptoException- exception thrown in case of error occurred during the decryption.
-
aesECBEncrypt
Encrypts the value using the "AES/ECB/PKCS5Padding" transform and the key passed as parameter. If required (seesetPasswordPadding(boolean)) it normalizes the key to the dimension passed as parameter.- Parameters:
key- The key used to encrypt.keyLength- The to-be-derived key length (it can be 128, 192 or 256).value- The value to encrypt.- Returns:
- The
Stringthat represents the encrypted value. - Throws:
CryptoException- exception thrown in case of error occurred during the decryption.
-
aesECBDecrypt
Decrypts the value using the "AES/ECB,/PKCS5Padding" transform and the key passed as parameter. If required (seesetPasswordPadding(boolean)) it normalizes the key to the dimension passed as parameter.- Parameters:
key- The key used to decrypt.keyLength- The to-be-derived key length (it can be 128, 192 or 256).value- The value to decrypt.- Returns:
- The
Stringthat represents the decrypted value. - Throws:
CryptoException- exception thrown in case of error occurred during the decryption.
-
encrypt
protected String encrypt(String algorithm, String transform, String key, String value, boolean useIV) throws CryptoException - Throws:
CryptoException
-
decrypt
protected String decrypt(String algorithm, String transform, String key, String value, boolean useIV) throws CryptoException - Throws:
CryptoException
-
encrypt
Returns aStringabout the providedvalueonce encrypted, using the provided parameters.WARNING: Legacy interface, better not use: lets you pass algorithm (e.g. "AES") to Cipher.getInstance(), so - per JSA specification - actual transform "algorithm/mode/padding" is automatically chosen as the default valid for the provider (may vary across platforms or implementations).
- Parameters:
algorithm-Stringabout the encryption algorithm to use.key-Stringabout the encryption key.value-Stringabout the value to encrypt.- Returns:
- a
Stringabout the providedvalueonce encrypted, using the provided parameters. - Throws:
CryptoException- exception thrown in case of error occurred during the encryption.
-
decrypt
Returns aStringabout the providedvalueonce decrypted, using the provided parameters.WARNING: Legacy interface, better not use: lets you pass algorithm (e.g. "AES") to Cipher.getInstance(), so - per JSA specification - actual transform "algorithm/mode/padding" is automatically chosen as the default valid for the provider (may vary across platforms or implementations).
- Parameters:
algorithm-Stringabout the decryption algorithm to use.key-Stringabout the decryption key.value-Stringabout the value to decrypt.- Returns:
- a
Stringabout the providedvalueonce decrypted, using the provided parameters. - Throws:
CryptoException- exception thrown in case of error occurred during the decryption.
-
hash
public static String hash(DigestAlgorithm digestAlgorithm, String salt, String message) throws CryptoException Returns a string that corresponds to the hashed message.- Parameters:
digestAlgorithm- The digestAlgorithm to use. It can be HEX_MD5, HEX_SHA1, HEX_SHA256, HEX_SHA512, B64_MD5, B64_SHA1, B64_SHA256, B64_SHA512, OB64_SHA1, OB64_SHA256, OB64_SHA512salt- The random string used as an additional input by the hashing mechanism.message- The message.- Returns:
- The hashed message.
- Throws:
CryptoException- exception thrown in case of unexpected digestAlgorithm.
-
generateToken
Generates a new authentication token for the successfully logged user.- Parameters:
id- The identity of the user.- Returns:
- A new authentication token for the successfully logged user.
- Throws:
CryptoException- exception thrown in case of error occurred during the hash calculation.
-