TgCrypto
TgCrypto is a cryptography library written in C as a Python extension, designed for high performance and portability. It serves as a crucial component for Pyrogram, implementing Telegram's required cryptographic algorithms: AES-256-IGE, AES-256-CTR, and AES-256-CBC. The latest version is 1.2.5, released in November 2022. However, its official GitHub repository was archived in December 2024, and the project is explicitly stated as no longer maintained or supported by its original authors.
Warnings
- breaking API function names changed from `ige_encrypt`, `ige_decrypt`, `ctr_encrypt`, `ctr_decrypt` (in v1.0.x) to `ige256_encrypt`, `ige256_decrypt`, `ctr256_encrypt`, `ctr256_decrypt` (in v1.2.x). Code using older, non-suffixed function names will break.
- breaking The official `pyrogram/tgcrypto` GitHub repository was archived on December 23, 2024, and the project is explicitly stated as no longer maintained or supported by its original authors. This means no further updates, bug fixes, or security patches are expected from the original maintainers.
- gotcha Installing `tgcrypto` often requires C/C++ build tools on the system (e.g., Visual C++ Build Tools on Windows, Xcode Command Line Tools on macOS, `gcc`/`clang` and Python development headers (`python3-dev`) on Linux), especially if pre-compiled wheels are not available for your specific Python version and operating system. Installation may fail with compiler errors without these tools.
- gotcha When using AES-256-IGE mode, the input `data` *must* be explicitly padded to a multiple of the block size (16 bytes) before encryption. The library does not handle padding automatically. Failure to pre-pad the data will result in a `ValueError` during encryption or incorrect decryption.
Install
-
pip install tgcrypto
Imports
- tgcrypto
import tgcrypto
Quickstart
import os
import tgcrypto
# Data for IGE mode must be padded to a multiple of the block size (16 bytes)
# This example adds 7 bytes to demonstrate padding
data = os.urandom(10 * 1024 * 1024 + 7)
key = os.urandom(32) # AES-256 key (32 bytes)
iv = os.urandom(32) # AES-256-IGE IV (32 bytes)
# Pad with zeroes to be a multiple of 16 bytes
data += bytes(-len(data) % 16)
# Perform IGE encryption and decryption
ige_encrypted = tgcrypto.ige256_encrypt(data, key, iv)
ige_decrypted = tgcrypto.ige256_decrypt(ige_encrypted, key, iv)
assert data == ige_decrypted
print("IGE encryption/decryption successful!")