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
null
or empty and throw anIllegalArgumentException
if that's the case. While that could be done in a record as well, we cannot addthrows IllegalArgumentException
to 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 Argon2Configuration
private 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 newCipherText
object with the providedmessage
,initialisation vector
andArgon2 parameters
. -
Method Summary
Modifier and TypeMethodDescription@NonNull CipherText
copy()
@NonNull String
decrypt
(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).boolean
Compares the field's values of thisCipherText
object to those of another object.static @NonNull CipherText
fromBase64
(@NonNull String base64) static @NonNull CipherText
Converts a string representation of aCipherText
object back to aCipherText
object.@NonNull String
toBase64()
Converts the secret message to a string representation that contains all the information necessary toconvert
it back to aCipherText
object anddecrypt
the message again.@NonNull String
toString()
Converts the secret message to a string representation that contains all the information necessary toconvert
it back to aCipherText
object anddecrypt
the 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 newCipherText
object with the providedmessage
,initialisation vector
andArgon2 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 providedmessage
orinitialisation vector
isnull
or 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 sameparameters
that 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 toconvert
it back to aCipherText
object anddecrypt
the 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-encodedinvalid 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 toconvert
it back to aCipherText
object anddecrypt
the message again. The string representation is in the format$base64$base64$base64$base64$[iv]$[ciphertext]
where- The string
argon2id
in Base64 v=[version]
where[version]
isArgon2Version.version
-
m=[memory],t=[iterations],p=[parallelism]
withinvalid reference
Argon2Configuration#getMemory()
invalid reference
Argon2Configuration#getIterations()
invalid reference
Argon2Configuration#getParallelism()
[salt]
is the base64-encodedinvalid 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:
copy
in 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 thisCipherText
object to those of another object.- Overrides:
equals
in classObject
- Parameters:
object
- The object to compare thisCipherText
object to.- Returns:
-
true
if the provided object is aCipherText
object and all fields of the givenCipherText
object match those of thisCipherText
object, especially (but not exclusively) if both objects are the same reference. -
false
if the provided object is not aCipherText
object or if any field of the givenCipherText
object 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 aCipherText
object back to aCipherText
object.- Parameters:
cipherText
- The string representation of aCipherText
object, 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-encodedinvalid reference
salt
-
[iv]
is the base64-encodedinitialisation vector
. -
[ciphertext]
is the base64-encodedencrypted message
.
- Returns:
- The
CipherText
object that was represented by the provided string. - Throws:
IllegalArgumentException
- If the provided string is not a validCipherText
string representation or if theArgon2 configuration
cannot beparsed
.- See Also:
-
fromBase64
@NonNull public static @NonNull CipherText fromBase64(@NonNull @NonNull String base64) throws IllegalArgumentException - Parameters:
base64
- The string representation of aCipherText
object, as returned bytoBase64()
, i.e. in the format$base64$base64$base64$base64$[iv]$[ciphertext]
where- The string
argon2id
in Base64 -
v=[version]
where[version]
isArgon2Version.version
-
m=[memory],t=[iterations],p=[parallelism]
withinvalid reference
Argon2Configuration#getMemory()
invalid reference
Argon2Configuration#getIterations()
invalid reference
Argon2Configuration#getParallelism()
[salt]
is the base64-encodedinvalid reference
salt
-
[iv]
is the base64-encodedinitialisation vector
. [ciphertext]
is the base64-encodedencrypted message
.
- The string
- Returns:
- The
CipherText
object that was represented by the provided string. - Throws:
IllegalArgumentException
- If the provided string is not a validCipherText
string representation or if theArgon2 configuration
cannot beparsed
.- See Also:
-