System.Formats.Asn1 9.0.0-rc.2.24473.5

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.Build.Tasks.Core
This package contains the Microsoft.Build.Tasks assembly which implements the commonly used tasks of MSBuild.
6
NuGet.Packaging
NuGet's understanding of packages. Reading nuspec, nupkgs and package signing.
4
MailKit
MailKit is an Open Source cross-platform .NET mail-client library that is based on MimeKit and optimized for mobile devices. Features include: * HTTP, Socks4, Socks4a and Socks5 proxy support. * SASL Authentication via ANONYMOUS, CRAM-MD5, DIGEST-MD5, LOGIN, NTLM, OAUTHBEARER, PLAIN, SCRAM-SHA-1, SCRAM-SHA-256, SCRAM-SHA-512 and XOAUTH2. * A fully-cancellable SmtpClient with support for STARTTLS, 8BITMIME, BINARYMIME, ENHANCEDSTATUSCODES, SIZE, DSN, PIPELINING and SMTPUTF8. * A fully-cancellable Pop3Client with support for STLS, UIDL, APOP, PIPELINING, UTF8, and LANG. * A fully-cancellable ImapClient with support for ACL, QUOTA, LITERAL+, IDLE, NAMESPACE, ID, CHILDREN, LOGINDISABLED, STARTTLS, MULTIAPPEND, UNSELECT, UIDPLUS, CONDSTORE, ESEARCH, SASL-IR, COMPRESS, WITHIN, ENABLE, QRESYNC, SORT, THREAD, ANNOTATE, LIST-EXTENDED, ESORT, METADATA / METADATA-SERVER, NOTIFY, FILTERS, LIST-STATUS, SORT=DISPLAY, SPECIAL-USE / CREATE-SPECIAL-USE, SEARCH=FUZZY, MOVE, UTF8=ACCEPT / UTF8=ONLY, LITERAL-, APPENDLIMIT, STATUS=SIZE, OBJECTID, REPLACE, SAVEDATE, XLIST, and X-GM-EXT1. * Client-side sorting and threading of messages (the Ordinal Subject and the Jamie Zawinski threading algorithms are supported). * Asynchronous versions of all methods that hit the network. * S/MIME, OpenPGP, DKIM and ARC support via MimeKit. * Microsoft TNEF support via MimeKit.
4
Microsoft.Bcl.Cryptography
Provides support for some cryptographic primitives for .NET Framework and .NET Standard.
4
Microsoft.Build.Tasks.Core
This package contains the Microsoft.Build.Tasks assembly which implements the commonly used tasks of MSBuild.
4
NuGet.Packaging
NuGet's understanding of packages. Reading nuspec, nupkgs and package signing.
3
EPPlus
A spreadsheet library for .NET framework and .NET core
3
Microsoft.Build.Tasks.Core
This package contains the Microsoft.Build.Tasks assembly which implements the commonly used tasks of MSBuild.
3
Microsoft.EntityFrameworkCore.SqlServer.HierarchyId
Adds hierarchyid support to the SQL Server EF Core provider
3
MimeKit
MimeKit is an Open Source library for creating and parsing MIME, S/MIME and PGP messages on desktop and mobile platforms. It also supports parsing of Unix mbox files. Unlike any other .NET MIME parser, MimeKit's parser does not need to parse string input nor does it use a TextReader. Instead, it parses raw byte streams, thus allowing it to better support undeclared 8bit text in headers as well as message bodies. It also means that MimeKit's parser is significantly faster than other .NET MIME parsers. MimeKit's parser also uses a real tokenizer when parsing the headers rather than regex or string.Split() like most other .NET MIME parsers. This means that MimeKit is much more RFC-compliant than any other .NET MIME parser out there, including the commercial implementations. In addition to having a far superior parser implementation, MimeKit's object tree is not a derivative of System.Net.Mail objects and thus does not suffer from System.Net.Mail's limitations. API documentation can be found on the web at http://www.mimekit.net/docs For those that need SMTP, POP3 or IMAP support, check out https://github.com/jstedfast/MailKit
3
OPCFoundation.NetStandard.Opc.Ua.Security.Certificates
OPC UA Security X509 Certificates Class Library
3
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.
3

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
10.0.0-preview.5.25277.114 0 2025/6/6
10.0.0-preview.4.25258.110 2 2025/5/26
10.0.0-preview.3.25171.5 1 2025/6/4
10.0.0-preview.2.25163.2 1 2025/5/28
10.0.0-preview.1.25080.5 1 2025/5/27
9.0.5 1 2025/5/28
9.0.4 0 2025/4/8
9.0.3 0 2025/3/11
9.0.2 1 2025/6/7
9.0.1 1 2025/6/6
9.0.0 0 2024/11/12
9.0.0-rc.2.24473.5 1 2025/5/30
9.0.0-rc.1.24431.7 0 2024/9/10
9.0.0-preview.7.24405.7 1 2025/5/27
9.0.0-preview.6.24327.7 1 2025/5/28
9.0.0-preview.5.24306.7 1 2025/6/4
9.0.0-preview.4.24266.19 1 2025/6/3
9.0.0-preview.3.24172.9 1 2025/6/4
9.0.0-preview.2.24128.5 1 2025/6/2
9.0.0-preview.1.24080.9 1 2025/5/27
8.0.2 1 2025/6/6
8.0.1 1 2025/6/6
8.0.0 0 2023/11/14
8.0.0-rc.2.23479.6 1 2025/6/4
8.0.0-rc.1.23419.4 1 2025/6/5
8.0.0-preview.7.23375.6 1 2025/6/2
8.0.0-preview.6.23329.7 1 2025/5/28
8.0.0-preview.5.23280.8 1 2025/5/27
8.0.0-preview.4.23259.5 1 2025/6/3
8.0.0-preview.3.23174.8 1 2025/5/28
8.0.0-preview.2.23128.3 1 2025/5/27
8.0.0-preview.1.23110.8 1 2025/5/28
7.0.0 1 2025/6/4
7.0.0-rc.2.22472.3 1 2025/6/4
7.0.0-rc.1.22426.10 0 2022/9/14
7.0.0-preview.7.22375.6 1 2025/5/28
7.0.0-preview.6.22324.4 1 2025/5/27
7.0.0-preview.5.22301.12 1 2025/6/3
7.0.0-preview.4.22229.4 1 2025/5/27
7.0.0-preview.3.22175.4 1 2025/5/27
7.0.0-preview.2.22152.2 1 2025/6/3
7.0.0-preview.1.22076.8 1 2025/5/28
6.0.1 1 2025/6/7
6.0.0 0 2021/11/8
6.0.0-rc.2.21480.5 1 2025/6/6
6.0.0-rc.1.21451.13 1 2025/6/6
6.0.0-preview.7.21377.19 1 2025/5/28
6.0.0-preview.6.21352.12 1 2025/5/28
6.0.0-preview.5.21301.5 1 2025/5/27
6.0.0-preview.4.21253.7 1 2025/5/27
6.0.0-preview.3.21201.4 1 2025/5/27
6.0.0-preview.2.21154.6 1 2025/5/28
6.0.0-preview.1.21102.12 1 2025/6/3
5.0.0 0 2020/11/9
5.0.0-rc.2.20475.5 1 2025/6/6
5.0.0-rc.1.20451.14 2 2025/5/26
5.0.0-preview.8.20407.11 2 2025/6/1
5.0.0-preview.7.20364.11 1 2025/5/27