Soenneker.Encryption.AesGcm 11.0.85

Prefix Reserved
dotnet add package Soenneker.Encryption.AesGcm --version 11.0.85
                    
NuGet\Install-Package Soenneker.Encryption.AesGcm -Version 11.0.85
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Soenneker.Encryption.AesGcm" Version="11.0.85" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Encryption.AesGcm" Version="11.0.85" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.Encryption.AesGcm" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Soenneker.Encryption.AesGcm --version 11.0.85
                    
#r "nuget: Soenneker.Encryption.AesGcm, 11.0.85"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Soenneker.Encryption.AesGcm@11.0.85
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Soenneker.Encryption.AesGcm&version=11.0.85
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Encryption.AesGcm&version=11.0.85
                    
Install as a Cake Tool

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

Soenneker.Encryption.AesGcm

Authenticated encryption for strings and byte payloads using AES-GCM, random nonces, optional associated data, and a versioned string envelope.

Install

dotnet add package Soenneker.Encryption.AesGcm

Encrypt and decrypt a string

using System.Security.Cryptography;
using Soenneker.Encryption.AesGcm;

byte[] keyBytes = RandomNumberGenerator.GetBytes(32);
string keyMaterial = Convert.ToBase64String(keyBytes);
CryptographicOperations.ZeroMemory(keyBytes);

const string context = "tenant-42:api-token";

string encrypted = AesGcmUtil.Encrypt(
    "secret value",
    keyMaterial,
    associatedData: context);

string plaintext = AesGcmUtil.Decrypt(
    encrypted,
    keyMaterial,
    associatedData: context);

Each encryption generates a new 12-byte nonce, so encrypting the same value twice produces different output. The encoded format is:

v1:<base64 nonce>:<base64 ciphertext>:<base64 authentication tag>

Associated data is authenticated but neither encrypted nor stored in the payload. Decryption must receive the exact same bytes or string; a mismatch fails authentication.

Handle untrusted ciphertext

if (!AesGcmUtil.TryDecrypt(encrypted, keyMaterial, out string? value, context))
{
    // Malformed envelope, wrong key/context, or failed authentication.
}

Decrypt throws for malformed envelopes, invalid key material, an unexpected prefix, or authentication failure. TryDecrypt converts those expected input failures into false and sets the output to null.

Encrypt bytes

byte[] key = RandomNumberGenerator.GetBytes(32);
byte[] data = "secret value"u8.ToArray();
byte[] contextBytes = "tenant-42:api-token"u8.ToArray();

AesGcmEncryptedPayload payload = AesGcmUtil.Encrypt(data, key, contextBytes);
byte[] decrypted = AesGcmUtil.Decrypt(payload, key, contextBytes);

CryptographicOperations.ZeroMemory(key);
CryptographicOperations.ZeroMemory(data);
CryptographicOperations.ZeroMemory(decrypted);

The raw payload exposes mutable nonce, ciphertext, and tag arrays. Store or transmit all three, and do not modify them before decryption.

Key material

Use a cryptographically random 16-, 24-, or preferably 32-byte key stored in a secret manager. For the string API, Base64-encode those bytes as shown above. If a string is not Base64 that decodes to an AES key length, BuildKey(string) hashes its UTF-8 bytes once with SHA-256. That behavior is a deterministic compatibility conversion, not password hardening: do not pass a human password or other low-entropy secret. Derive password-based keys with an appropriate salted, work-factored KDF before calling this package.

For byte APIs, key material of an AES key length is copied directly; other non-empty input is SHA-256 hashed. BuildKey returns key bytes owned by the caller, who should clear them with CryptographicOperations.ZeroMemory when finished.

Versioning and rotation

The prefix defaults to v1 and is checked exactly during decryption. It is a format marker, not an authenticated key identifier. Keep the expected prefix and allowed keys under application policy; do not trust an incoming prefix to select arbitrary key material. For rotation, store a trusted key identifier alongside the encrypted value and retain old keys only for the required migration window.

Never reuse a nonce with the same AES key. The high-level encryption methods generate nonces with RandomNumberGenerator; if you construct raw payloads elsewhere, nonce uniqueness is your responsibility.

Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
11.0.85 104 11/18/2026
11.0.83 51 11/18/2026
11.0.82 107 11/15/2026
11.0.81 160 11/11/2026
11.0.80 91 11/11/2026
11.0.79 105 11/11/2026
11.0.78 92 11/11/2026
11.0.77 88 11/11/2026
11.0.76 96 11/11/2026
11.0.75 224 11/3/2026
11.0.74 96 11/3/2026
11.0.73 99 11/2/2026
11.0.72 142 10/27/2026
11.0.71 108 10/26/2026
11.0.70 107 10/26/2026
4.0.69 177 10/15/2026
4.0.68 95 10/14/2026
4.0.67 98 10/14/2026
4.0.66 101 10/14/2026
4.0.65 96 10/14/2026
Loading failed

Updated Soenneker.Extensions.String to 11.0.824