System.Formats.Asn1 9.0.0

Prefix Reserved
dotnet add package System.Formats.Asn1 --version 9.0.0                
NuGet\Install-Package System.Formats.Asn1 -Version 9.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="System.Formats.Asn1" Version="9.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add System.Formats.Asn1 --version 9.0.0                
#r "nuget: System.Formats.Asn1, 9.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install System.Formats.Asn1 as a Cake Addin
#addin nuget:?package=System.Formats.Asn1&version=9.0.0

// Install System.Formats.Asn1 as a Cake Tool
#tool nuget:?package=System.Formats.Asn1&version=9.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.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (265)

Showing the top 5 NuGet packages that depend on System.Formats.Asn1:

Package Downloads
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.

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.

System.Security.Cryptography.Pkcs

Provides support for PKCS and CMS algorithms. Commonly Used Types: System.Security.Cryptography.Pkcs.EnvelopedCms

Microsoft.EntityFrameworkCore.SqlServer

Microsoft SQL Server database provider for Entity Framework Core.

Microsoft.VisualStudio.Web.CodeGenerators.Mvc

Code Generators for ASP.NET Core MVC. Contains code generators for MVC Controllers and Views.

GitHub repositories (44)

Showing the top 5 popular GitHub repositories that depend on System.Formats.Asn1:

Repository Stars
PowerShell/PowerShell
PowerShell for every system!
MonoGame/MonoGame
One framework for creating powerful cross-platform games.
dotnet/machinelearning
ML.NET is an open source and cross-platform machine learning framework for .NET.
unoplatform/uno
Open-source platform for building cross-platform native Mobile, Web, Desktop and Embedded apps quickly. Create rich, C#/XAML, single-codebase apps from any IDE. Hot Reload included! 90m+ NuGet Downloads!!
elsa-workflows/elsa-core
A .NET workflows library
Version Downloads Last updated
9.0.0 136,957 11/12/2024
9.0.0-rc.2.24473.5 97,730 10/8/2024
9.0.0-rc.1.24431.7 23,720 9/10/2024
9.0.0-preview.7.24405.7 14,649 8/13/2024
9.0.0-preview.6.24327.7 57,405 7/9/2024
9.0.0-preview.5.24306.7 7,043 6/11/2024
9.0.0-preview.4.24266.19 11,547 5/21/2024
9.0.0-preview.3.24172.9 86,245 4/11/2024
9.0.0-preview.2.24128.5 26,079 3/12/2024
9.0.0-preview.1.24080.9 33,648 2/13/2024
8.0.1 16,673,575 7/9/2024
8.0.0 44,251,673 11/14/2023 8.0.0 has at least one vulnerability with high severity.
8.0.0-rc.2.23479.6 699,919 10/10/2023 8.0.0-rc.2.23479.6 has at least one vulnerability with high severity.
8.0.0-rc.1.23419.4 143,836 9/12/2023 8.0.0-rc.1.23419.4 has at least one vulnerability with high severity.
8.0.0-preview.7.23375.6 257,942 8/8/2023 8.0.0-preview.7.23375.6 has at least one vulnerability with high severity.
8.0.0-preview.6.23329.7 10,996 7/11/2023 8.0.0-preview.6.23329.7 has at least one vulnerability with high severity.
8.0.0-preview.5.23280.8 22,106 6/13/2023 8.0.0-preview.5.23280.8 has at least one vulnerability with high severity.
8.0.0-preview.4.23259.5 195,642 5/16/2023 8.0.0-preview.4.23259.5 has at least one vulnerability with high severity.
8.0.0-preview.3.23174.8 36,846 4/11/2023 8.0.0-preview.3.23174.8 has at least one vulnerability with high severity.
8.0.0-preview.2.23128.3 22,884 3/14/2023 8.0.0-preview.2.23128.3 has at least one vulnerability with high severity.
8.0.0-preview.1.23110.8 64,696 2/21/2023 8.0.0-preview.1.23110.8 has at least one vulnerability with high severity.
7.0.0 50,992,683 11/7/2022 7.0.0 has at least one vulnerability with high severity.
7.0.0-rc.2.22472.3 65,695 10/11/2022 7.0.0-rc.2.22472.3 has at least one vulnerability with high severity.
7.0.0-rc.1.22426.10 70,556 9/14/2022 7.0.0-rc.1.22426.10 has at least one vulnerability with high severity.
7.0.0-preview.7.22375.6 17,516 8/9/2022 7.0.0-preview.7.22375.6 has at least one vulnerability with high severity.
7.0.0-preview.6.22324.4 20,991 7/12/2022 7.0.0-preview.6.22324.4 has at least one vulnerability with high severity.
7.0.0-preview.5.22301.12 2,686 6/14/2022 7.0.0-preview.5.22301.12 has at least one vulnerability with high severity.
7.0.0-preview.4.22229.4 18,347 5/10/2022 7.0.0-preview.4.22229.4 has at least one vulnerability with high severity.
7.0.0-preview.3.22175.4 3,648 4/13/2022 7.0.0-preview.3.22175.4 has at least one vulnerability with high severity.
7.0.0-preview.2.22152.2 20,563 3/14/2022 7.0.0-preview.2.22152.2 has at least one vulnerability with high severity.
7.0.0-preview.1.22076.8 5,764 2/17/2022 7.0.0-preview.1.22076.8 has at least one vulnerability with high severity.
6.0.1 1,260,107 7/9/2024
6.0.0 152,406,392 11/8/2021 6.0.0 has at least one vulnerability with high severity.
6.0.0-rc.2.21480.5 22,616 10/12/2021 6.0.0-rc.2.21480.5 has at least one vulnerability with high severity.
6.0.0-rc.1.21451.13 15,181 9/14/2021 6.0.0-rc.1.21451.13 has at least one vulnerability with high severity.
6.0.0-preview.7.21377.19 18,494 8/10/2021 6.0.0-preview.7.21377.19 has at least one vulnerability with high severity.
6.0.0-preview.6.21352.12 7,241 7/14/2021 6.0.0-preview.6.21352.12 has at least one vulnerability with high severity.
6.0.0-preview.5.21301.5 7,572 6/15/2021 6.0.0-preview.5.21301.5 has at least one vulnerability with high severity.
6.0.0-preview.4.21253.7 189,855 5/24/2021 6.0.0-preview.4.21253.7 has at least one vulnerability with high severity.
6.0.0-preview.3.21201.4 18,100 4/8/2021 6.0.0-preview.3.21201.4 has at least one vulnerability with high severity.
6.0.0-preview.2.21154.6 25,290 3/11/2021 6.0.0-preview.2.21154.6 has at least one vulnerability with high severity.
6.0.0-preview.1.21102.12 12,749 2/12/2021 6.0.0-preview.1.21102.12 has at least one vulnerability with high severity.
5.0.0 314,537,958 11/9/2020 5.0.0 has at least one vulnerability with high severity.
5.0.0-rc.2.20475.5 19,778 10/13/2020 5.0.0-rc.2.20475.5 has at least one vulnerability with high severity.
5.0.0-rc.1.20451.14 63,535 9/14/2020 5.0.0-rc.1.20451.14 has at least one vulnerability with high severity.
5.0.0-preview.8.20407.11 81,703 8/25/2020 5.0.0-preview.8.20407.11 has at least one vulnerability with high severity.
5.0.0-preview.7.20364.11 16,848 7/21/2020 5.0.0-preview.7.20364.11 has at least one vulnerability with high severity.