java.lang.Object
page.codeberg.friedolyn.crypto.CipherText
- All Implemented Interfaces:
Copyable<CipherText>
Represents a secret message that has been encrypted using the
AES algorithm in
Galois/Counter Mode (GCM). The message can be
decrypted again using the same human-readable password that was used to encrypt it. Of
course, that password is not stored in the CipherText object but must be provided by the caller.- Implementation Note:
- This class is not a record, because the constructor needs to check that the provided cipher text and
initialisation vector are not
nullor empty and throw anIllegalArgumentExceptionif that's the case. While that could be done in a record as well, we cannot addthrows IllegalArgumentExceptionto the record's constructor signature, risking that callers of the constructor will not handle the exception. JavaDoc is not a sufficient replacement here!
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final @NonNull Argon2Configurationprivate final byte[]The secret message after it has been encrypted.private final byte[] -
Constructor Summary
ConstructorsConstructorDescriptionCipherText(byte[] cipherText, byte[] initialisationVector, @NonNull Argon2Configuration argon2Configuration) Constructs a newCipherTextobject with the providedmessage,initialisation vectorandArgon2 parameters. -
Method Summary
Modifier and TypeMethodDescription@NonNull CipherTextcopy()@NonNull Stringdecrypt(byte[] password) Converts the secret message from its encrypted form back to its human-readable form, using the AES algorithm in Galois/Counter Mode (GCM).booleanCompares the field's values of thisCipherTextobject to those of another object.static @NonNull CipherTextfromBase64(@NonNull String base64) static @NonNull CipherTextConverts a string representation of aCipherTextobject back to aCipherTextobject.@NonNull StringtoBase64()Converts the secret message to a string representation that contains all the information necessary toconvertit back to aCipherTextobject anddecryptthe message again.@NonNull StringtoString()Converts the secret message to a string representation that contains all the information necessary toconvertit back to aCipherTextobject anddecryptthe message again.
-
Field Details
-
cipherText
private final byte[] cipherTextThe secret message after it has been encrypted. -
initialisationVector
private final byte[] initialisationVector -
argon2Configuration
-
-
Constructor Details
-
CipherText
public CipherText(byte[] cipherText, byte[] initialisationVector, @NonNull @NonNull Argon2Configuration argon2Configuration) throws IllegalArgumentException Constructs a newCipherTextobject with the providedmessage,initialisation vectorandArgon2 parameters.- Parameters:
cipherText- The secret message after it has been encrypted.initialisationVector- The initialisation vector (nonce) that was passed to the AES in Galois/Counter Mode.argon2Configuration- The Argon2 parameters that were used to derive the AES encryption key from the user's human-readable password.- Throws:
IllegalArgumentException- If the providedmessageorinitialisation vectorisnullor empty.- See Also:
-
-
Method Details
-
decrypt
Converts the secret message from its encrypted form back to its human-readable form, using the AES algorithm in Galois/Counter Mode (GCM).- Parameters:
password- The human-readable password that the user entered to encrypt the message. Will be used to derive the actual encryption key using the Argon2 algorithm with the exact sameparametersthat were used to encrypt the message.- Returns:
- The human-readable message that was previously encrypted.
- Throws:
RuntimeException- If the provided password is not a valid AES password, if the provided password does not match the password that was used to encrypt the message or if an unexpected error occurs during the decryption process.
-
toString
Converts the secret message to a string representation that contains all the information necessary toconvertit back to aCipherTextobject anddecryptthe message again. The string representation is in the format$argon2id$v=[version]$m=[memory],t=[iterations],p=[parallelism]$[salt]$[iv]$[ciphertext]where[version]isArgon2Version.version[memory]isinvalid reference
Argon2Configuration#getMemory()[iterations]isinvalid reference
Argon2Configuration#getIterations()[parallelism]isinvalid reference
Argon2Configuration#getParallelism()[salt]is the base64-encoded.invalid reference
salt[iv]is the base64-encodedinitialisation vector.[ciphertext]is the base64-encodedencrypted message.
-
toBase64
Converts the secret message to a string representation that contains all the information necessary toconvertit back to aCipherTextobject anddecryptthe message again. The string representation is in the format$base64$base64$base64$base64$[iv]$[ciphertext]where- The string
argon2idin Base64 v=[version]where[version]isArgon2Version.version-
m=[memory],t=[iterations],p=[parallelism]with,invalid reference
Argon2Configuration#getMemory()andinvalid reference
Argon2Configuration#getIterations()invalid reference
Argon2Configuration#getParallelism() [salt]is the base64-encoded.invalid reference
salt[iv]is the base64-encodedinitialisation vector.[ciphertext]is the base64-encodedencrypted message.
- Returns:
- The cipher text as a string with all necessary information to decrypt it again.
- See Also:
- The string
-
copy
- Specified by:
copyin interfaceCopyable<CipherText>- Returns:
- A deep copy of the object, i.e. a clone that has the exact same values as the original object, but is a different instance (new reference).
-
equals
Compares the field's values of thisCipherTextobject to those of another object.- Overrides:
equalsin classObject- Parameters:
object- The object to compare thisCipherTextobject to.- Returns:
-
trueif the provided object is aCipherTextobject and all fields of the givenCipherTextobject match those of thisCipherTextobject, especially (but not exclusively) if both objects are the same reference. -
falseif the provided object is not aCipherTextobject or if any field of the givenCipherTextobject does not match the corresponding field of thisCipherText.
-
-
parse
@NonNull public static @NonNull CipherText parse(@NonNull @NonNull String cipherText) throws IllegalArgumentException Converts a string representation of aCipherTextobject back to aCipherTextobject.- Parameters:
cipherText- The string representation of aCipherTextobject, as returned bytoString(), i.e. in the format$argon2id$v=[version]$m=[memory],t=[iterations],p=[parallelism]$[salt]$[iv]$[ciphertext]where[version]isArgon2Version.version[memory]isinvalid reference
Argon2Configuration#getMemory()[iterations]isinvalid reference
Argon2Configuration#getIterations()[parallelism]isinvalid reference
Argon2Configuration#getParallelism()-
[salt]is the base64-encoded.invalid reference
salt -
[iv]is the base64-encodedinitialisation vector. -
[ciphertext]is the base64-encodedencrypted message.
- Returns:
- The
CipherTextobject that was represented by the provided string. - Throws:
IllegalArgumentException- If the provided string is not a validCipherTextstring representation or if theArgon2 configurationcannot beparsed.- See Also:
-
fromBase64
@NonNull public static @NonNull CipherText fromBase64(@NonNull @NonNull String base64) throws IllegalArgumentException - Parameters:
base64- The string representation of aCipherTextobject, as returned bytoBase64(), i.e. in the format$base64$base64$base64$base64$[iv]$[ciphertext]where- The string
argon2idin Base64 -
v=[version]where[version]isArgon2Version.version -
m=[memory],t=[iterations],p=[parallelism]with,invalid reference
Argon2Configuration#getMemory()andinvalid reference
Argon2Configuration#getIterations()invalid reference
Argon2Configuration#getParallelism() [salt]is the base64-encoded.invalid reference
salt-
[iv]is the base64-encodedinitialisation vector. [ciphertext]is the base64-encodedencrypted message.
- The string
- Returns:
- The
CipherTextobject that was represented by the provided string. - Throws:
IllegalArgumentException- If the provided string is not a validCipherTextstring representation or if theArgon2 configurationcannot beparsed.- See Also:
-