{"id":13613371,"url":"https://github.com/pyrogram/tgcrypto","last_synced_at":"2025-04-04T03:09:57.948Z","repository":{"id":29499990,"uuid":"119180048","full_name":"pyrogram/tgcrypto","owner":"pyrogram","description":"Fast and Portable Cryptography Extension Library for Pyrogram","archived":false,"fork":false,"pushed_at":"2024-02-11T07:00:08.000Z","size":144,"stargazers_count":189,"open_issues_count":5,"forks_count":47,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-10-29T16:13:32.716Z","etag":null,"topics":["aes","aes-cbc","aes-ctr","aes-ige","cipher","crypto","cryptography","encryption","hacktoberfest","mtproto","pyrogram","python","telegram"],"latest_commit_sha":null,"homepage":"https://pyrogram.org","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pyrogram.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-01-27T16:16:00.000Z","updated_at":"2024-10-28T11:51:17.000Z","dependencies_parsed_at":"2023-12-12T18:31:46.471Z","dependency_job_id":"86062da3-b254-42b1-bc04-5dc6cba8bba6","html_url":"https://github.com/pyrogram/tgcrypto","commit_stats":{"total_commits":67,"total_committers":4,"mean_commits":16.75,"dds":0.04477611940298509,"last_synced_commit":"7c7d865c02106188364f4f25a4fa8ffbe75286b3"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyrogram%2Ftgcrypto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyrogram%2Ftgcrypto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyrogram%2Ftgcrypto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyrogram%2Ftgcrypto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pyrogram","download_url":"https://codeload.github.com/pyrogram/tgcrypto/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246945081,"owners_count":20858878,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aes","aes-cbc","aes-ctr","aes-ige","cipher","crypto","cryptography","encryption","hacktoberfest","mtproto","pyrogram","python","telegram"],"created_at":"2024-08-01T20:00:45.245Z","updated_at":"2025-04-04T03:09:57.928Z","avatar_url":"https://github.com/pyrogram.png","language":"C","readme":"# TgCrypto\n\n\u003e [!NOTE]\n\u003e The project is no longer maintained or supported. Thanks for appreciating it.\n\n\u003e [!NOTE]\n\u003e The implementations of the algorithms presented in this repository are to be considered for educational purposes only.\n\n\u003e Fast and Portable Cryptography Extension Library for Pyrogram\n\n**TgCrypto** is a Cryptography Library written in C as a Python extension. It is designed to be portable, fast,\neasy to install and use. TgCrypto is intended for [Pyrogram](https://github.com/pyrogram/pyrogram) and implements the\ncryptographic algorithms Telegram requires, namely:\n\n- **`AES-256-IGE`** - used in [MTProto v2.0](https://core.telegram.org/mtproto).\n- **`AES-256-CTR`** - used for [CDN encrypted files](https://core.telegram.org/cdn).\n- **`AES-256-CBC`** - used for [encrypted passport credentials](https://core.telegram.org/passport).\n\n## Requirements\n\n- Python 3.7 or higher.\n\n## Installation\n\n``` bash\n$ pip3 install -U tgcrypto\n```\n\n## API\n\nTgCrypto API consists of these six methods:\n\n```python\ndef ige256_encrypt(data: bytes, key: bytes, iv: bytes) -\u003e bytes: ...\ndef ige256_decrypt(data: bytes, key: bytes, iv: bytes) -\u003e bytes: ...\n\ndef ctr256_encrypt(data: bytes, key: bytes, iv: bytes, state: bytes) -\u003e bytes: ...\ndef ctr256_decrypt(data: bytes, key: bytes, iv: bytes, state: bytes) -\u003e bytes: ...\n\ndef cbc256_encrypt(data: bytes, key: bytes, iv: bytes) -\u003e bytes: ...\ndef cbc256_decrypt(data: bytes, key: bytes, iv: bytes) -\u003e bytes: ...\n```\n\n## Usage\n\n### IGE Mode\n\n**Note**: Data must be padded to match a multiple of the block size (16 bytes).\n\n``` python\nimport os\n\nimport tgcrypto\n\ndata = os.urandom(10 * 1024 * 1024 + 7)  # 10 MB of random data + 7 bytes to show padding\nkey = os.urandom(32)  # Random Key\niv = os.urandom(32)  # Random IV\n\n# Pad with zeroes: -7 % 16 = 9\ndata += bytes(-len(data) % 16)\n\nige_encrypted = tgcrypto.ige256_encrypt(data, key, iv)\nige_decrypted = tgcrypto.ige256_decrypt(ige_encrypted, key, iv)\n\nprint(data == ige_decrypted)  # True\n```\n    \n### CTR Mode (single chunk)\n\n``` python\nimport os\n\nimport tgcrypto\n\ndata = os.urandom(10 * 1024 * 1024)  # 10 MB of random data\n\nkey = os.urandom(32)  # Random Key\n\nenc_iv = bytearray(os.urandom(16))  # Random IV\ndec_iv = enc_iv.copy()  # Keep a copy for decryption\n\nctr_encrypted = tgcrypto.ctr256_encrypt(data, key, enc_iv, bytes(1))\nctr_decrypted = tgcrypto.ctr256_decrypt(ctr_encrypted, key, dec_iv, bytes(1))\n\nprint(data == ctr_decrypted)  # True\n```\n\n### CTR Mode (stream)\n\n``` python\nimport os\nfrom io import BytesIO\n\nimport tgcrypto\n\ndata = BytesIO(os.urandom(10 * 1024 * 1024))  # 10 MB of random data\n\nkey = os.urandom(32)  # Random Key\n\nenc_iv = bytearray(os.urandom(16))  # Random IV\ndec_iv = enc_iv.copy()  # Keep a copy for decryption\n\nenc_state = bytes(1)  # Encryption state, starts from 0\ndec_state = bytes(1)  # Decryption state, starts from 0\n\nencrypted_data = BytesIO()  # Encrypted data buffer\ndecrypted_data = BytesIO()  # Decrypted data buffer\n\nwhile True:\n    chunk = data.read(1024)\n\n    if not chunk:\n        break\n\n    # Write 1K encrypted bytes into the encrypted data buffer\n    encrypted_data.write(tgcrypto.ctr256_encrypt(chunk, key, enc_iv, enc_state))\n\n# Reset position. We need to read it now\nencrypted_data.seek(0)\n\nwhile True:\n    chunk = encrypted_data.read(1024)\n\n    if not chunk:\n        break\n\n    # Write 1K decrypted bytes into the decrypted data buffer\n    decrypted_data.write(tgcrypto.ctr256_decrypt(chunk, key, dec_iv, dec_state))\n\nprint(data.getvalue() == decrypted_data.getvalue())  # True\n```\n\n### CBC Mode\n\n**Note**: Data must be padded to match a multiple of the block size (16 bytes).\n\n``` python\nimport os\n\nimport tgcrypto\n\ndata = os.urandom(10 * 1024 * 1024 + 7)  # 10 MB of random data + 7 bytes to show padding\nkey = os.urandom(32)  # Random Key\n\nenc_iv = bytearray(os.urandom(16))  # Random IV\ndec_iv = enc_iv.copy()  # Keep a copy for decryption\n\n# Pad with zeroes: -7 % 16 = 9\ndata += bytes(-len(data) % 16)\n\ncbc_encrypted = tgcrypto.cbc256_encrypt(data, key, enc_iv)\ncbc_decrypted = tgcrypto.cbc256_decrypt(cbc_encrypted, key, dec_iv)\n\nprint(data == cbc_decrypted)  # True\n```\n\n## Testing\n\n1. Clone this repository: `git clone https://github.com/pyrogram/tgcrypto`.\n2. Enter the directory: `cd tgcrypto`.\n3. Install `tox`: `pip3 install tox`\n4. Run tests: `tox`.\n\n## License\n\n[LGPLv3+](COPYING.lesser) © 2017-present [Dan](https://github.com/delivrance)\n","funding_links":[],"categories":["C"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpyrogram%2Ftgcrypto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpyrogram%2Ftgcrypto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpyrogram%2Ftgcrypto/lists"}