Package dev.brachtendorf
Class CryptoUtil
- java.lang.Object
-
- dev.brachtendorf.CryptoUtil
-
public class CryptoUtil extends Object
Crypto utilities used to encrypt and decrypt data.NO WARRANTY or liability that the functions are used correctly. If you work with highly sensitive data look for someone who actually knows their way around cryptographic code.
- Author:
- Kilian
-
-
Constructor Summary
Constructors Constructor Description CryptoUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Stringdecrypt(SecretKey key, String encrypted)static SecretKeyderiveKey(char[] password)Derives a 128 bit AES key from a given password useful for further encryption usage.static SecretKeyderiveKey(char[] password, byte[] salt, int keyLength)Derives an AES key from a given password useful for further encryption usage.static StringencryptAES(SecretKey key, String text)Encrypt a given string with AES/CBC/PKCS5PADDING cipher.
-
-
-
Method Detail
-
deriveKey
public static SecretKey deriveKey(char[] password, byte[] salt, int keyLength) throws InvalidKeySpecException
Derives an AES key from a given password useful for further encryption usage. Key derivation is needed due to AES requiring an x bit key from a short password sequence- Parameters:
password- User defined passwordsalt- Salt added to the passwordkeyLength- bit key length. Has to be a 2 complement 64 128 256 525 ...- Returns:
- A secret key used for encryption and decryption operations
- Throws:
InvalidKeySpecException- if the given key specificationis inappropriate for this secret-key- Since:
- 1.0.0 com.github.kilianB
- See Also:
- Based on https://stackoverflow.com/questions/992019/java-256-bit-aes-password-based-encryption/992413
-
deriveKey
public static SecretKey deriveKey(char[] password)
Derives a 128 bit AES key from a given password useful for further encryption usage.- Parameters:
password- The password used to create the key- Returns:
- A secret key used for encryption and decryption operations
- Since:
- 1.0.0 com.github.kilianB
- See Also:
- Based on https://stackoverflow.com/questions/992019/java-256-bit-aes-password-based-encryption/992413
-
encryptAES
public static String encryptAES(SecretKey key, String text) throws Exception
Encrypt a given string with AES/CBC/PKCS5PADDING cipher. The returned string is base64 encoded. The first 16 bytes are holds the init vector enabling persistent storage and reconstruction at a later stage.- Parameters:
key- they key used to encrypt the datatext- the data t encrypt- Returns:
- the encrypted text with the init vector encoded as base64 string
- Throws:
Exception- if an exception occurs- Since:
- 1.0.0 com.github.kilianB
- See Also:
- Partially based on: https://stackoverflow.com/questions/15554296/simple-java-aes-encrypt-decrypt-example
-
decrypt
public static String decrypt(SecretKey key, String encrypted) throws Exception
- Parameters:
key- used to decrypt the data. A key with the same settings during encryption should be used to retrieve useable resultsencrypted- the base64 representation of the encrypted text as returned byencryptAES(SecretKey, String)- Returns:
- the decrypted text
- Throws:
Exception- if an Exception occurs- Since:
- 1.0.0 com.github.kilianB
-
-