Modern cryptography helpers that balance safety, performance, and developer ergonomics for application-level secrets. The package bundles symmetric encryption, password-based encryption, public-key operations, hashing, and Base64 helpers behind cohesive service interfaces with dependency injection support.
IAesGcmEncryption delivers nonce management, authentication tags, and optional AAD.IAesEncryption supplies a simple AES-CBC wrapper for scenarios that require deterministic
output (key escrow, repeatable secrets).services.AddEncryptionServices() wires the entire stack with sensible lifetimes.| Interface | Implementation | Purpose | Authenticated |
|---|---|---|---|
IAesEncryption |
AesEncryption |
AES-CBC encryption/decryption with composite key serialization | ❌ |
IAesGcmEncryption |
AesGcmEncryption |
AEAD wrapper with automatic nonce generation and string helpers | ✅ |
IPasswordAesEncryption |
PasswordAesEncryption |
PBKDF2 + AES-CBC helper for password-protected payloads | ❌ |
IRsaEncryption |
RsaEncryption |
RSA 2048/4096 encryption and PKCS#1 signing/verifying | N/A |
IHmacHashing |
HmacHashing |
HMAC-SHA256/512 with caching, Base64/hex output helpers | N/A |
IShaHashing |
ShaHashing |
SHA256/512 hashing utilities with verification helpers | N/A |
| Extensions | Base65StringExtensions |
Base64/Base64Url encode, decode, and validation helpers | N/A |
Naming note:
Base65StringExtensionsretains legacy naming while covering both Base64 and Base64Url utilities.
var services = new ServiceCollection();
services.AddEncryptionServices();
await using var provider = services.BuildServiceProvider();
var aesGcm = provider.GetRequiredService<IAesGcmEncryption>();
var cipher = aesGcm.EncryptString("hello world");
var plain = aesGcm.DecryptString(cipher); // "hello world"
var gcm = provider.GetRequiredService<IAesGcmEncryption>();
var aad = Encoding.UTF8.GetBytes("order:1234");
var cipher = gcm.EncryptString("sensitive payload", aad);
var plain = gcm.DecryptString(cipher, aad);
var passwordCrypto = provider.GetRequiredService<IPasswordAesEncryption>();
var encrypted = passwordCrypto.Encrypt("config-json", "Sup3r$ecret");
var recovered = passwordCrypto.Decrypt(encrypted, "Sup3r$ecret");
var rsa = provider.GetRequiredService<IRsaEncryption>();
var cipher = rsa.Encrypt("api-key");
var signature = rsa.Sign("message");
var publicOnly = RsaEncryption.FromPublicKey(rsa.PublicKey);
var verified = publicOnly.Verify("message", signature);
var hmac = provider.GetRequiredService<IHmacHashing>();
var mac = hmac.Compute("body", "shared-secret");
var ok = hmac.Verify("body", "shared-secret", mac);
var compact = "payload".ToBase64UrlString();
var original = compact.FromBase64UrlString();
var isValid = compact.IsBase64UrlString();
Add all encryption primitives with a single extension:
public void ConfigureServices(IServiceCollection services)
{
services.AddEncryptionServices();
}
Each interface is registered as a transient implementation to keep cryptographic state isolated per consumer. If you need long-lived instances (for example to reuse RSA key pairs), register the implementation yourself with the required lifetime.
<key>:<iv>) and deterministic encryption for legacy use cases.IAesGcmEncryption for new development; only use IAesEncryption where deterministic output is required.DKNet.Svc.Encryption ships with >97% line coverage across deterministic and randomised test suites. Tests assert
round-trip behaviour, guard rails for invalid inputs, and key material validation, making the package suitable for audit-heavy
solutions.