Skip to main content

Python-ASN1 is a simple ASN.1 encoder and decoder for Python 2.7 and 3.5+.

Features

  • Support BER (parser) and DER (parser and generator) encoding (including indefinite lengths)

  • 100% python, compatible with version 2.7, 3.5 and higher

  • Can be integrated by just including a file into your project

  • Support most common ASN.1 types including REAL (encoding and decoding).

Dependencies

Python-ASN1 relies on Python-Future for Python 2 and 3 compatibility. To install Python-Future:

pip install future

Python-ASN1 relies on type hints. For Python 2.7, a backport of the standard library typing module has to be installed:

pip install typing

This is not necessary for Python 3.5 and higher since it is part of the standard library.

How to install Python-asn1

Install from PyPi with the following:

pip install asn1

or download the repository from GitHub and install with the following:

python setup.py install

You can also simply include asn1.py into your project.

How to use Python-asn1

Encoding

If you want to encode data and retrieve its DER-encoded representation, use code such as:

import asn1

encoder = asn1.Encoder()
encoder.start()
encoder.write('1.2.3', asn1.Numbers.ObjectIdentifier)
encoded_bytes = encoder.output()

Decoding

If you want to decode ASN.1 from DER or BER encoded bytes, use code such as:

import asn1

decoder = asn1.Decoder()
decoder.start(encoded_bytes)
tag, value = decoder.read()

Documentation

The complete documentation is available on Read The Docs:

python-asn1.readthedocs.io

Contributing

Contributions to Python-ASN1 are welcome. If you want to contribute, please fork the repository on GitHub, make your changes and submit a pull request (PR). Please make sure that your code is properly tested and that all tests pass. In particular, tests should pass for old versions of Python (2.7 and from 3.5 to 3.14). You can use the provided Docker file to test your code on all those versions of Python.

License

Python-ASN1 is free software that is made available under the MIT license. Consult the file LICENSE that is distributed together with this library for the exact licensing terms.

Changelog

3.3.0 (2026-05-15)

  • Fix #298 - Incorrect handling of indefinite form length when using ENTER/LEAVE

  • Fix #296 - Support for writing IMPLICIT values

3.2.0 (2026-02-06)

  • New API using a context manager
    • Support for using Encoder via context manager

    • Support for encoding sequences via context manager

    • Support for encoding sets via context manager

    • Support for using Decoder via context manager

  • Add support of Python 3.14

  • New way of installing old versions of Python in the Docker file. As a consequence, all versions of Python including 2.7 and from 3.5 to 3.14 are now properly tested.

3.1.0 (2025-05-16)

  • #291 - Add support for mypy. As a consequence, the previous module has been migrated to a package.

3.0.1 (2025-05-02)

  • Fix #288 - Use DER encoding by default as in previous versions

3.0.0 (2025-03-03)

  • Encoding and decoding of the ASN.1 REAL type

  • Support of indefinite lengths

  • Encoding and decoding of complex data (lists, sets, …)

  • Add support for streams (file-like objects) when encoding and decoding

  • Optionally return the number of unused bits when decoding a BitString (see also #276)

  • #286 - Add ASN.1:2008 compliance test suite

2.8.0 (2025-02-20)

  • PR #285 - Use Enum with Tag for pretty representation (thanks to @Julien00859)

  • Make explicit that Python 3.12 and 3.13 are supported

  • Fix some links in the documentation

  • Add a Docker file for testing

2.7.1 (2024-08-07)

  • Fix OID encoding/decoding for the first octet according to ITU-T X.690 (thanks to Ian Neal)

2.7.0 (2023-01-17)

  • Add context manager support (thanks to Mastermind-U)

2.6.0 (2022-07-15)

  • Add support for GeneralizedTime (thanks to vollkorntomate)

2.5.0 (2022-03-03)

  • Fixes to BitString decoding and encoding of IA5String and UTCTime (thanks to 0xbf00)

2.4.2 (2021-10-29)

  • Fix a minor mistake in the dump.py example

  • Add Python 3.9 and 3.10

2.4.1 (2020-07-16)

  • Fix #89 - Replace explicit references to enum34 by enum-compat

2.4.0 (2020-06-23)

  • Fix #21 - Invalid decoding in non-Universal classes

  • Fix #57 - Invalid encoding of non-Universal classes

2.3.1 (2020-04-06)

  • No change in code, only in packaging and in texts (README, …)

2.3.0 (2020-04-05)

  • Tests: Replace nose by pytest

  • Add Python 3.8, remove Python 3.4 support

  • PR#26 (from rumbah): Add test for default (implicit) encoding types

  • PR#25 (from thomwiggers): Attempt to support BIT STRING

  • Fix wrong example code, see #27

  • (from jcrowgey) Makes the package usable with pip install

  • Remove support of Python 3.3 (some dependencies do not support 3.3)

  • PR#15 (from ThePlasmaRailgun) Fix parsing of object identifier

  • PR#10 (from robinleander): Allow user to determine decoding tagtype

2.2.0 (2017-10-30)

  • Use “true” enums instead of classes. Use enun34 backport for old Python versions.

2.1.1 (2017-10-30)

  • Fix a bug (#9): two’s complement corner case with values such as -32769. Add new test cases to test them.

2.1.0 (2016-12-18)

  • Add more documentation

  • Use (simulated) enumerations

  • Add Python 2.6 in automated checks and tests

  • Add type hints (for static checking) and fix some code

2.0.0 (2016-12-16)

  • First public release by Sebastien Andrivet

  • Support both python 2 and 3 (with Python-Future)

  • All strings are now in unicode

  • Add more ASN.1 tags (like PrintableString)

  • Fix errors in the example (dump.py)

  • Code reorganization

0.9 (2011-05-18)

  • Initial public release by Geert Jansen

Metadata

Release files for asn1 3.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asn1 3.3.0
File Size Uploaded Provenance
asn1-3.3.0.tar.gz 51.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for asn1 3.3.0
File Interpreter ABI Platform
asn1-3.3.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 69.6 kB

Release files / asn1-3.3.0.tar.gz

Download URL asn1-3.3.0.tar.gz
Size 51.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0493484b714418c75ffa3810883c3ce2570d6210cea9ce98e3e616b1b99bc494
BLAKE2b-256 checksum
How to use checksums
41cd771b167a69ca15d19a551c9b941fea40c41b3788f2fa0fe2d472a3f596de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.9

Release files / asn1-3.3.0-py2.py3-none-any.whl

Download URL asn1-3.3.0-py2.py3-none-any.whl
Size 17.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
091c675274fa79535efdd3efd55deff1c3e5dc16e9026a0a683eb5e3bf37cc96
BLAKE2b-256 checksum
How to use checksums
edfaf14b74ccfbc55167077545d66e31ce0cb53022914f491fcb3d578ad17757
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.9

Security for asn1 3.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page