DKNet

DKNet.Svc.Encryption

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.

✨ Highlights

🧱 Provided Services

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: Base65StringExtensions retains legacy naming while covering both Base64 and Base64Url utilities.

🚀 Quick Start

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"

📦 Usage Recipes

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);

Password-Based Encryption

var passwordCrypto = provider.GetRequiredService<IPasswordAesEncryption>();
var encrypted = passwordCrypto.Encrypt("config-json", "Sup3r$ecret");
var recovered = passwordCrypto.Decrypt(encrypted, "Sup3r$ecret");

RSA Envelope + Signature

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);

HMAC Hashing

var hmac = provider.GetRequiredService<IHmacHashing>();
var mac = hmac.Compute("body", "shared-secret");
var ok  = hmac.Verify("body", "shared-secret", mac);

Base64 Helpers

var compact = "payload".ToBase64UrlString();
var original = compact.FromBase64UrlString();
var isValid = compact.IsBase64UrlString();

🧩 DI Integration

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.

🧠 Design Notes

🛡️ Security Guidance

✅ Testing & Quality

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.