Class SecretBox


  • public class SecretBox
    extends java.lang.Object
    Encryption and decryption using XSalsa20Poly1305.

    Compatible with NaCl's box and secretbox constructions.

    • Constructor Summary

      Constructors 
      Constructor Description
      SecretBox​(byte[] secretKey)
      Create a new SecretBox instance with the given secret key.
      SecretBox​(byte[] publicKey, byte[] privateKey)
      Create a new SecretBox instance given a Curve25519 public key and a Curve25519 private key.
    • 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SecretBox

        public SecretBox​(byte[] secretKey)
        Create a new SecretBox instance with the given secret key.
        Parameters:
        secretKey - a 32-byte secret key
        See Also:
        Keys.generateSecretKey()
    • 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 nonce
        ciphertext - the encrypted message
        Returns:
        an Optional of the original plaintext, or if either the key, nonce, or ciphertext was modified, an empty Optional
        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 SecureRandom provider 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 SecureRandom provider, 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 SecureRandom provider, 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