Data is encrypted using 128 bit AES/GCM without padding. The format for the encrypted data consists of an 8 byte field containing a "salt", followed by a 12 byte field containing an initial vector, followed by the AES/GCM encrypted data. For decryption, the salt is used to reconstruct a secret key from the password, and the initial vector is used to initialize the cipher used for decryption.
Use cases include storing private keys from a key pair locally in cases where any reasonable cipher is sufficient and where performance is not an issue. This class is not meant to be a replacement for the Java APIs. For example, it would not be appropriate for encrypting a large file because the whole file would have to be read into memory before the encryption started, requiring the allocation of large arrays.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]decrypt(char[] password, byte[] data) Decrypt data given a password.static byte[]Decrypt data given a password provided as aString.static char[]decryptToChars(char[] password, byte[] data) Decrypt data representing an array of characters given a password.static char[]decryptToChars(String password, byte[] data) Decrypt data representing an array of characters given a password provided as aString.static StringdecryptToString(char[] password, byte[] data) Decrypt data representing aStringgiven a password.static StringdecryptToString(String password, byte[] data) static byte[]encrypt(char[] password, byte[] data) Encrypt data given a password.static byte[]encrypt(char[] password, char[] carray) Encrypt an array of characters given a password.static byte[]Encrypt aStringgiven a password.static byte[]Encrypt data given a password provided as aString.static byte[]Encrypt an array of characters given a password provided as a string.static byte[]static SecretKeygetKeyFromPW(char[] pw, byte[] salt)
-
Constructor Details
-
SymmetricCipher
public SymmetricCipher()
-
-
Method Details
-
getKeyFromPW
- Throws:
GeneralSecurityException
-
encrypt
Encrypt data given a password.- Parameters:
password- the passworddata- the data to encrypt- Returns:
- the encrypted data
- Throws:
GeneralSecurityException- if an error occurred
-
encrypt
Encrypt data given a password provided as aString. Normally one should not provide a password as a string as the string may persist in memory. This method is provided for convenience when an application runs for a trivial amount of time or when used with a scripting language that cannot support char arrays.- Parameters:
password- the passworddata- the data to encrypt- Returns:
- the encrypted data
- Throws:
GeneralSecurityException- if an error occurred
-
encrypt
Encrypt aStringgiven a password.- Parameters:
password- the passwordstring- the data to encrypt- Returns:
- the encrypted data
- Throws:
GeneralSecurityException- if a security error occurred
-
encrypt
Encrypt an array of characters given a password. The character data will be encoded using the UTF-8 charset.- Parameters:
password- the passwordcarray- the data to encrypt- Returns:
- the encrypted data
- Throws:
GeneralSecurityException- if a security error occurred
-
encrypt
Encrypt an array of characters given a password provided as a string. The character data will be encoded using the UTF-8 charset.- Parameters:
password- the passwordcarray- the data to encrypt- Returns:
- the encrypted data
- Throws:
GeneralSecurityException- if a security error occurred
-
encrypt
Encrypt aStringgiven a password provided as aString. Normally one should not provide a password as a string as the string may persist in memory. This method is provided for convenience when an application runs for a trivial amount of time or when used with a scripting language that cannot support char arrays. The string provided as an argument will be encoded using UTF-8 before being encrypted.- Parameters:
password- the passwordstring- the data to encrypt- Returns:
- the encrypted data
- Throws:
GeneralSecurityException- if a security error occurred
-
decrypt
Decrypt data given a password.- Parameters:
password- the passworddata- encrypted data, encrypted byencrypt(char[],byte[])- Returns:
- the decrypted data
- Throws:
GeneralSecurityException- if a security error occurred
-
decrypt
Decrypt data given a password provided as aString. Normally one should not provide a password as a string as the string may persist in memory. This method is provided for convenience when an application runs for a trivial amount of time or when used with a scripting language that cannot support char arrays but does support byte arrays.- Parameters:
password- the passworddata- encrypted data, encrypted byencrypt(char[],byte[])- Returns:
- the decrypted data
- Throws:
GeneralSecurityException- if a security error occurred
-
decryptToString
Decrypt data representing aStringgiven a password.- Parameters:
password- the passworddata- encrypted data, encrypted byencrypt(char[],byte[])- Returns:
- the decrypted string
- Throws:
GeneralSecurityException- if a security error occurred
-
decryptToString
Decrypt data representing aStringgiven a password provided as aString. Normally one should not provide a password as a string as the string may persist in memory. This method is provided for convenience when an application runs for a trivial amount of time or when used with a scripting language that cannot support char arrays but does support byte arrays.- Parameters:
password- the passworddata- encrypted data, encrypted byencrypt(char[],byte[])- Returns:
- the decrypted string
- Throws:
GeneralSecurityException- if a security error occurred
-
decryptToChars
Decrypt data representing an array of characters given a password.- Parameters:
password- the passworddata- encrypted data, encrypted byencrypt(char[],byte[])- Returns:
- the decrypted character
- Throws:
GeneralSecurityException- if a security error occurred
-
decryptToChars
Decrypt data representing an array of characters given a password provided as aString. Normally one should not provide a password as a string as the string may persist in memory. This method is provided for convenience when an application runs for a trivial amount of time or when used with a scripting language that cannot support char arrays but does support byte arrays.- Parameters:
password- the passworddata- encrypted data, encrypted byencrypt(char[],byte[])- Returns:
- the decrypted character array
- Throws:
GeneralSecurityException- if a security error occurred
-