Skip to main content

Ultra-fast AES-256 cryptography for Telegram MTProto - AES-NI accelerated, API-compatible with tgcrypto and cryptg

Project description

cryptogram ⚡

Ultra-fast AES-256 cryptography for Telegram MTProto - hardware AES-NI accelerated.

PyPI Python License: MIT

Created by Ankit Chaubey - ankitchaubey.dev@gmail.com
GitHub: github.com/ankit-chaubey/cryptogram


Why cryptogram?

Feature cryptogram tgcrypto cryptg
AES-256-IGE ✓
AES-256-CTR ✓
AES-256-CBC ✓
PQ Factorisation
tgcrypto API
cryptg API
AES-NI hardware ✅ OpenSSL ✅ Rust
CBC speed ~1 GB/s ~170 MB/s
CTR speed ~900 MB/s ~140 MB/s
Pure-Python fallback
Build-time deps none none Rust toolchain

Performance (vs tgcrypto, same machine)

CBC-dec   cryptogram: 5079 MB/s   tgcrypto: 213 MB/s   →  24×  faster ✓
CBC-enc   cryptogram:  974 MB/s   tgcrypto: 174 MB/s   →   6×  faster ✓
CTR       cryptogram:  908 MB/s   tgcrypto: 141 MB/s   →   6×  faster ✓
IGE-enc   cryptogram:   96 MB/s   tgcrypto: 162 MB/s   →  same AES-NI¹

¹ IGE is inherently sequential (each block depends on the previous), so throughput is similar to tgcrypto. For all modes that can be parallelised, cryptogram wins decisively.


Installation

pip install cryptogram

Requires Python 3.8+. OpenSSL must be installed (it is by default on all major OS).
No Rust toolchain required.


Usage

Drop-in replacement for tgcrypto

import cryptogram as tgcrypto   # 100% API-compatible

# IGE (used heavily in Telegram MTProto)
encrypted = cryptogram.ige256_encrypt(data, key, iv)   # key=32B, iv=32B
decrypted = cryptogram.ige256_decrypt(encrypted, key, iv)

# CTR (Telegram file downloads)
iv    = bytearray(16)
state = bytearray(1)
encrypted = cryptogram.ctr256_encrypt(data, key, iv, state)
decrypted = cryptogram.ctr256_decrypt(encrypted, key, iv, state)

# CBC
encrypted = cryptogram.cbc256_encrypt(data, key, iv)   # iv=16B
decrypted = cryptogram.cbc256_decrypt(encrypted, key, iv)

Drop-in replacement for cryptg (Telethon)

import cryptogram as cryptg   # 100% API-compatible

encrypted = cryptogram.encrypt_ige(plain, key, iv)
decrypted = cryptogram.decrypt_ige(cipher, key, iv)
p, q      = cryptogram.factorize_pq_pair(pq)

Use with Telethon

# In your project, just install cryptogram - Telethon auto-detects it
pip install cryptogram

# Or explicitly:
import cryptogram
# Telethon checks for `cryptg`, so alias it:
import sys
sys.modules['cryptg'] = cryptogram

Use with Pyrogram

# Install cryptogram - Pyrogram checks for tgcrypto automatically
pip install cryptogram

# Or alias:
import sys
import cryptogram
sys.modules['tgcrypto'] = cryptogram

Extra utilities

import cryptogram

print(cryptogram.has_aesni())     # True  - hardware AES-NI active
print(cryptogram.get_backend())   # "C/AES-NI"

How it works

  • C extension (_cryptogram.c) loaded at import time
  • Dynamically links OpenSSL libcrypto at runtime - no compile-time headers needed
  • OpenSSL uses hardware AES-NI instructions automatically on x86/x86_64
  • CTR & CBC are run in bulk using EVP_CipherUpdate which pipelines multiple AES-NI rounds in parallel, giving 6–24× throughput vs single-block approaches
  • IGE is sequential by the spec; each 16-byte block depends on the previous
  • PQ factorisation uses Brent's improvement on Pollard's ρ with deterministic Miller-Rabin primality (guaranteed correct for all 64-bit semi-primes)
  • Pure-Python fallback activates automatically if the C extension can't be imported (uses the cryptography PyPI package as backend)

API Reference

tgcrypto-compatible

Function Signature Description
ige256_encrypt (data, key, iv) → bytes AES-256-IGE encrypt. key=32B, iv=32B, len(data)%16==0
ige256_decrypt (data, key, iv) → bytes AES-256-IGE decrypt
ctr256_encrypt (data, key, iv, state) → bytes AES-256-CTR encrypt. iv=bytearray(16), state=bytearray(1)
ctr256_decrypt (data, key, iv, state) → bytes AES-256-CTR decrypt (same as encrypt)
cbc256_encrypt (data, key, iv) → bytes AES-256-CBC encrypt. iv=16B
cbc256_decrypt (data, key, iv) → bytes AES-256-CBC decrypt

cryptg-compatible

Function Signature Description
encrypt_ige (plain, key, iv) → bytes AES-256-IGE encrypt
decrypt_ige (cipher, key, iv) → bytes AES-256-IGE decrypt
factorize_pq_pair (pq: int) → (int, int) Factorise semi-prime pq into (p, q) where p ≤ q

Extra

Function Description
has_aesni() → bool Whether CPU supports AES-NI hardware instructions
get_backend() → str Active backend: "C/AES-NI", "C/table", or "Python/cryptography"

Testing

# Run correctness tests (28 tests, including NIST vectors)
python tests/test_cryptogram.py

# Run benchmark
python tests/test_cryptogram.py --benchmark

License

MIT - Copyright © 2024 Ankit Chaubey
See LICENSE for full text.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cryptogram-0.1.2.tar.gz (18.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

cryptogram-0.1.2-cp312-cp312-win_amd64.whl (22.3 kB view details)

Uploaded CPython 3.12Windows x86-64

cryptogram-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (48.9 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

cryptogram-0.1.2-cp312-cp312-macosx_11_0_arm64.whl (14.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

cryptogram-0.1.2-cp311-cp311-win_amd64.whl (22.3 kB view details)

Uploaded CPython 3.11Windows x86-64

cryptogram-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.3 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

cryptogram-0.1.2-cp311-cp311-macosx_11_0_arm64.whl (14.9 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

cryptogram-0.1.2-cp310-cp310-win_amd64.whl (22.3 kB view details)

Uploaded CPython 3.10Windows x86-64

cryptogram-0.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (48.4 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

cryptogram-0.1.2-cp310-cp310-macosx_11_0_arm64.whl (14.9 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

cryptogram-0.1.2-cp39-cp39-win_amd64.whl (22.2 kB view details)

Uploaded CPython 3.9Windows x86-64

cryptogram-0.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (48.2 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

cryptogram-0.1.2-cp39-cp39-macosx_11_0_arm64.whl (14.9 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

cryptogram-0.1.2-cp38-cp38-win_amd64.whl (22.2 kB view details)

Uploaded CPython 3.8Windows x86-64

cryptogram-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (48.6 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

cryptogram-0.1.2-cp38-cp38-macosx_11_0_arm64.whl (14.8 kB view details)

Uploaded CPython 3.8macOS 11.0+ ARM64

File details

Details for the file cryptogram-0.1.2.tar.gz.

File metadata

  • Download URL: cryptogram-0.1.2.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cryptogram-0.1.2.tar.gz
Algorithm Hash digest
SHA256 66cc52291d5b802ba905d5886c94cf1f4dce7ceff4787db8ec0fa1b652286c5f
MD5 a9f5bd7351455655c33e9b8bf59a82f8
BLAKE2b-256 1c1fbc7180e7bb86cb930c510f5efc6449e764e7586e927db37d960a4e468fa5

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.2.tar.gz:

Publisher: publish.yml on ankit-chaubey/cryptogram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cryptogram-0.1.2-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: cryptogram-0.1.2-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 22.3 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cryptogram-0.1.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f7e8934bbd277cc8cc26f038edafeec755baeccd740ecbefe2399e109a4f4e7e
MD5 47caef2a7e2380aaee7d5ddff590245b
BLAKE2b-256 aba2665da4dccec49af093c29ec7cd6522b832740f21ee2e214672932781166e

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.2-cp312-cp312-win_amd64.whl:

Publisher: publish.yml on ankit-chaubey/cryptogram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cryptogram-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d4e4e73edd1270a725ebb27819a7d39faf731d113ab8c7eb566b0627ce76993b
MD5 4d9d6c92d17e1cd7b30b2b61289a2766
BLAKE2b-256 8577b706c77e2408f9c103ce767c1a75f173018ef64b9fe30a2bd49edef474a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on ankit-chaubey/cryptogram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cryptogram-0.1.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 10983a7d369f7329ccbdebe8bbdd8b094758270e87f3c0fc68a1e0270848241d
MD5 7fd8ce72616ba734e60fdbabe9d1c8cc
BLAKE2b-256 abcf72a5d6aff32c1d3fcd4e3d4d52e0d55d75241f2297a34e490c169de5623e

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.2-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: publish.yml on ankit-chaubey/cryptogram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cryptogram-0.1.2-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: cryptogram-0.1.2-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 22.3 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cryptogram-0.1.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 0a40cf33eefb00366f79eca7f89374d71d1db138d340bd476b3c5de04f01e250
MD5 359017025d42344d7e92e02670fc6f3b
BLAKE2b-256 207ab026ed0b53291cf6192a7d7a23ea42355f35b8d8aaebb24af90cb62c2993

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.2-cp311-cp311-win_amd64.whl:

Publisher: publish.yml on ankit-chaubey/cryptogram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cryptogram-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 da4e6cfb4cb38713048e51fa89278e08b056167e80a96ababf842586f185cb90
MD5 5afa9b3bf461e32663be8517ab2b58de
BLAKE2b-256 b7912cbbdac05ca7654f8933234668193a92dd87868da91f764ebeeb2be9b9a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on ankit-chaubey/cryptogram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cryptogram-0.1.2-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 eb0d39047b2db340dac05d8fe481734204c6e501471c908559909befa41c1601
MD5 b4044ebfe527674120ab8adc9ee195ef
BLAKE2b-256 9cdea99a2dbd4324627eaf45efa198e1bc2ae242d7e784109388ffb3b018b917

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.2-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: publish.yml on ankit-chaubey/cryptogram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cryptogram-0.1.2-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: cryptogram-0.1.2-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 22.3 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cryptogram-0.1.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 1f4217a6239c1120ccb285255996d3d79e31f31a7b5ee58c0f054ed1be8421b4
MD5 c2f3257bf4edb68bc21d1264384ead7f
BLAKE2b-256 56d0b1c9d430223e6def38be3ecb595543005a717aea21c395fb48bbc39b7443

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.2-cp310-cp310-win_amd64.whl:

Publisher: publish.yml on ankit-chaubey/cryptogram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cryptogram-0.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1cae7ec58785750fb479bb50d52b69656749e1b71bfd4e403f8e4bba75e1d12f
MD5 6b9c64d7f019258c1546e2e63d3300c9
BLAKE2b-256 4e26ef3814d88fd4e23e65ba6d9586487e75f42d9e3e42ef74b77b293f008213

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on ankit-chaubey/cryptogram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cryptogram-0.1.2-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8661d27741a135452e96b99cc0da86dfd62b7b40e2ca7d028e719429cf369f90
MD5 17d8cf86404dd614f5fb6014f818a888
BLAKE2b-256 012c376aeeb515eaa009eb451a04b51b9c2862ff9131656c6ec1b1abaaa831b2

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.2-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: publish.yml on ankit-chaubey/cryptogram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cryptogram-0.1.2-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: cryptogram-0.1.2-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cryptogram-0.1.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 32fcf6bca91f4bb0c6e5160046be23212faa5c2f2d01b87550bd45744a7cac2b
MD5 10a9730c6f322e0a57cbb3a9caa5d145
BLAKE2b-256 fe30b00e52cdfde520e5bea8cd11c1d8138e15558875179bf54c6c9f3ad70b49

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.2-cp39-cp39-win_amd64.whl:

Publisher: publish.yml on ankit-chaubey/cryptogram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cryptogram-0.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 423ce6d9ad765ea63585a32b85b668a22c81cbc355ed329099c07ee3c893f6d6
MD5 a6d6e60fc67bb8b63d9f124b5e948468
BLAKE2b-256 62cc528e45c20df21f53ff0a3a8cf6df9c3756b798a49a30e1a31dc97a89b5dc

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on ankit-chaubey/cryptogram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cryptogram-0.1.2-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 aeb748f17ce3396505792b7a1c304672289d226289864964b138c125c1120821
MD5 2ee001c650f79c315a44445bc3cb3ffd
BLAKE2b-256 9af30aca5e4645693e59ecb27573b06c5e6180f3e401f969a994d43d2bc52229

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.2-cp39-cp39-macosx_11_0_arm64.whl:

Publisher: publish.yml on ankit-chaubey/cryptogram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cryptogram-0.1.2-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: cryptogram-0.1.2-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cryptogram-0.1.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 e125bc3ee5efc841f7c7683c83bad20ae7f6a64956e12bd4fec1bba5ac459bb0
MD5 1fd225c45fd0172fc295fbbf7662f613
BLAKE2b-256 b6c7eeab91815f17be27fee2a9af328fe0aa9a48c73b0d722a9596f5dbd4bc7b

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.2-cp38-cp38-win_amd64.whl:

Publisher: publish.yml on ankit-chaubey/cryptogram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cryptogram-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 afff0966e3743de2dc09273d75f6097e13ee72ec0a641a531d40f1ffbe2d2034
MD5 0f095769849b4e8c57bd0cface0e5c41
BLAKE2b-256 1ec03745135465e587a5075c8bcd9fa11cacfc175a4422f32ad4211bf5c2a775

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on ankit-chaubey/cryptogram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cryptogram-0.1.2-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.2-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 842dc213f1a2b80b3e4dcb09f4bf3f09bb2e7a1ed3a513e4ab3e93910bef570e
MD5 bbf035940f83ff32937b23300e8e24aa
BLAKE2b-256 ec968bec11723c4ee5af2fe9bd958ba91f03a38838d70111469556c585c6aa80

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.2-cp38-cp38-macosx_11_0_arm64.whl:

Publisher: publish.yml on ankit-chaubey/cryptogram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page