Field Encryption
Tayra's field encryption engine scans your entity types for [PersonalData] and [DeepPersonalData] annotations, then encrypts and decrypts those fields using AES-256-GCM with per-subject keys - strings in place, non-string members into a companion byte[].
ITayra - The Public API
All encryption operations go through ITayra:
var tayra = provider.GetRequiredService<ITayra>();
await tayra.EncryptAsync(customer); // Encrypts all PII fields in-place
await tayra.DecryptAsync(customer); // Decrypts back to plaintextWithout dependency injection, create a standalone host:
using var tayra = TayraHost.Create(opts => opts.LicenseKey = key);| Method | Purpose |
|---|---|
EncryptAsync<T> | Encrypts all [PersonalData] fields on the object in-place. Keys are auto-created if needed. Blind indexes are computed automatically. |
DecryptAsync<T> | Decrypts all [PersonalData] fields in-place. If the key is missing (shredded), replacement values are used. |
HasPersonalData<T> | Returns true if the type has any PII annotations. Useful for conditional encryption. |
Metadata-Driven Reflection
Internal implementation - provided for clarity
The field encryption engine does not hard-code field names or types. Instead, it relies on a metadata cache to discover annotated properties:
- Type scanning - On first use, the metadata cache scans the type's public instance properties using reflection.
- Field classification - Each annotated property is classified as
Text,Deep,Serialized,TextCollection, orDeepCollection. - Subject resolution -
[DataSubjectId]properties are recorded with their group and prefix. - Caching - The metadata is cached in a
ConcurrentDictionaryand reused for all subsequent calls.
After the first call, there is zero reflection overhead.
EncryptAsync Flow
Internal implementation - provided for clarity
When EncryptAsync<T> is called:
EncryptAsync(customer)
│
├─ Get metadata for typeof(Customer)
│ └─ Fields: Name (Text), Email (Text)
│ └─ Subjects: Id (default group)
│
├─ Resolve keys for encryption
│ └─ Get or create AES key for the data subject
│ └─ Returns 32-byte AES key (created or existing)
│
├─ For each field:
│ ├─ Text: AesGcm.Encrypt → Base64 → set property
│ ├─ Deep: Recurse into nested object
│ ├─ Serialized: To JSON → AesGcm.Encrypt → set companion byte[], zero the original
│ ├─ TextCollection: Encrypt each element
│ └─ DeepCollection: Recurse into each element
│
├─ Emit telemetry (activity, metrics, audit event)
└─ ReturnKey points:
- Keys are created automatically on first use.
- The operation is in-place - properties are modified directly on the object. Integrations that only need the encrypted bytes never encrypt your object in place:
- The Marten serializers and the JSON converter encrypt a private copy through
FieldEncrypterReflectionHelper.EncryptCopyForSerializationAsyncand serializeEncryptedForSerialization.Value. Your object is only read, so another thread reading it during the write (a later stage of a Marten composite projection, for one) always sees plaintext. The copy is shallow except along the paths encryption writes to: the object, objects reached through[DeepPersonalData], and personal data collections. Blind-index companions are still set on your object, because they are hashes and ORMs read them off it after the write. A personal data collection that cannot be rebuilt (read-only or immutable) falls back to encrypting in place and restoring. - The Wolverine, EF Core and MongoDB integrations use
FieldEncrypterReflectionHelper.EncryptForSerializationAsync, which encrypts in place and returns a handle that puts the plaintext back exactly once the write has ended, whether it succeeded or failed. Repeated writes of one instance never encrypt ciphertext, but the object holds ciphertext while the write is in progress. - A custom serializer wrapper should use
EncryptCopyForSerializationAsyncwhen it only reads the object it writes.
- The Marten serializers and the JSON converter encrypt a private copy through
- Encryption fails closed: if a
[DataSubjectId]value isnull, or no key can be resolved for a field's group,EncryptAsyncthrows anInvalidOperationExceptioninstead of leaving those fields as plaintext. The one exception is a subject markedAllowMissing: its group is then stored as it is, and the write is audited (StoredWithoutSubject) and counted. - If a masking strategy is set (via the
Maskingproperty), the masked value is embedded in the ciphertext string.
DecryptAsync Flow
Internal implementation - provided for clarity
When DecryptAsync<T> is called:
DecryptAsync(customer)
│
├─ Get metadata for typeof(Customer)
│
├─ Resolve keys for decryption
│ └─ Retrieve key for the data subject
│ ├─ Key exists → byte[] returned
│ └─ Key shredded → null returned
│
├─ For each field:
│ ├─ Group has no subject (the [DataSubjectId] is null):
│ │ └─ Leave the value as stored
│ │
│ ├─ Key exists:
│ │ └─ Base64 decode → AesGcm.Decrypt → set property
│ │
│ └─ Key is null (shredded):
│ ├─ Check for embedded redacted value (TAYRA_M: prefix)
│ │ └─ Found → use redacted value
│ └─ Not found → use ReplacementValue from attribute (or default)
│
├─ Emit telemetry
└─ ReturnKey points:
- Never creates keys during decryption.
- A
nullsubject is not a missing key: encryption never writes ciphertext without one, so that group's fields are returned exactly as stored and noCryptoShreddingDetectedevent is raised. - A missing key triggers the replacement value logic. It uses the attribute
ReplacementValuewhen set (for string fields a masking strategy, if configured, supersedes it), otherwise theIReplacementValueProvidertype default. - A
FormatException(the stored value is not Base64 - typically legacy plaintext from before Tayra was adopted) is tolerated quietly atDebuglevel, leaving the property unchanged. - A
CryptographicException(tampered ciphertext or AAD context mismatch) is surfaced: it is logged atWarninglevel and emits anIntegrityCheckFailedaudit event. The property is left unchanged.
Masking Embedding
When a [PersonalData] field has a masking strategy configured (via the Masking property):
During encryption:
- The plaintext value is redacted according to the mode (e.g.,
"Jane Doe"becomes"Ja******"withMaskAfter(2)). - The redacted value is prepended to the ciphertext:
TAYRA_M:Ja******\n{base64_ciphertext}. - The combined string is stored in the property.
During decryption with key:
- The
TAYRA_M:prefix is detected and stripped. - The remaining Base64 ciphertext is decrypted normally.
- The original plaintext is restored.
During decryption without key (shredded):
- The
TAYRA_M:prefix is detected. - The redacted value (
Ja******) is extracted and returned. - The
ReplacementValuefrom the attribute is ignored.
Thread Safety
ITayra is registered as a singleton and is thread-safe. Concurrent calls on different objects are safe. Concurrent calls on the same object instance are not safe - use external synchronization if needed.
See Also
- Crypto Engine - Key management layer (internal)
- Key Store - Key persistence (public extension point)
- Encryption - AES-256-GCM wire format
- Attributes Overview - The annotations that drive the encrypter
- Getting Started - End-to-end usage example
