AgeSharp 0.2.0-preview.7
dotnet add package AgeSharp --version 0.2.0-preview.7
NuGet\Install-Package AgeSharp -Version 0.2.0-preview.7
<PackageReference Include="AgeSharp" Version="0.2.0-preview.7" />
<PackageVersion Include="AgeSharp" Version="0.2.0-preview.7" />
<PackageReference Include="AgeSharp" />
paket add AgeSharp --version 0.2.0-preview.7
#r "nuget: AgeSharp, 0.2.0-preview.7"
#:package AgeSharp@0.2.0-preview.7
#addin nuget:?package=AgeSharp&version=0.2.0-preview.7&prerelease
#tool nuget:?package=AgeSharp&version=0.2.0-preview.7&prerelease
<p align="center"> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://github.com/FiloSottile/age/blob/main/logo/logo_white.svg"> <source media="(prefers-color-scheme: light)" srcset="https://github.com/FiloSottile/age/blob/main/logo/logo.svg"> <img alt="The age logo, a wireframe of St. Peters dome in Rome, with the text: age, file encryption" width="600" src="https://github.com/FiloSottile/age/blob/main/logo/logo.svg"> </picture> </p>
AgeSharp is a C# implementation of the
age file encryption format, fully interoperable
with the reference Go implementation and
other age-compatible tools.
It depends on BouncyCastle.Cryptography (2.7.0 or later) and SSH.NET, which it uses to decrypt passphrase-protected SSH keys; both come with the package. It targets .NET 10. A project that pins an older BouncyCastle gets a NU1605 (package downgrade) error on restore and needs to raise its pin to 2.7.0.
Features
- All standard recipient types: X25519, scrypt/passphrase, SSH-Ed25519, SSH-RSA
- Post-quantum ML-KEM-768-X25519 hybrid encryption
- Hardware keys: tagged P-256 and P-256 + ML-KEM-768 recipients (
age1tag1…,age1tagpq1…), encryption only - Plugin protocol — interoperates with
age-plugin-*binaries - Encrypt to multiple recipients
- ASCII armor support
- Streaming encryption and decryption across all APIs — memory is bounded by a single 64 KiB chunk buffer regardless of input size (1 GiB file uses the same working set as a 1 MB file)
- Pull-based streaming (
EncryptReader/DecryptReader) returns a readableStreamfor pipe-and-forget use cases - Detached header APIs (
EncryptDetached/DecryptDetached) - Random-access decryption (
AgeRandomAccess) — seek into encrypted files - Header inspection without decryption (
AgeHeader.Parse) - Encrypted identity files (passphrase-protected)
- Recipients file parsing (
-Rstyle files with comments) - Fully interoperable — files produced by AgeSharp decrypt with
age,rage, and vice versa - Runs in Blazor WebAssembly — automatically uses a managed
ChaCha20-Poly1305 backend in the browser, where the platform cipher is
unavailable. One exception: of passphrase-protected SSH keys, only ssh-ed25519
keys protected with
aes256-gcm@openssh.comorchacha20-poly1305@openssh.comdecrypt there — notssh-keygen's default AES-CTR, and no ssh-rsa key
Installation
dotnet add package AgeSharp
Usage
Encrypt and decrypt
using Age;
using Age.Recipients;
using var identity = X25519Identity.Generate();
var recipient = identity.Recipient;
using var input = new MemoryStream("Hello, age!"u8.ToArray());
using var encrypted = new MemoryStream();
AgeEncrypt.Encrypt(input, encrypted, recipient);
encrypted.Position = 0;
using var decrypted = new MemoryStream();
AgeEncrypt.Decrypt(encrypted, decrypted, identity);
Passphrase encryption
var passphrase = new ScryptRecipient("correct-horse-battery-staple");
using var input = new MemoryStream("Hello, age!"u8.ToArray());
using var encrypted = new MemoryStream();
AgeEncrypt.Encrypt(input, encrypted, passphrase);
encrypted.Position = 0;
using var decrypted = new MemoryStream();
AgeEncrypt.Decrypt(encrypted, decrypted, passphrase);
ASCII armor
AgeEncrypt.Encrypt(input, encrypted, armor: true, recipient);
// -----BEGIN AGE ENCRYPTED FILE-----
// YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSA...
// -----END AGE ENCRYPTED FILE-----
Multiple recipients
using var alice = X25519Identity.Generate();
using var bob = X25519Identity.Generate();
AgeEncrypt.Encrypt(input, encrypted, alice.Recipient, bob.Recipient);
// Either identity can decrypt
AgeEncrypt.Decrypt(encrypted, decrypted, bob);
SSH keys
var recipient = SshEd25519Recipient.Parse("ssh-ed25519 AAAA...");
var identity = SshEd25519Identity.Parse(File.ReadAllText("/path/to/id_ed25519"));
AgeEncrypt.Encrypt(input, encrypted, recipient);
encrypted.Position = 0;
AgeEncrypt.Decrypt(encrypted, decrypted, identity);
A passphrase-protected key (OpenSSH format) needs somewhere to get its passphrase from. It is asked for only when a file addressed to the key is decrypted, and never for encrypting to the key:
var identity = AgeKeygen.ParseSshIdentity(
File.ReadAllText("/path/to/id_ed25519"),
passphrase: () => PromptForPassphrase());
An ssh-rsa key protected in the older PEM format (BEGIN RSA PRIVATE KEY with
Proc-Type: 4,ENCRYPTED, as ssh-keygen wrote before OpenSSH 7.8 and still writes with
-m PEM) does not store its public key, so, as with age, it needs its .pub file
too. The decrypted key must match it:
var identity = AgeKeygen.ParseSshIdentity(
File.ReadAllText("/path/to/id_rsa"),
File.ReadAllText("/path/to/id_rsa.pub"),
passphrase: () => PromptForPassphrase());
Keys encrypted as PKCS#8 (BEGIN ENCRYPTED PRIVATE KEY) are not read, as in age;
convert them with ssh-keygen -p.
Post-quantum (ML-KEM-768-X25519)
using var identity = MlKem768X25519Identity.Generate();
var recipient = identity.Recipient;
AgeEncrypt.Encrypt(input, encrypted, recipient);
Hardware keys (tagged recipients)
age's tagged recipient types are for keys held on hardware, by plugins that use
this format: age1tag1… (P-256) and age1tagpq1… (P-256 hybridised with
ML-KEM-768, post-quantum). AgeSharp encrypts to them natively; decrypting is done
by the plugin, through PluginIdentity.
- Each stanza carries a tag that lets the hardware recognise it, so anyone who knows the public key can tell which files were encrypted to it.
- Like
age1pq1…, anage1tagpq1…recipient can't be mixed with classical recipients in one file: that would void its post-quantum protection.
var recipient = P256TagRecipient.Parse("age1tag1...");
AgeEncrypt.Encrypt(input, encrypted, recipient);
// Recipients files read them too
var recipients = AgeKeygen.ParseRecipientsFile(File.ReadAllText("recipients.txt"));
Pull-based streaming
Returns a readable Stream — header and key setup is eager, payload encryption/decryption is lazy (chunk-by-chunk on Read()).
// Encrypt: returns a Stream you read ciphertext from
using var encryptedStream = AgeEncrypt.EncryptReader(plaintext, recipient);
encryptedStream.CopyTo(networkStream);
// Decrypt: returns a Stream you read plaintext from
using var decryptedStream = AgeEncrypt.DecryptReader(ciphertext, identity);
decryptedStream.CopyTo(outputStream);
Detached headers
Splits the age header and payload into separate streams — useful for storing the header and payload in different locations.
// Encrypt with separate header and payload
AgeEncrypt.EncryptDetached(input, headerOutput, payloadOutput, recipient);
// Decrypt from separate streams
AgeEncrypt.DecryptDetached(headerInput, payloadInput, output, identity);
Random-access decryption
Seek into an encrypted file and decrypt individual chunks without reading the whole file — useful for encrypted archives, databases, and large files.
using var ra = new AgeRandomAccess(ciphertext, identity);
Console.WriteLine($"Plaintext length: {ra.PlaintextLength}");
// Read 100 bytes at offset 50000
var buf = new byte[100];
ra.ReadAt(50000, buf);
// Or get a seekable Stream
using var stream = ra.GetStream();
stream.Seek(50000, SeekOrigin.Begin);
stream.Read(buf);
Header inspection
Parse the header of an encrypted file without decrypting it.
var header = AgeHeader.Parse(stream);
Console.WriteLine($"Recipients: {header.RecipientCount}");
Console.WriteLine($"Armored: {header.IsArmored}");
Console.WriteLine($"Payload offset: {header.PayloadOffset}");
foreach (var stanza in header.Recipients)
Console.WriteLine($" {stanza.Type}: {stanza.Args[0]}");
Parse existing keys
using var identity = AgeKeygen.ParseIdentity("AGE-SECRET-KEY-1...");
var recipient = AgeKeygen.ParseRecipient("age1...");
ParseIdentity and ParseRecipient read X25519 keys only. For a recipient string of
any type (age1pq1…, age1tag1…, an SSH public key, a plugin's age1name1…), parse
it as a one-line recipients file, or use the type's own Parse:
var recipient = AgeKeygen.ParseRecipientsFile(line)[0]; // any type
var tagged = P256TagRecipient.Parse("age1tag1..."); // a specific type
var ssh = AgeKeygen.ParseSshRecipient("ssh-ed25519 AAAA...");
Custom recipients and identities
Implement IRecipient and IIdentity to integrate custom key types,
remote secrets managers, or age plugins.
public class MyRecipient : IRecipient
{
public string? Label => null; // or a security label to prevent mixing
public Stanza Wrap(ReadOnlySpan<byte> fileKey)
{
// Wrap the file key using your custom scheme
return new Stanza("MyType", ["arg1"], wrappedKey);
}
}
public class MyIdentity : IIdentity
{
public byte[]? Unwrap(Stanza stanza)
{
// Called once per stanza in the file's header. Return the file
// key if this identity matches the stanza, null if not.
}
}
When decrypting, AgeSharp's own identities (X25519, ML-KEM-768-X25519, scrypt) are tried before any other, whatever order they are passed in, as in the Go implementation. A custom, SSH or plugin identity, which may ask the user for a passphrase or a touch, is asked only if none of those opens the file.
Parsing limits
An age header must be buffered in full before its MAC can be verified, so
AgeSharp caps how much it will read before authentication — otherwise a hostile
or truncated stream with an unterminated (or endlessly repeated) line could
exhaust memory. The limits are exposed as constants on AgeLimits:
| Constant | Default | Bounds |
|---|---|---|
AgeLimits.MaxHeaderBytes |
2 MiB | The whole header (all stanzas) |
AgeLimits.MaxRecipientStanzas |
1024 | Recipient stanzas in a header |
AgeLimits.MaxStanzaArguments |
128 | Arguments on one stanza line |
AgeLimits.MaxArmorLineBytes |
64 KiB | A single ASCII-armor line |
Exceeding a limit throws AgeHeaderException / AgeArmorException. The age
specification sets no such
bounds. The three header limits are the ones the Go implementation applies from
v1.3.2 on, so both accept and reject the same files; the armor line limit is
AgeSharp's own. They sit far above any real file (the largest built-in stanza
line is ~1.5 KiB, so 2 MiB holds well over a thousand post-quantum recipients),
so legitimate input never trips them. AgeLimits.MaxHeaderLineBytes is obsolete:
a header line is bounded only by MaxHeaderBytes.
Command line
AgeSharp is a library. For a command-line tool, use the reference age or rage; AgeSharp reads and writes the same files.
Development
make build # Build
make test # Run all tests (interop tests run when age and rage are on PATH)
make bench # Run BenchmarkDotNet benchmarks
Benchmarks
See docs/BENCHMARKS.md for BenchmarkDotNet results.
Feature comparison
See docs/FEATURE_COMPARISON.md for a detailed comparison
with the Go reference implementation and Rust's rage.
See also
- age-encryption.org/v1 — the age format specification
- age — the reference Go implementation and CLI
- rage — a Rust implementation of age
- awesome-age — age plugins, tools, and integrations
AgeSharp targets .NET 10. If you need an older runtime, there is a community port to .NET Standard 2.0 / .NET Framework 4.8:
- AgeSharpNetStandard — third-party, not maintained or audited by this project. It is a point-in-time port that may lag behind AgeSharp (including security fixes), so review it yourself before relying on it.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- BouncyCastle.Cryptography (>= 2.7.0)
- SSH.NET (>= 2026.0.1)
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 |
|---|---|---|
| 0.2.0-preview.7 | 159 | 11/19/2026 |
| 0.2.0-preview.6 | 64 | 11/19/2026 |
| 0.2.0-preview.5 | 64 | 11/18/2026 |
| 0.2.0-preview.4 | 1,364 | 7/28/2026 |
| 0.2.0-preview.3 | 2,063 | 7/23/2026 |
| 0.2.0-preview.2 | 475 | 4/20/2026 |
| 0.2.0-preview.1 | 87 | 4/20/2026 |
| 0.1.0-preview.4 | 110 | 2/25/2026 |
| 0.1.0-preview.3 | 98 | 2/25/2026 |
| 0.1.0-preview.2 | 141 | 2/25/2026 |
| 0.1.0-preview.1 | 102 | 2/24/2026 |