Class EncryptionOperations
Public facade for encryption/decryption operations.
All methods are exposed via AuthenticatedSdk.
-
Constructor Summary
ConstructorsConstructorDescriptionEncryptionOperations(InternalServiceFactory svc, TokenSource tokenSource, BooleanSupplier timingMetricsEnabled, org.slf4j.Logger userLogger) -
Method Summary
Modifier and TypeMethodDescriptionDecrypts a JWE token string, Compact or General JSON.decryptFileCompact(String recipientKid, Path input, Path output) Decrypts a file containing a JWE token, Compact or General JSON.decryptFileStream(String recipientKid, Path input, Path output) Decrypts a file in streaming mode (designed for large files >100 MB).Encrypts raw bytes using the key identified bykid.encryptFileCompact(String kid, Path input, Path output) Encrypts a file using the key identified bykid.encryptFileStream(String kid, Path input, Path output) Encrypts a file in streaming mode (designed for large files >100 MB).
-
Constructor Details
-
EncryptionOperations
public EncryptionOperations(InternalServiceFactory svc, TokenSource tokenSource, BooleanSupplier timingMetricsEnabled, org.slf4j.Logger userLogger)
-
-
Method Details
-
encrypt
Encrypts raw bytes using the key identified bykid.The key type determines the algorithm automatically: asymmetric keys (ML-KEM, RSA, ECC) perform key encapsulation + symmetric encryption; symmetric keys (AES) encrypt directly. The result is a JWE string containing the ciphertext and all metadata needed for decryption: Compact (RFC 7516 §7.1) for a single-recipient key, General JSON (§7.2) for a composite KEM key, whose one recipient per component Compact Serialization cannot hold.
This method is suitable for in-memory payloads up to ~100 MB. For larger files, use
encryptFileStream(String, Path, Path)to avoid loading the entire payload into memory.- Parameters:
kid- key identifier referencing a key stored in ANKASecure©; must not benullplaintext- the raw bytes to encrypt; must not benull- Returns:
- encryption result containing the JWE token and operation metadata;
never
null - Throws:
AnkaSecureSdkException- if the key identified bykiddoes not exist, does not support encryption, the authenticated client lacks permission, or the server returns an errorNullPointerException- ifkidorplaintextisnull
-
encryptFileCompact
public EncryptResult encryptFileCompact(String kid, Path input, Path output) throws AnkaSecureSdkException Encrypts a file using the key identified bykid.The entire file is loaded into memory, encrypted, and the resulting JWE is written to the output file (Compact (RFC 7516 §7.1) for a single-recipient key, General JSON (§7.2) for a composite KEM key, whose one recipient per component Compact Serialization cannot hold). This method is suitable for files up to ~100 MB. For larger files, use
encryptFileStream(String, Path, Path)which processes files in chunks without loading them entirely into memory.The output file contains a complete single-line JWE string, in either serialization, that can be decrypted using
decryptFile(Path, Path).- Parameters:
kid- key identifier referencing a key stored in ANKASecure©; must not benullinput- path to plaintext file; must not benulland file must existoutput- path where JWE will be written; must not benull- Returns:
- encryption result containing the JWE token and operation metadata;
never
null - Throws:
AnkaSecureSdkException- if the key identified bykiddoes not exist, does not support encryption, the input file cannot be read, the output file cannot be written, or the server returns an errorNullPointerException- ifkid,input, oroutputisnull
-
encryptFileStream
public EncryptResult encryptFileStream(String kid, Path input, Path output) throws AnkaSecureSdkException Encrypts a file in streaming mode (designed for large files >100 MB).Streaming variant that processes the file in chunks using multipart upload, minimizing memory consumption. Unlike
encryptFile(String, Path, Path), this method produces a detached JWE (General-JSON header + raw ciphertext stream) instead of a compact JWE string.The detached format separates the JWE header from the ciphertext payload, enabling efficient streaming decryption of large files using
decryptFileStream(Path, Path).- Parameters:
kid- key identifier referencing a key stored in ANKASecure©; must not benullinput- path to plaintext file; must not benulland file must existoutput- path where encrypted file will be written (detached JWE format); must not benull- Returns:
- encryption result with operation metadata (JWE token field may be
nullin streaming mode); nevernull - Throws:
AnkaSecureSdkException- if the key identified bykiddoes not exist, does not support encryption, the input file cannot be read, the output file cannot be written, or the server returns an errorNullPointerException- ifkid,input, oroutputisnull
-
decrypt
public DecryptResult decrypt(String recipientKid, String ciphertextJwe) throws AnkaSecureSdkException Decrypts a JWE token string, Compact or General JSON.Extracts the key identifier from the JWE header, retrieves the corresponding private key, and decrypts the ciphertext using the appropriate algorithm. Supports both asymmetric (ML-KEM, RSA, ECC) and symmetric (AES) encryption.
This method is the inverse of
encrypt(String, byte[]). The JWE token must be a valid RFC 7516 JWE as produced by ANKASecure© encryption operations: Compact (§7.1) or General JSON (§7.2), detected from the content.- Parameters:
recipientKid- the recipient of the JWE to decrypt with, ornullwhen the JWE names one key; required by the server when the JWE is addressed to several keys (HTTP 400 namingrecipientKidotherwise)ciphertextJwe- the JWE token string: Compact (five Base64URL segments separated by dots) or General JSON (a JSON object); must not benull- Returns:
- decryption result containing the plaintext bytes and operation metadata;
never
null - Throws:
AnkaSecureSdkException- if the JWE is malformed, the key does not exist, the authenticated client lacks permission to use the key, or decryption fails due to tamperingNullPointerException- ifciphertextJweisnull
-
decryptFileCompact
public DecryptResultMetadata decryptFileCompact(String recipientKid, Path input, Path output) throws AnkaSecureSdkException Decrypts a file containing a JWE token, Compact or General JSON.Reads the entire JWE file, decrypts its contents, and writes the plaintext to the output file. This method is the inverse of
encryptFile(String, Path, Path)and is suitable for files up to ~100 MB. For larger files, usedecryptFileStream(Path, Path).The input file must contain a valid JWE string as produced by ANKASecure© encryption operations: Compact (RFC 7516 §7.1) or General JSON (§7.2, written for a composite KEM key), detected from the content.
- Parameters:
recipientKid- the recipient of the JWE to decrypt with, ornullwhen the JWE names one key; required by the server when the JWE is addressed to several keys (HTTP 400 namingrecipientKidotherwise)input- path to file containing a Compact or General JSON JWE token; must not benulland file must existoutput- path where plaintext will be written; must not benull- Returns:
- decryption metadata including key used, algorithm, and warnings;
never
null - Throws:
AnkaSecureSdkException- if the input file cannot be read, the JWE is malformed, the key does not exist, the authenticated client lacks permission, decryption fails, or the output file cannot be writtenNullPointerException- ifinputoroutputisnull
-
decryptFileStream
public DecryptResultMetadata decryptFileStream(String recipientKid, Path input, Path output) throws AnkaSecureSdkException Decrypts a file in streaming mode (designed for large files >100 MB).Streaming variant that processes the file in chunks using multipart download, minimizing memory consumption. This method is the inverse of
encryptFileStream(String, Path, Path)and expects a detached JWE format (General-JSON header + raw ciphertext stream).Unlike
decryptFile(Path, Path), this method does not load the entire encrypted file into memory, making it suitable for very large files.- Parameters:
recipientKid- the recipient of the JWE to decrypt with, ornullwhen the JWE names one key; required by the server when the JWE is addressed to several keys (HTTP 400 namingrecipientKidotherwise)input- path to encrypted file (detached JWE format); must not benulland file must existoutput- path where plaintext will be written; must not benull- Returns:
- decryption metadata including key used, algorithm, and warnings;
never
null - Throws:
AnkaSecureSdkException- if the input file cannot be read, the JWE is malformed, the key does not exist, the authenticated client lacks permission, decryption fails, or the output file cannot be writtenNullPointerException- ifinputoroutputisnull
-