Skip to main content

Overview

The EncryptService provides high-level encryption and decryption operations for strings, bytes, and files. It handles data encryption using symmetric keys, key wrapping, and key encapsulation mechanisms.

Location

Interface

String Encryption

encryptString()

Encrypts a string to an EncString.
Parameters:
  • plainValue - The string to encrypt
  • key - The symmetric key to encrypt with
Returns: Promise<EncString> - Encrypted string
For new use-cases, prefer using the DataEnvelope inside the SDK instead. This is both safer and more maintainable.
Example:

decryptString()

Decrypts an EncString to a plaintext string.
Parameters:
  • encString - The encrypted string
  • key - The symmetric key to decrypt with
Returns: Promise<string> - Decrypted plaintext string Throws: Error if decryption fails
This throws if decryption fails. If decryption failures are expected, the callsite should log where the failure occurred and handle it with domain-specific logic (e.g., show a UI error).
Example:

Byte Array Encryption

encryptBytes()

Encrypts a byte array to an EncString.
Parameters:
  • plainValue - The bytes to encrypt
  • key - The symmetric key to encrypt with
Returns: Promise<EncString> - Encrypted bytes as an EncString
Deprecated. Bytes are not the right abstraction to encrypt in. Use key wrapping or file encryption instead. Contact the Key-Management team if you think you need this.

decryptBytes()

Decrypts an EncString to a byte array.
Parameters:
  • encString - The encrypted string containing bytes
  • key - The symmetric key to decrypt with
Returns: Promise<Uint8Array> - Decrypted bytes Throws: Error if decryption fails
Deprecated. Use key wrapping or file encryption instead.

File Encryption

encryptFileData()

Encrypts file data to an EncArrayBuffer.
Parameters:
  • plainValue - The file data to encrypt
  • key - The symmetric key to encrypt with
Returns: Promise<EncArrayBuffer> - Encrypted file data Example:

decryptFileData()

Decrypts an EncArrayBuffer to file data.
Parameters:
  • encBuffer - The encrypted file buffer
  • key - The symmetric key to decrypt with
Returns: Promise<Uint8Array> - Decrypted file data Throws: Error if decryption fails Example:

Key Wrapping

Key wrapping is used to securely encrypt cryptographic keys with other keys. See Key Wrap on Wikipedia.

wrapDecapsulationKey()

Wraps a private key (decapsulation key) with a symmetric key.
Parameters:
  • decapsulationKeyPcks8 - The private key in PKCS8 format
  • wrappingKey - The symmetric key to wrap with
Returns: Promise<EncString> - Wrapped private key

unwrapDecapsulationKey()

Unwraps a private key (decapsulation key) with a symmetric key.
Parameters:
  • wrappedDecapsulationKey - The wrapped private key
  • wrappingKey - The symmetric key to unwrap with
Returns: Promise<Uint8Array> - Unwrapped private key as bytes Throws: Error if unwrapping fails

wrapEncapsulationKey()

Wraps a public key (encapsulation key) with a symmetric key.
Parameters:
  • encapsulationKeySpki - The public key in SPKI format
  • wrappingKey - The symmetric key to wrap with
Returns: Promise<EncString> - Wrapped public key

unwrapEncapsulationKey()

Unwraps a public key (encapsulation key) with a symmetric key.
Parameters:
  • wrappedEncapsulationKey - The wrapped public key
  • wrappingKey - The symmetric key to unwrap with
Returns: Promise<Uint8Array> - Unwrapped public key as bytes Throws: Error if unwrapping fails

wrapSymmetricKey()

Wraps a symmetric key with another symmetric key.
Parameters:
  • keyToBeWrapped - The symmetric key to wrap
  • wrappingKey - The symmetric key to wrap with
Returns: Promise<EncString> - Wrapped symmetric key Example:

unwrapSymmetricKey()

Unwraps a symmetric key with another symmetric key.
Parameters:
  • keyToBeUnwrapped - The wrapped symmetric key
  • wrappingKey - The symmetric key to unwrap with
Returns: Promise<SymmetricCryptoKey> - Unwrapped symmetric key Throws: Error if unwrapping fails Example:

Key Encapsulation

Key encapsulation mechanisms (KEM) are used to securely share symmetric keys using asymmetric cryptography. See Key Encapsulation on Wikipedia.
These methods do not establish sender authenticity.

encapsulateKeyUnsigned()

Encapsulates a symmetric key with an asymmetric public key.
Parameters:
  • sharedKey - The symmetric key to share
  • encapsulationKey - The recipient’s public key
Returns: Promise<EncString> - Encapsulated key
You probably do not want to use this. Contact the Key-Management team if you think you need to.

decapsulateKeyUnsigned()

Decapsulates a shared symmetric key with an asymmetric private key.
Parameters:
  • encryptedSharedKey - The encapsulated shared key
  • decapsulationKey - The private key to decapsulate with
Returns: Promise<SymmetricCryptoKey> - Decapsulated symmetric key Throws: Error if decapsulation fails

Hashing

hash()

Generates a base64-encoded hash of a value.
Parameters:
  • value - The value to hash
  • algorithm - The hashing algorithm to use
Returns: Promise<string> - Base64-encoded hash Example:

Encryption Types

The service supports multiple encryption types defined in EncryptionType:

Symmetric Encryption Types

Asymmetric Encryption Types

Data Types

EncString

Represents an encrypted string with metadata about the encryption type. Location: libs/common/src/key-management/crypto/models/enc-string.ts Structure:
Format: <encryptionType>.<iv>|<data>|<mac> Examples:
  • 0.iv|data - AES-256-CBC without MAC
  • 2.iv|data|mac - AES-256-CBC with HMAC-SHA256
  • 3.data - RSA-2048-OAEP-SHA256

EncArrayBuffer

Represents encrypted binary data (used for file encryption). Location: libs/common/src/platform/models/domain/enc-array-buffer.ts Structure:

SymmetricCryptoKey

Represents a symmetric encryption key. Location: libs/common/src/platform/models/domain/symmetric-crypto-key.ts Key Types:
Key Sizes:
  • 32 bytes (256 bits) - AES-256 only
  • 64 bytes (512 bits) - AES-256 + HMAC-SHA256

Usage Examples

Encrypting User Data

Encrypting Files

Key Wrapping for Sharing

Security Considerations

  1. Error Handling: Decryption methods throw on failure. Always handle these errors appropriately in your application.
  2. Key Management: Never log or expose encryption keys. Always use secure key storage.
  3. Authenticated Encryption: Prefer AesCbc256_HmacSha256_B64 over AesCbc256_B64 for integrity protection.
  4. Deprecation: Some methods are deprecated. Check warnings before using in new code.
  5. SDK Preference: For new features, use the SDK’s DataEnvelope instead of direct encryption.