Python-ASN1 is a straightforward library for encoding and decoding ASN.1 data, compatible with Python 2.7+ and 3.5+. It supports both BER (Basic Encoding Rules) for parsing and DER (Distinguished Encoding Rules) for parsing and generation, including indefinite lengths. The library is written entirely in Python and supports most common ASN.1 types, including the REAL type. The latest version is 3.2.0, with a regular release cadence addressing new Python versions and API enhancements.
pip install asn1Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates encoding and decoding a simple ObjectIdentifier and a Sequence containing an Integer and a UTF8String. It utilizes the modern context manager API introduced in version 3.2.0 for both encoding and decoding, which simplifies resource management and constructed type handling.
Upgrade to version 3.0.1 or later. If staying on 3.0.0, explicitly specify `asn1.Encoder.DER` when initializing the encoder or `start()` method if DER is desired, or be aware that CER will be used by default without a stream, and DER with a stream.
Always explicitly specify the ASN.1 `Numbers` type (e.g., `asn1.Numbers.IA5String`) when encoding a Python value if the default mapping is not the desired ASN.1 type.
Migrate encoding and decoding logic to use the `with asn1.Encoder(...) as encoder:` and `with asn1.Decoder(...) as decoder:` syntax for improved readability and maintainability. Review the documentation for examples of the new API.
Install the module using pip: 'pip install asn1'.
Reinstall the 'cryptography' package ensuring all dependencies are met: 'pip install --force-reinstall cryptography'.
Import 'asn1' directly: 'import asn1'.
Ensure the entire ASN.1 encoded data is read before decoding.
Import 'Sequence' from 'pyasn1.type.univ': 'from pyasn1.type.univ import Sequence'.