System.Formats.Asn1 9.0.2

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
Oracle.ManagedDataAccess
Managed Oracle Data Provider for .NET (ODP.NET) features optimized ADO.NET data access to the Oracle database for .NET Framework and is 100% managed code. ODP.NET allows developers to take advantage of advanced Oracle database functionality, including Real Application Clusters, self-tuning statement cache, Application Continuity, JSON Relational Duality, and Fast Connection Failover.
22
Oracle.ManagedDataAccess
ODP.NET, Managed Driver is a 100% native code .NET Framework driver for Oracle Database. No additional Oracle Client software is required to be installed to connect to Oracle Database.
25
Oracle.ManagedDataAccess.Core
Oracle Data Provider for .NET (ODP.NET) Core is a multi-platform ADO.NET driver that provides fast data access from Microsoft .NET (Core) clients to Oracle databases. ODP.NET Core consists of a single 100% managed code dynamic-link library.
25
Oracle.ManagedDataAccess.Core
Oracle Data Provider for .NET (ODP.NET) Core is a multi-platform ADO.NET driver that provides fast data access from Microsoft .NET (Core) clients to Oracle databases. ODP.NET Core consists of a single 100% managed code dynamic-link library.
41
Oracle.ManagedDataAccess.Core
Oracle Data Provider for .NET (ODP.NET) Core is an ADO.NET driver that provides fast data access from Microsoft .NET Core clients to Oracle databases. ODP.NET Core consists of a single 100% managed code dynamic-link library.
22
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.
24
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.
783
System.Security.Cryptography.OpenSsl
Provides cryptographic algorithm implementations and key management for non-Windows systems with OpenSSL. Commonly Used Types: System.Security.Cryptography.RSAOpenSsl When using NuGet 3.x this package requires at least version 3.4.
23
System.Security.Cryptography.OpenSsl
Provides cryptographic algorithm implementations and key management for non-Windows systems with OpenSSL. Commonly Used Types: System.Security.Cryptography.RSAOpenSsl When using NuGet 3.x this package requires at least version 3.4.
26
System.Security.Cryptography.OpenSsl
Provides cryptographic algorithm implementations and key management for non-Windows systems with OpenSSL. Commonly Used Types: System.Security.Cryptography.RSAOpenSsl When using NuGet 3.x this package requires at least version 3.4.
36
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
26
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
43
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
62
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
315
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.
28
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.
30
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.
864

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.6.25358.103 0 07/15/2025
10.0.0-preview.5.25277.114 4 06/07/2025
10.0.0-preview.4.25258.110 6 05/19/2025
10.0.0-preview.3.25171.5 5 04/23/2025
10.0.0-preview.2.25163.2 11 03/26/2025
10.0.0-preview.1.25080.5 14 03/05/2025
9.0.7 0 07/08/2025
9.0.6 4 06/15/2025
9.0.5 4 05/18/2025
9.0.4 12 04/23/2025
9.0.3 10 03/26/2025
9.0.2 12 03/01/2025
9.0.1 12 01/21/2025
9.0.0 19 11/17/2024
9.0.0-rc.2.24473.5 20 10/09/2024
9.0.0-rc.1.24431.7 19 09/17/2024
9.0.0-preview.7.24405.7 19 08/14/2024
9.0.0-preview.6.24327.7 19 07/17/2024
9.0.0-preview.5.24306.7 16 06/17/2024
9.0.0-preview.4.24266.19 39 05/24/2024
9.0.0-preview.3.24172.9 22 04/11/2024
9.0.0-preview.2.24128.5 24 03/15/2024
9.0.0-preview.1.24080.9 23 03/08/2024
8.0.2 15 03/05/2025
8.0.1 21 07/17/2024
8.0.0 23 02/10/2024
8.0.0-rc.2.23479.6 29 02/07/2024
8.0.0-rc.1.23419.4 29 02/07/2024
8.0.0-preview.7.23375.6 25 02/07/2024
8.0.0-preview.6.23329.7 29 02/07/2024
8.0.0-preview.5.23280.8 21 02/07/2024
8.0.0-preview.4.23259.5 19 02/07/2024
8.0.0-preview.3.23174.8 23 02/07/2024
8.0.0-preview.2.23128.3 26 02/07/2024
8.0.0-preview.1.23110.8 43 02/07/2024
7.0.0 22 02/10/2024
7.0.0-rc.2.22472.3 22 02/07/2024
7.0.0-rc.1.22426.10 28 02/09/2024
7.0.0-preview.7.22375.6 27 02/07/2024
7.0.0-preview.6.22324.4 25 02/07/2024
7.0.0-preview.5.22301.12 46 02/11/2024
7.0.0-preview.4.22229.4 26 02/07/2024
7.0.0-preview.3.22175.4 21 02/07/2024
7.0.0-preview.2.22152.2 43 02/07/2024
7.0.0-preview.1.22076.8 26 02/07/2024
6.0.2-mauipre.1.22102.15 28 02/11/2024
6.0.2-mauipre.1.22054.8 24 02/07/2024
6.0.1 21 07/12/2024
6.0.0 373 12/15/2021
6.0.0-rc.2.21480.5 30 02/07/2024
6.0.0-rc.1.21451.13 48 02/09/2024
6.0.0-preview.7.21377.19 23 02/11/2024
6.0.0-preview.6.21352.12 30 02/11/2024
6.0.0-preview.5.21301.5 24 02/07/2024
6.0.0-preview.4.21253.7 26 02/07/2024
6.0.0-preview.3.21201.4 22 02/07/2024
6.0.0-preview.2.21154.6 27 02/07/2024
6.0.0-preview.1.21102.12 24 02/11/2024
5.0.0 449 11/30/2020
5.0.0-rc.2.20475.5 25 02/07/2024
5.0.0-rc.1.20451.14 28 02/09/2024
5.0.0-preview.8.20407.11 27 02/11/2024
5.0.0-preview.7.20364.11 27 02/11/2024