Overview
TheEncryptService 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.
plainValue- The string to encryptkey- The symmetric key to encrypt with
Promise<EncString> - Encrypted string
For new use-cases, prefer using the DataEnvelope inside the SDK instead. This is both safer and more maintainable.
decryptString()
Decrypts an EncString to a plaintext string.
encString- The encrypted stringkey- The symmetric key to decrypt with
Promise<string> - Decrypted plaintext string
Throws: Error if decryption fails
Example:
Byte Array Encryption
encryptBytes()
Encrypts a byte array to an EncString.
plainValue- The bytes to encryptkey- The symmetric key to encrypt with
Promise<EncString> - Encrypted bytes as an EncString
decryptBytes()
Decrypts an EncString to a byte array.
encString- The encrypted string containing byteskey- The symmetric key to decrypt with
Promise<Uint8Array> - Decrypted bytes
Throws: Error if decryption fails
File Encryption
encryptFileData()
Encrypts file data to an EncArrayBuffer.
plainValue- The file data to encryptkey- The symmetric key to encrypt with
Promise<EncArrayBuffer> - Encrypted file data
Example:
decryptFileData()
Decrypts an EncArrayBuffer to file data.
encBuffer- The encrypted file bufferkey- The symmetric key to decrypt with
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.
decapsulationKeyPcks8- The private key in PKCS8 formatwrappingKey- The symmetric key to wrap with
Promise<EncString> - Wrapped private key
unwrapDecapsulationKey()
Unwraps a private key (decapsulation key) with a symmetric key.
wrappedDecapsulationKey- The wrapped private keywrappingKey- The symmetric key to unwrap with
Promise<Uint8Array> - Unwrapped private key as bytes
Throws: Error if unwrapping fails
wrapEncapsulationKey()
Wraps a public key (encapsulation key) with a symmetric key.
encapsulationKeySpki- The public key in SPKI formatwrappingKey- The symmetric key to wrap with
Promise<EncString> - Wrapped public key
unwrapEncapsulationKey()
Unwraps a public key (encapsulation key) with a symmetric key.
wrappedEncapsulationKey- The wrapped public keywrappingKey- The symmetric key to unwrap with
Promise<Uint8Array> - Unwrapped public key as bytes
Throws: Error if unwrapping fails
wrapSymmetricKey()
Wraps a symmetric key with another symmetric key.
keyToBeWrapped- The symmetric key to wrapwrappingKey- The symmetric key to wrap with
Promise<EncString> - Wrapped symmetric key
Example:
unwrapSymmetricKey()
Unwraps a symmetric key with another symmetric key.
keyToBeUnwrapped- The wrapped symmetric keywrappingKey- The symmetric key to unwrap with
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.
sharedKey- The symmetric key to shareencapsulationKey- The recipient’s public key
Promise<EncString> - Encapsulated key
decapsulateKeyUnsigned()
Decapsulates a shared symmetric key with an asymmetric private key.
encryptedSharedKey- The encapsulated shared keydecapsulationKey- The private key to decapsulate with
Promise<SymmetricCryptoKey> - Decapsulated symmetric key
Throws: Error if decapsulation fails
Hashing
hash()
Generates a base64-encoded hash of a value.
value- The value to hashalgorithm- The hashing algorithm to use
Promise<string> - Base64-encoded hash
Example:
Encryption Types
The service supports multiple encryption types defined inEncryptionType:
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:
<encryptionType>.<iv>|<data>|<mac>
Examples:
0.iv|data- AES-256-CBC without MAC2.iv|data|mac- AES-256-CBC with HMAC-SHA2563.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:
- 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
Related Services
- CryptoFunctionService - Low-level crypto primitives
- KeyService - Key management and generation
Security Considerations
- Error Handling: Decryption methods throw on failure. Always handle these errors appropriately in your application.
- Key Management: Never log or expose encryption keys. Always use secure key storage.
-
Authenticated Encryption: Prefer
AesCbc256_HmacSha256_B64overAesCbc256_B64for integrity protection. - Deprecation: Some methods are deprecated. Check warnings before using in new code.
- SDK Preference: For new features, use the SDK’s DataEnvelope instead of direct encryption.