Class SecretBox
- java.lang.Object
-
- com.codahale.xsalsa20poly1305.SecretBox
-
public class SecretBox extends java.lang.ObjectEncryption and decryption using XSalsa20Poly1305.Compatible with NaCl's
boxandsecretboxconstructions.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]nonce()Generates a random nonce.byte[]nonce(byte[] message)Generates a random nonce which is guaranteed to be unique even if the process's PRNG is exhausted or compromised.java.util.Optional<byte[]>open(byte[] nonce, byte[] ciphertext)Decrypt a ciphertext using the given key and nonce.byte[]seal(byte[] nonce, byte[] plaintext)Encrypt a plaintext using the given key and nonce.
-
-
-
Constructor Detail
-
SecretBox
public SecretBox(byte[] secretKey)
Create a newSecretBoxinstance with the given secret key.- Parameters:
secretKey- a 32-byte secret key- See Also:
Keys.generateSecretKey()
-
SecretBox
public SecretBox(byte[] publicKey, byte[] privateKey)Create a newSecretBoxinstance given a Curve25519 public key and a Curve25519 private key.- Parameters:
publicKey- a Curve25519 public keyprivateKey- a Curve25519 private key- See Also:
Keys.generatePrivateKey(),Keys.generatePublicKey(byte[])
-
-
Method Detail
-
seal
public byte[] seal(byte[] nonce, byte[] plaintext)Encrypt a plaintext using the given key and nonce.- Parameters:
nonce- a 24-byte nonce (cf.nonce(byte[]),nonce())plaintext- an arbitrary message- Returns:
- the ciphertext
-
open
public java.util.Optional<byte[]> open(byte[] nonce, byte[] ciphertext)Decrypt a ciphertext using the given key and nonce.- Parameters:
nonce- a 24-byte nonceciphertext- the encrypted message- Returns:
- an
Optionalof the original plaintext, or if either the key, nonce, or ciphertext was modified, an emptyOptional - See Also:
nonce(byte[]),nonce()
-
nonce
public byte[] nonce()
Generates a random nonce.N.B.: Use of this method is probably fine, but because an entropy-exhausted or compromised
SecureRandomprovider might generate duplicate nonces (which would allow an attacker to potentially decrypt and even forge messages),nonce(byte[])is recommended instead.- Returns:
- a 24-byte nonce
-
nonce
public byte[] nonce(byte[] message)
Generates a random nonce which is guaranteed to be unique even if the process's PRNG is exhausted or compromised.Internally, this creates a Blake2b instance with the given key, a random 16-byte salt, and a random 16-byte personalization tag. It then hashes the message and returns the resulting 24-byte digest as the nonce.
In the event of a broken or entropy-exhausted
SecureRandomprovider, the nonce is essentially equivalent to a synthetic IV and should be unique for any given key/message pair. The result will be deterministic, which will allow attackers to detect duplicate messages.In the event of a compromised
SecureRandomprovider, the attacker would need a complete second-preimage attack against Blake2b in order to produce colliding nonces.- Parameters:
message- the message to be encrypted- Returns:
- a 24-byte nonce
-
-