public class JCryptoBox extends Object
Boxes uses authenticated encryption (AES-GCM) which relies on two input parameters: a secret key and a nonce. Secret keys are automatically generated based on the given sender and recipient public keys. Public keys and their corresponding private keys form a keypair where the public keys can be safely shared with others while the private key should be kept safe.
The nonce parameter is an accompanying value to the secret key used in encrypting or decrypting a single message/packet. A nonce must not be reused with the same key! Use of a nonce can either be sequential numbers (useful in contexts where stable increasing numbers are guaranteed) or random byte strings. While a nonce can be of arbitrary length, the effective size of a GCM nonce is 12 bytes as longer values are hashed into a 12 byte value.
JCryptoBox.Seal,
JCryptoBox.Unseal| Modifier and Type | Class and Description |
|---|---|
static class |
JCryptoBox.Seal
Sealed boxes provide the ability for an anonymous sender to encrypt a message to a known recipient given their
public key.
|
static class |
JCryptoBox.Unseal
Provides functionality to unseal a sealed box.
|
| Modifier and Type | Field and Description |
|---|---|
static int |
TAG_LENGTH
Number of bytes needed to store the authentication tag at the end of a box.
|
| Modifier and Type | Method and Description |
|---|---|
byte[] |
box(byte[] nonce,
byte[] message)
Boxes the given message bytes and returns the boxed message.
|
byte[] |
box(byte[] nonce,
byte[] message,
int offset,
int length)
Boxes the given message slice and returns the boxed message.
|
void |
box(byte[] nonce,
byte[] input,
int inOffset,
int inLength,
byte[] output,
int outOffset)
Boxes the given slice of input data into the given output array at the given offset.
|
static JCryptoBox |
boxing(KeyPair senderKeyPair,
PublicKey recipientKey)
Initializes a box to box data from the provided sender to the provided recipient.
|
static PrivateKey |
decodePrivateKey(byte[] encodedPrivateKey)
Decodes an encoded private key.
|
static PublicKey |
decodePublicKey(byte[] encodedPublicKey)
Decodes an encoded public key.
|
static byte[] |
encodeKey(Key key)
Encodes a key into its default encoded format.
|
static KeyPair |
generateKeyPair()
Generates a random public and private keypair.
|
byte[] |
open(byte[] nonce,
byte[] box)
Opens the given boxed message and returns the decrypted message.
|
byte[] |
open(byte[] nonce,
byte[] box,
int offset,
int length)
Opens the given boxed message slice and returns the decrypted message.
|
void |
open(byte[] nonce,
byte[] input,
int inOffset,
int inLength,
byte[] output,
int outOffset)
Opens the given boxed input slice and writes the decrypted data to the given output array at the given offset.
|
static JCryptoBox |
opening(KeyPair recipientKeyPair,
PublicKey senderKey)
Initializes a box to open data from the provided sender to the provided recipient.
|
static JCryptoBox.Seal |
sealing(PublicKey recipient)
Creates a box to seal to the provided recipient key for creating sealed boxes.
|
static JCryptoBox.Unseal |
unsealing(KeyPair recipient)
Creates a box to unseal from the provided recipient keypair for decrypting sealed boxes sent to the recipient.
|
public static final int TAG_LENGTH
public void box(byte[] nonce,
byte[] input,
int inOffset,
int inLength,
byte[] output,
int outOffset)
nonce - nonce to use to encrypt the input datainput - array of bytes to read data to encryptinOffset - where in the input array to begin reading datainLength - how many bytes to read and encryptoutput - array of bytes to write encrypted data tooutOffset - where in the output array to begin writing dataIllegalArgumentException - if the output buffer is too smallNullPointerException - if any arrays are nullpublic byte[] box(byte[] nonce,
byte[] message,
int offset,
int length)
nonce - nonce to use to encrypt the input messagemessage - array of bytes to read data to encryptoffset - where in the message array to begin reading data to encryptlength - how many bytes to read and encryptNullPointerException - if any arrays are nullpublic byte[] box(byte[] nonce,
byte[] message)
nonce - nonce to use to encrypt the input messagemessage - array of bytes to encryptNullPointerException - if any args are nullpublic void open(byte[] nonce,
byte[] input,
int inOffset,
int inLength,
byte[] output,
int outOffset)
nonce - nonce used to encrypt the boxed messageinput - array of bytes to read boxed data to decryptinOffset - where in the input array to begin reading data to decryptinLength - length of the boxed message in bytes (includes authentication tag)output - array of bytes to write decrypted message tooutOffset - where in the output array to begin writing decrypted dataIllegalArgumentException - if the boxed data cannot be successfully authenticated and decrypted or if the
output buffer is too smallNullPointerException - if any arrays are nullpublic byte[] open(byte[] nonce,
byte[] box,
int offset,
int length)
nonce - nonce used to encrypt the boxed messagebox - array of bytes to read boxed data to decryptoffset - where in the input array to begin reading data to decryptlength - length of the boxed message in bytes (includes authentication tag)IllegalArgumentException - if the boxed data cannot be successfully authenticated and decryptedNullPointerException - if any arrays are nullpublic byte[] open(byte[] nonce,
byte[] box)
nonce - nonce used to encrypt the boxed messagebox - array of boxed data to decryptIllegalArgumentException - if the boxed data cannot be successfully authenticated and decryptedNullPointerException - if any args are nullpublic static JCryptoBox boxing(KeyPair senderKeyPair, PublicKey recipientKey)
senderKeyPair - keypair of the principal sending the boxed datarecipientKey - public key of the principal opening the boxed dataNullPointerException - if any args are nullopening(KeyPair, PublicKey)public static JCryptoBox opening(KeyPair recipientKeyPair, PublicKey senderKey)
recipientKeyPair - keypair of the principal opening the boxed datasenderKey - public key of the principal who sent the boxed dataNullPointerException - if any args are nullboxing(KeyPair, PublicKey)public static JCryptoBox.Seal sealing(PublicKey recipient)
recipient - public key of the principal receiving the sealed boxNullPointerException - if the provided key is nullunsealing(KeyPair)public static JCryptoBox.Unseal unsealing(KeyPair recipient)
recipient - keypair of recipient of sealed boxesNullPointerException - if the provided keypair is nullsealing(PublicKey)public static KeyPair generateKeyPair()
public static byte[] encodeKey(Key key)
X509EncodedKeySpec, while private keys are formatted for PKCS8EncodedKeySpec.key - key to encodeNullPointerException - if the provided key is nullpublic static PublicKey decodePublicKey(byte[] encodedPublicKey)
encodedPublicKey - encoded key data to parse and decodeIllegalArgumentException - if the provided public key data is invalidNullPointerException - if the provided key is nullpublic static PrivateKey decodePrivateKey(byte[] encodedPrivateKey)
encodedPrivateKey - encoded key data to parse and decodeIllegalArgumentException - if the provided private key data is invalidNullPointerException - if the provided key is nullCopyright © 2021. All rights reserved.