Registry / serialization / asn1
library3.3.0pypypi✓ verified 24d ago

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 asn1
INSTALL
IMPORT
SIG · ASN1
A
asn1
serializationpythonv3.3.0
Install
1.6s avg
Import
18ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.018s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.018s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Encoder
from asn1 import Encoder
Decoder
from asn1 import Decoder
Numbers
from asn1 import Numbers

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.

import asn1 # 1. Encoding an ObjectIdentifier and a Sequence with asn1.Encoder() as encoder: # Encode an ObjectIdentifier (e.g., "1.2.3") encoder.write('1.2.3', asn1.Numbers.ObjectIdentifier) # Encode a Sequence containing an Integer and a UTF8String with encoder.enter(asn1.Numbers.Sequence) as sequence_encoder: sequence_encoder.write(123, asn1.Numbers.Integer) sequence_encoder.write('hello', asn1.Numbers.UTF8String) encoded_bytes = encoder.output() print(f"Encoded bytes: {encoded_bytes.hex()}") # 2. Decoding the ASN.1 data decoded_elements = [] with asn1.Decoder(stream=encoded_bytes) as decoder: # Decode the first element (ObjectIdentifier) tag, value = decoder.read() decoded_elements.append(f"Tag: {tag.nr} (ObjectIdentifier), Class: {tag.cls}, Value: {value}") # Decode the next element, which is the Sequence tag, value = decoder.read() if tag.nr == asn1.Numbers.Sequence: decoded_elements.append(f"Tag: {tag.nr} (Sequence), Class: {tag.cls}") # Enter the sequence to decode its elements with decoder.enter() as sequence_decoder: while not sequence_decoder.eof(): tag, value = sequence_decoder.read() decoded_elements.append(f" -> Tag: {tag.nr}, Class: {tag.cls}, Value: {value}") print("\nDecoded data:") for item in decoded_elements: print(item)
Debug
Known issues
breakingIn version 3.0.0, the `Encoder` incorrectly defaulted to CER encoding, diverging from previous versions which used DER. This was a significant behavioral change for users who did not explicitly specify the encoding method.
fix
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.
affects: 3.0.0
gotchaDue to ASN.1 having more distinct data types than Python, a single Python type might map to multiple ASN.1 types. The library uses the most frequently expected ASN.1 type by default. If a different ASN.1 type (e.g., 'IA5String' vs. 'UTF8String' for a Python string) is intended, it must be explicitly specified during encoding using `encoder.write(value, asn1.Numbers.DesiredType)`.
fix
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.
affects: All versions
gotchaVersion 3.2.0 introduced a new, more Pythonic context manager API for `Encoder` and `Decoder` (e.g., `with asn1.Encoder() as encoder:`). While the older `encoder.start()` / `encoder.output()` methods remain backward-compatible, the context manager approach is recommended for cleaner code and proper resource handling, especially for constructed types and streams.
fix
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.
affects: Prior to 3.2.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'asn1'
The 'asn1' module is not installed in the Python environment.
fix
Install the module using pip: 'pip install asn1'.
ImportError: cannot import name 'asn1' from 'cryptography.hazmat.bindings._rust' (unknown location)
The 'cryptography' package is not properly installed or is missing dependencies.
fix
Reinstall the 'cryptography' package ensuring all dependencies are met: 'pip install --force-reinstall cryptography'.
ImportError: cannot import name asn1
Attempting to import 'asn1' from 'pysnmp' which does not contain an 'asn1' module.
fix
Import 'asn1' directly: 'import asn1'.
asn1.Error: Premature end of input.
The decoder encountered incomplete or truncated ASN.1 data.
fix
Ensure the entire ASN.1 encoded data is read before decoding.
No field named 'r' defined for ASNBitcoinSignature
Incorrect import of 'Sequence' from 'asn1crypto.core' instead of 'pyasn1.type.univ'.
fix
Import 'Sequence' from 'pyasn1.type.univ': 'from pyasn1.type.univ import Sequence'.
Upgrade
Version history
3.3.0latest on PyPI · released May 15, 2026
Audit
Dependencies
futureoptionalRequired for Python 2.7 and 3.x compatibility in older environments. Not strictly needed for modern Python 3.5+ installations.
typingoptionalRequired for type hints in Python 2.7. Not needed for Python 3.5+ as 'typing' is part of the standard library.
Agent activity
33 hits · last 30 days
node
28
OpenAI (training)
1
Resources
asn1 — pip install asn1 · libregistry