System.Formats.Asn1 11.0.0

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
System.Security.Cryptography.OpenSsl
Provides cryptographic algorithm implementations and key management for non-Windows systems with OpenSSL. Commonly Used Types: System.Security.Cryptography.RSAOpenSsl
176
System.Security.Cryptography.OpenSsl
Provides cryptographic algorithm implementations and key management for non-Windows systems with OpenSSL. Commonly Used Types: System.Security.Cryptography.RSAOpenSsl
169
System.Security.Cryptography.OpenSsl
Provides cryptographic algorithm implementations and key management for non-Windows systems with OpenSSL. Commonly Used Types: System.Security.Cryptography.RSAOpenSsl
167
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.
164
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.
163
Microsoft.Bcl.Cryptography
Provides support for some cryptographic primitives for .NET Framework and .NET Standard.
162
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.
157
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
155
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.
155
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
152
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
149
Microsoft.DotNet.Scaffolding.Shared
Contains interfaces for Project Model and messaging for scaffolding.
147
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
147
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.
146
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.
146
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
145
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
144
System.Security.Cryptography.Pkcs
Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms
143

.NET 10.0

  • No dependencies.

.NET 11.0

  • No dependencies.

.NET Framework 4.6.2

.NET Standard 2.0

Version Downloads Last updated
11.0.0 4 11/10/2026
11.0.0-rc.3.26522.148 11 10/26/2026
11.0.0-rc.2.26504.105 13 10/14/2026
11.0.0-rc.1.26425.128 23 9/10/2026
11.0.0-preview.7.26381.103 22 8/12/2026
11.0.0-preview.6.26359.118 26 7/17/2026
11.0.0-preview.5.26302.115 31 6/12/2026
11.0.0-preview.4.26230.115 38 5/13/2026
11.0.0-preview.3.26207.106 31 5/3/2026
11.0.0-preview.2.26159.112 40 3/12/2026
11.0.0-preview.1.26104.118 37 2/14/2026
10.0.15 4 11/10/2026
10.0.14 9 10/27/2026
10.0.13 17 10/14/2026
10.0.12 31 9/9/2026
10.0.11 20 8/12/2026
10.0.10 29 7/18/2026
10.0.9 36 6/13/2026
10.0.8 41 5/13/2026
10.0.7 32 5/3/2026
10.0.6 37 5/3/2026
10.0.5 39 3/16/2026
10.0.4 48 3/12/2026
10.0.3 40 2/14/2026
10.0.2 48 1/20/2026
10.0.1 50 12/11/2025
10.0.0 71 11/13/2025
10.0.0-rc.2.25502.107 77 10/15/2025
10.0.0-rc.1.25451.107 89 9/10/2025
10.0.0-preview.7.25380.108 88 8/14/2025
10.0.0-preview.6.25358.103 85 7/21/2025
10.0.0-preview.5.25277.114 92 6/9/2025
10.0.0-preview.4.25258.110 108 5/15/2025
10.0.0-preview.3.25171.5 92 4/11/2025
10.0.0-preview.2.25163.2 104 3/18/2025
10.0.0-preview.1.25080.5 122 2/26/2025
9.0.23 4 11/10/2026
9.0.22 9 10/27/2026
9.0.21 23 10/14/2026
9.0.20 22 9/10/2026
9.0.19 25 8/12/2026
9.0.18 29 7/18/2026
9.0.17 26 6/13/2026
9.0.16 40 5/13/2026
9.0.15 35 5/3/2026
9.0.14 46 3/12/2026
9.0.13 37 2/14/2026
9.0.12 39 1/20/2026
9.0.11 74 11/11/2025
9.0.10 89 10/15/2025
9.0.9 73 9/10/2025
9.0.8 86 8/6/2025
9.0.7 109 7/11/2025
9.0.6 97 6/11/2025
9.0.5 107 5/16/2025
9.0.4 105 4/11/2025
9.0.3 117 3/13/2025
9.0.2 155 2/14/2025
9.0.1 121 1/22/2025
9.0.0 145 11/13/2024
9.0.0-rc.2.24473.5 123 10/20/2024
9.0.0-rc.1.24431.7 117 9/12/2024
9.0.0-preview.7.24405.7 127 8/19/2024
9.0.0-preview.6.24327.7 131 7/10/2024
9.0.0-preview.5.24306.7 130 6/14/2024
9.0.0-preview.4.24266.19 128 6/4/2024
9.0.0-preview.3.24172.9 135 4/13/2024
9.0.0-preview.2.24128.5 133 3/26/2024
9.0.0-preview.1.24080.9 131 2/26/2024
8.0.2 136 2/14/2025
8.0.1 165 7/10/2024
8.0.0 139 11/16/2023
8.0.0-rc.2.23479.6 153 10/29/2023
8.0.0-rc.1.23419.4 130 9/15/2023
8.0.0-preview.7.23375.6 131 8/24/2023
8.0.0-preview.6.23329.7 132 7/31/2023
8.0.0-preview.5.23280.8 158 8/1/2023
8.0.0-preview.4.23259.5 141 8/1/2023
8.0.0-preview.3.23174.8 139 5/12/2023
8.0.0-preview.2.23128.3 141 4/4/2023
8.0.0-preview.1.23110.8 152 2/23/2023
7.0.0 168 12/4/2022
7.0.0-rc.2.22472.3 134 12/4/2022
7.0.0-rc.1.22426.10 140 9/15/2022
7.0.0-preview.7.22375.6 133 9/15/2022
7.0.0-preview.6.22324.4 148 9/15/2022
7.0.0-preview.5.22301.12 134 6/27/2022
7.0.0-preview.4.22229.4 146 6/27/2022
7.0.0-preview.3.22175.4 133 6/27/2022
7.0.0-preview.2.22152.2 146 6/27/2022
7.0.0-preview.1.22076.8 136 6/27/2022
6.0.2-mauipre.1.22102.15 140 6/27/2022
6.0.2-mauipre.1.22054.8 145 6/27/2022
6.0.1 154 7/10/2024
6.0.0 178 6/27/2022
6.0.0-rc.2.21480.5 140 6/27/2022
6.0.0-rc.1.21451.13 134 6/27/2022
6.0.0-preview.7.21377.19 146 6/27/2022
6.0.0-preview.6.21352.12 144 6/27/2022
6.0.0-preview.5.21301.5 151 6/27/2022
6.0.0-preview.4.21253.7 131 6/27/2022
6.0.0-preview.3.21201.4 146 6/27/2022
6.0.0-preview.2.21154.6 140 6/27/2022
6.0.0-preview.1.21102.12 155 6/27/2022
5.0.0 142 6/27/2022
5.0.0-rc.2.20475.5 138 6/27/2022
5.0.0-rc.1.20451.14 162 6/27/2022
5.0.0-preview.8.20407.11 146 6/27/2022
5.0.0-preview.7.20364.11 147 6/27/2022