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 Chaubeyankitchaubey.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.1.tar.gz (19.8 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.1-cp312-cp312-win_amd64.whl (23.5 kB view details)

Uploaded CPython 3.12Windows x86-64

cryptogram-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

cryptogram-0.1.1-cp312-cp312-macosx_11_0_arm64.whl (15.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

cryptogram-0.1.1-cp311-cp311-win_amd64.whl (23.5 kB view details)

Uploaded CPython 3.11Windows x86-64

cryptogram-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (50.1 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

cryptogram-0.1.1-cp311-cp311-macosx_11_0_arm64.whl (15.7 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

cryptogram-0.1.1-cp310-cp310-win_amd64.whl (23.5 kB view details)

Uploaded CPython 3.10Windows x86-64

cryptogram-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.2 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

cryptogram-0.1.1-cp310-cp310-macosx_11_0_arm64.whl (15.7 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

cryptogram-0.1.1-cp39-cp39-win_amd64.whl (23.5 kB view details)

Uploaded CPython 3.9Windows x86-64

cryptogram-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.0 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

cryptogram-0.1.1-cp39-cp39-macosx_11_0_arm64.whl (15.7 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

cryptogram-0.1.1-cp38-cp38-win_amd64.whl (23.4 kB view details)

Uploaded CPython 3.8Windows x86-64

cryptogram-0.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.4 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

cryptogram-0.1.1-cp38-cp38-macosx_11_0_arm64.whl (15.6 kB view details)

Uploaded CPython 3.8macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for cryptogram-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0fd3acca715af42bfeb6ac89b1c474a7524f845aea3c3b9230a43fa6a5ca3657
MD5 d13c3e85f22d95c77a080a5bbfa0a826
BLAKE2b-256 6822d708ce335e202b360840f034466caba991b3f469471acb323c32e2b923be

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.1.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.1-cp312-cp312-win_amd64.whl.

File metadata

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

File hashes

Hashes for cryptogram-0.1.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c646c28164889ad1f174205a75dc8bfd994acffda0f90e6b3e3214fe3b763206
MD5 c0534a86d6e4ba9c904c2b254ac7e367
BLAKE2b-256 bb8088fdf98b6c1d0ffab5c32788f004b02464b82ea23757c10001714492fb18

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.1-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.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0328b288862ba4a21ce56574a8d7678191e9eb5fb39c3d1579efb112e3c5add7
MD5 9dfb2a934c8e6d18b863beefc79db06f
BLAKE2b-256 b31ea8a78c9683a65daf670540388e896f5d860d7febffa621303c35e424dc69

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.1-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.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a08a15b639fcd0c1d33010b4f401f930901d1c3a8eeb04c6f215400a7c093d43
MD5 76995db09ae057bedb595600f50233ef
BLAKE2b-256 f5958a996113b0e9e1c2ce6d2a90db4c8f583c38b7aecfa4c90253158f5d18f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.1-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.1-cp311-cp311-win_amd64.whl.

File metadata

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

File hashes

Hashes for cryptogram-0.1.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3b722e7e0740311978ee93bef25c752e4246746e3d59983f1e5245d55390d67d
MD5 259d36780225db6daa08c332bf8edbc9
BLAKE2b-256 26b9326d72c4105a18523551ffed3eb895b6913ed1a0abf7749fc9c3b4097670

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.1-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.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7fb15b94944c9b124983e42b66a70fe177dacb7a98f4f5b0285bca10e6f1229c
MD5 0399c85f77e87394cbaeb5b3fb898ede
BLAKE2b-256 1e51d0986d7433876c0b06392beb2300d52de7e3ac65b678c12810d48166a5ea

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.1-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.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 05d1e689e51c1f19ccf37b5cc0e271e5b327ca47ce5150f3343525a0510a8ae3
MD5 0a4571e3d130248212d862b31e7645b4
BLAKE2b-256 604955953304c3d67341a53c912ed0fd1d19017bf9186485e605356987f05e5b

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.1-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.1-cp310-cp310-win_amd64.whl.

File metadata

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

File hashes

Hashes for cryptogram-0.1.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 5ee4e81709248398cf23c5b61ffc2355186a607afd97cd08823e7173d6d5ef58
MD5 9ae0c1af3d9cf0e8c451465f20acfac4
BLAKE2b-256 3d9f157b44a941242cd647d1c646e709c1aa662114c82507f6b66997cc38c5eb

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.1-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.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 76b3f79068362e4d556bc00c8025b03a64c37c80158289040d3956a222cc1875
MD5 b7ab630019b93a841ed3fb118ff7e73b
BLAKE2b-256 3a704877c9f0b4233384bab22d04abb3dff2b75fcc829d2d2b77a740e1963db6

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.1-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.1-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c3999b3abdf75e64ddfa9c0eb57dbf1d3d78ee55753f57acca4fe978eab29be2
MD5 2ea37f2e60032f92e26cba96ce778266
BLAKE2b-256 3b9fae5b6240f56862506ee10a5b0d327650191df77aea1f91f54da2d9168912

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.1-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.1-cp39-cp39-win_amd64.whl.

File metadata

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

File hashes

Hashes for cryptogram-0.1.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 6b37f0288499e1bf252c3b48c6a110825875b86ac845863125ed4ea8cd3de71e
MD5 c18b793d52a837c068e6c54194d85f20
BLAKE2b-256 10cf137cee2915423403e66ad4d3ea2a52944ea620590d68eee31f0425b4cefc

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.1-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.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ecc5a1268991f98eb3fc8a44ef91cf85b956195e43de0c780ea1891967261f25
MD5 ee49cfdb09f0474465a55a0bf9ebbb4c
BLAKE2b-256 08cb88e57c5c870efcc4c4108e946243787e62e609d3c46ac2a0db2c9c75d710

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.1-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.1-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 10a1f5955cbe717e0d2c88703f4265a7604db054f3a7bebfcdfcc4a6c67dcfa5
MD5 d1c5794e1bcbc7fe3bd3e0368810cd35
BLAKE2b-256 7e78894feefeed0d8899ae9abefe1ad78f3d07026550cf5a64d64f19ed2ad59b

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.1-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.1-cp38-cp38-win_amd64.whl.

File metadata

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

File hashes

Hashes for cryptogram-0.1.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 1cde87ea1232ad3acd4d468d2fab8435a74216516dc152c862029f804b8ced8e
MD5 06b64d706d045eecf46122c93c705eb1
BLAKE2b-256 4044edede2ead65cf9d2e10a787f12ea6b8a290249c114ceae0ea431a8b41301

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.1-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.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b32e569e86b59629fd4d8b8fce97f03880b600f61abd7b0df3e8398feb3d817b
MD5 5feed7a1e8207057a5e044983d4eda40
BLAKE2b-256 f157111915f7c990f77a1195b44897b3862675022783ae19fa610d588524aa52

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.1-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.1-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cryptogram-0.1.1-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bc558ded4a88b5445b88420f629d542a52e5e792c74ded756ee6860a38bf98ce
MD5 019a885bca7e1d013f16d7b321072595
BLAKE2b-256 7bad1b13bee5db21567cd25cf2e2c4012ca01a6f95f961d18ad003aec44b1ddd

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptogram-0.1.1-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