System.Formats.Asn1 9.0.20

About

Provides functionality for parsing, encoding, and decoding data using Abstract Syntax Notation One (ASN.1). ASN.1 is a standard interface description language for defining data structures that can be serialized and deserialized in a cross-platform way.

Key Features

  • Parse ASN.1 data into .NET types.
  • Encode .NET types into ASN.1 format.
  • Support for BER, CER, DER: Handles Basic Encoding Rules (BER), Canonical Encoding Rules (CER), and Distinguished Encoding Rules (DER).

How to Use

Parsing ASN.1 data:

using System.Formats.Asn1;
using System.Numerics;

// Sample ASN.1 encoded data (DER format)
byte[] asn1Data = [0x30, 0x09, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x01, 0x03];

// Create an AsnReader to parse the data
AsnReader reader = new(asn1Data, AsnEncodingRules.DER);

// Parse the sequence
AsnReader sequenceReader = reader.ReadSequence();

// Read integers from the sequence
BigInteger firstInt = sequenceReader.ReadInteger();
BigInteger secondInt = sequenceReader.ReadInteger();
BigInteger thirdInt = sequenceReader.ReadInteger();

Console.WriteLine($"First integer: {firstInt}");
Console.WriteLine($"Second integer: {secondInt}");
Console.WriteLine($"Third integer: {thirdInt}");

// First integer: 1
// Second integer: 2
// Third integer: 3

Decoding ASN.1 data using AsnDecoder:

using System.Formats.Asn1;
using System.Numerics;
using System.Text;

// Sample ASN.1 encoded data
byte[] booleanData = [0x01, 0x01, 0xFF]; // BOOLEAN TRUE
byte[] integerData = [0x02, 0x01, 0x05]; // INTEGER 5
byte[] octetStringData = [0x04, 0x03, 0x41, 0x42, 0x43]; // OCTET STRING "ABC"
byte[] objectIdentifierData = [0x06, 0x03, 0x2A, 0x03, 0x04]; // OBJECT IDENTIFIER 1.2.3.4
byte[] utf8StringData = [0x0C, 0x05, 0x48, 0x65, 0x6C, 0x6C, 0x6F]; // UTF8String "Hello"

int bytesConsumed;

bool booleanValue = AsnDecoder.ReadBoolean(booleanData, AsnEncodingRules.DER, out bytesConsumed);
Console.WriteLine($"Decoded BOOLEAN value: {booleanValue}, Bytes consumed: {bytesConsumed}");
// Decoded BOOLEAN value: True, Bytes consumed: 3

BigInteger integerValue = AsnDecoder.ReadInteger(integerData, AsnEncodingRules.DER, out bytesConsumed);
Console.WriteLine($"Decoded INTEGER value: {integerValue}, Bytes consumed: {bytesConsumed}");
// Decoded INTEGER value: 5, Bytes consumed: 3

byte[] octetStringValue = AsnDecoder.ReadOctetString(octetStringData, AsnEncodingRules.DER, out bytesConsumed);
Console.WriteLine($"Decoded OCTET STRING value: {Encoding.ASCII.GetString(octetStringValue)}, Bytes consumed: {bytesConsumed}");
// Decoded OCTET STRING value: ABC, Bytes consumed: 5

string objectIdentifierValue = AsnDecoder.ReadObjectIdentifier(objectIdentifierData, AsnEncodingRules.DER, out bytesConsumed);
Console.WriteLine($"Decoded OBJECT IDENTIFIER value: {objectIdentifierValue}, Bytes consumed: {bytesConsumed}");
// Decoded OBJECT IDENTIFIER value: 1.2.3.4, Bytes consumed: 5

string utf8StringValue = AsnDecoder.ReadCharacterString(utf8StringData, AsnEncodingRules.DER, UniversalTagNumber.UTF8String, out bytesConsumed);
Console.WriteLine($"Decoded UTF8String value: {utf8StringValue}, Bytes consumed: {bytesConsumed}");
// Decoded UTF8String value: Hello, Bytes consumed: 7

Encoding ASN.1 data:

// Create an AsnWriter to encode data
AsnWriter writer = new(AsnEncodingRules.DER);

// Create a scope for the sequence
using (AsnWriter.Scope scope = writer.PushSequence())
{
    // Write integers to the sequence
    writer.WriteInteger(1);
    writer.WriteInteger(2);
    writer.WriteInteger(3);
}

// Get the encoded data
byte[] encodedData = writer.Encode();

Console.WriteLine($"Encoded ASN.1 Data: {BitConverter.ToString(encodedData)}");

// Encoded ASN.1 Data: 30-09-02-01-01-02-01-02-02-01-03

Main Types

The main types provided by this library are:

  • System.Formats.Asn1.AsnReader
  • System.Formats.Asn1.AsnWriter
  • System.Formats.Asn1.AsnDecoder
  • System.Formats.Asn1.AsnEncodingRules

Additional Documentation

Feedback & Contributing

System.Formats.Asn1 is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.

Showing the top 20 packages that depend on System.Formats.Asn1.

Packages Downloads
Microsoft.DotNet.Scaffolding.Shared
Contains interfaces for Project Model and messaging for scaffolding.
181
Microsoft.EntityFrameworkCore.SqlServer
Microsoft SQL Server database provider for Entity Framework Core.
311
Microsoft.VisualStudio.Web.CodeGeneration
Contains the CodeGenCommand that finds the appropriate code generator and invokes it from project dependencies.
170
Microsoft.VisualStudio.Web.CodeGeneration.Design
Code Generation tool for ASP.NET Core. Contains the dotnet-aspnet-codegenerator command used for generating controllers and views.
602
Microsoft.VisualStudio.Web.CodeGenerators.Mvc
Code Generators for ASP.NET Core MVC. Contains code generators for MVC Controllers and Views.
213
NuGet.Packaging
NuGet's understanding of packages. Reading nuspec, nupkgs and package signing.
170
System.Security.Cryptography.Cng
Provides cryptographic algorithm implementations and key management with Windows Cryptographic Next Generation API (CNG). Commonly Used Types: System.Security.Cryptography.RSACng System.Security.Cryptography.ECDsaCng System.Security.Cryptography.CngKey When using NuGet 3.x this package requires at least version 3.4.
278
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
170
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
171
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
174
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
176
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
179
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
184
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
188
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
206
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
217
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms When using NuGet 3.x this package requires at least version 3.4.
170
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms When using NuGet 3.x this package requires at least version 3.4.
171
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms When using NuGet 3.x this package requires at least version 3.4.
282

https://go.microsoft.com/fwlink/?LinkID=799421

.NET Framework 4.6.2

.NET 8.0

  • No dependencies.

.NET 9.0

  • No dependencies.

.NET Standard 2.0

Version Downloads Last updated
11.0.0-rc.1.26425.128 4 09/09/2026
11.0.0-preview.7.26381.103 20 08/14/2026
11.0.0-preview.6.26359.118 25 07/17/2026
11.0.0-preview.5.26302.115 27 06/11/2026
11.0.0-preview.4.26230.115 24 05/12/2026
11.0.0-preview.3.26207.106 44 04/14/2026
11.0.0-preview.2.26159.112 53 03/12/2026
11.0.0-preview.1.26104.118 53 02/12/2026
10.0.12 6 09/09/2026
10.0.11 15 08/14/2026
10.0.10 17 07/17/2026
10.0.9 22 06/10/2026
10.0.8 31 05/12/2026
10.0.7 46 04/21/2026
10.0.6 45 04/14/2026
10.0.5 47 03/12/2026
10.0.4 46 03/12/2026
10.0.3 56 02/12/2026
10.0.2 58 01/13/2026
10.0.1 58 12/10/2025
10.0.0 68 11/13/2025
10.0.0-rc.2.25502.107 83 10/15/2025
10.0.0-rc.1.25451.107 92 09/09/2025
10.0.0-preview.7.25380.108 110 08/15/2025
10.0.0-preview.6.25358.103 104 07/17/2025
10.0.0-preview.5.25277.114 105 06/10/2025
10.0.0-preview.4.25258.110 99 05/15/2025
10.0.0-preview.3.25171.5 136 04/13/2025
10.0.0-preview.2.25163.2 133 04/01/2025
10.0.0-preview.1.25080.5 133 04/01/2025
9.0.20 4 09/10/2026
9.0.19 15 08/14/2026
9.0.18 19 07/18/2026
9.0.17 23 06/10/2026
9.0.16 39 05/12/2026
9.0.15 50 04/14/2026
9.0.14 44 03/12/2026
9.0.13 48 02/12/2026
9.0.12 55 01/13/2026
9.0.11 78 11/13/2025
9.0.10 93 10/15/2025
9.0.9 83 09/09/2025
9.0.8 102 08/06/2025
9.0.7 111 07/09/2025
9.0.6 118 06/12/2025
9.0.5 131 05/16/2025
9.0.4 115 04/10/2025
9.0.3 132 04/01/2025
9.0.2 126 04/01/2025
9.0.1 123 04/01/2025
9.0.0 125 11/14/2024
9.0.0-rc.2.24473.5 155 10/16/2024
9.0.0-rc.1.24431.7 118 09/19/2024
9.0.0-preview.7.24405.7 124 09/02/2024
9.0.0-preview.6.24327.7 136 07/22/2024
9.0.0-preview.5.24306.7 149 07/22/2024
9.0.0-preview.4.24266.19 124 07/22/2024
9.0.0-preview.3.24172.9 146 07/22/2024
9.0.0-preview.2.24128.5 148 07/22/2024
9.0.0-preview.1.24080.9 137 07/22/2024
8.0.2 136 04/02/2025
8.0.1 187 07/22/2024
8.0.0 151 02/05/2024
8.0.0-rc.2.23479.6 136 07/22/2024
8.0.0-rc.1.23419.4 143 07/22/2024
8.0.0-preview.7.23375.6 141 07/22/2024
8.0.0-preview.6.23329.7 135 07/22/2024
8.0.0-preview.5.23280.8 138 07/22/2024
8.0.0-preview.4.23259.5 143 07/22/2024
8.0.0-preview.3.23174.8 139 07/22/2024
8.0.0-preview.2.23128.3 154 07/22/2024
8.0.0-preview.1.23110.8 138 07/22/2024
7.0.0 137 07/22/2024
7.0.0-rc.2.22472.3 141 07/22/2024
7.0.0-rc.1.22426.10 144 07/22/2024
7.0.0-preview.7.22375.6 149 07/22/2024
7.0.0-preview.6.22324.4 152 07/22/2024
7.0.0-preview.5.22301.12 147 07/22/2024
7.0.0-preview.4.22229.4 142 07/22/2024
7.0.0-preview.3.22175.4 136 07/22/2024
7.0.0-preview.2.22152.2 149 07/22/2024
7.0.0-preview.1.22076.8 139 07/22/2024
6.0.2-mauipre.1.22054.8 65 11/27/2025
6.0.1 141 07/22/2024
6.0.0 299 02/09/2024
6.0.0-rc.2.21480.5 148 07/22/2024
6.0.0-rc.1.21451.13 151 07/22/2024
6.0.0-preview.7.21377.19 138 07/22/2024
6.0.0-preview.6.21352.12 148 07/22/2024
6.0.0-preview.5.21301.5 132 07/22/2024
6.0.0-preview.4.21253.7 149 07/22/2024
6.0.0-preview.3.21201.4 148 07/22/2024
6.0.0-preview.2.21154.6 145 07/22/2024
6.0.0-preview.1.21102.12 149 07/22/2024
5.0.0 306 02/04/2024
5.0.0-rc.2.20475.5 139 07/22/2024
5.0.0-rc.1.20451.14 145 07/22/2024
5.0.0-preview.8.20407.11 149 07/22/2024
5.0.0-preview.7.20364.11 144 07/22/2024