Java
Encrypting/Decrypting data with our backend SDKs may expose you to greater compliance burden because your server needs to handle plaintext data. Instead, we recommend using Relay or our Client-Side SDKs to encrypt data.
Getting started
The SDK has two ways to load your App's encryption key. Choose one before you start, since encryption works the same way after the SDK is initialized.
- API key (recommended): the SDK fetches the encryption key from the Evervault API at runtime, so there's no key material to manage. This is the default, and the steps below use it.
- Offline JSON Web Key Set (JWK Set): for isolated networks with no route to the Evervault API, initialize the SDK with a key you supply instead. See Encrypt without network access.
Install the SDK
First, let's install the Evervault SDK using either Gradle or Maven.
Initialize the SDK
Now, let's initialize the SDK using our App's ID and API key. If you don't have one yet, you can get one by creating an App in the Evervault Dashboard.
The SDK fetches your App's encryption key from the Evervault API when it initializes. To initialize it without network access instead, see Encrypt without network access.
Encrypt a string
Now that the SDK is initialized, we can encrypt a string.
Encrypt without network access
Offline initialization with a JWK Set is only available in the Java SDK.
Initializing the SDK with an API key is the recommended approach for every environment that can reach the Evervault API. The exception is an isolated network with no route to the public internet, such as a cardholder data environment. In those scenarios, the SDK can't fetch your App's encryption key when it starts, so you need to supply it with a key instead. To do this, download your App's public keys as a JWK Set from https://keys.evervault.com/<TEAM_UUID>/apps/<APP_ID>?format=jwks. Ship that document alongside your application and build the client from it.
Building a client this way encrypts without making any network calls, and comes with some trade-offs:
- You're responsible for storing the JWK Set and keeping it in sync with your App.
- Encryption is the only operation that works without network access. Decrypting, running Functions, and using Outbound Relay still call the Evervault API and need an API key.
See Evervault.withKey() and EvervaultKey.fromJwks() for the full set of overloads.
Reference
Evervault()
The SDK constructor takes two parameters; your App's ID and API key.
Parameters
Your Evervault App's ID.
Your Evervault App's API key.
Evervault.withKey()
Most integrations should use the Evervault() constructor. Only
use withKey() when the SDK runs somewhere without network access.
Builds a client that encrypts with a key you supply, rather than one fetched from the Evervault API. Read the key out of your App's JWK Set with EvervaultKey.fromJwks().
The client encrypts on the curve of the key it was given, so the kid you pick also decides the curve. Supplying an API key lets the same client decrypt and run Functions. Adding a Team UUID sets up Outbound Relay credentials without calling the Evervault API.
Setting enableOutboundRelay to true makes the SDK read its domain
configuration from the Evervault API, so a client built that way is no longer
offline. It also requires apiKey and teamUuid.
Parameters
Your Evervault App's ID.
Your Evervault App's API key. Needed for every operation other than encryption.
The key to encrypt with.
Your Evervault Team UUID. Sets up Outbound Relay credentials without calling the Evervault API, and requires an API key.
Whether to proxy HTTP requests through Outbound Relay. Requires
apiKeyandteamUuid. Defaults tofalse.
EvervaultKey.fromJwks()
Reads a public key out of a JWK Set so that Evervault.withKey() can encrypt with it. Pass the kid of the key you want, since a set can hold a key for each curve the SDK supports.
Call it without a kid when the set holds exactly one key. That overload is also the only way to read a key that carries no kid of its own.
Either form throws an EvervaultException if the JWK Set is malformed, or if no key matches the kid. It also throws an exception if the key describes a point that isn't on its stated curve. The message lists the key IDs in the set.
Parameters
The JWK Set, as JSON.
The key ID to read. Omit it only when the set holds a single key.
encrypt()
Encrypts data using Evervault Encryption. Evervault Strings can be used across all of our products.
To encrypt data using the Java SDK, simply pass a value into the evervault.encrypt() function. encrypt() will encrypt your data and return an object which is a String in case you passed a literal type like bool, String, int, float, char, byte.
The encrypted data can be stored in your database as normal and can be used with any of Evervault’s other services.
decrypt()
Decrypts the data previously encrypted with the encrypt() function or through Relay. An API key with the decrypt permission must be used to perform this operation.
Parameters
The data to decrypt.
The value type of the dat to deserialize into.
PCI Compliance
createClientSideDecryptToken()
Client Side Decrypt Tokens are versatile and short-lived tokens that frontend applications can utilise to decrypt data previously encrypted through Evervault. Client Side Decrypt Tokens are restricted to specific payloads.
By default, a Client Side Decrypt Token will live for 5 minutes into the future. The maximum time to live of the token is 10 minutes into the future.
Parameters
The payload containing encrypted data that the token will be used to decrypt.
The time the token will expire. Defaults to 5 minutes in the future.
run()
Lets you invoke an Evervault Function with a given payload. The function result will be deserialized into an instance of responseType and will be returned.
Parameters
The name of the function to invoke.
The payload to pass to the function.
The type into which the function's result will be serialized.
The request timeout defines the maximum duration the SDK will wait before aborting the function run if it has not completed.
createRunToken()
Creates a single use, time bound token (5 minutes) for invoking an Evervault Function with a given payload. Run Tokens can be used to invoke an Evervault Function client-side without providing a sensitive API Key.
Parameters
The name of the function to invoke.
Payload that the token can be used with. If not provided, a run token will be created, and the payload will not be validated when the function is executed.
Run Tokens can then be used to authenticate Function runs from the client-side.