telethon.crypto package

telethon.crypto.aes module

AES IGE implementation in Python. This module may use libssl if available.

class telethon.crypto.aes.AES

Bases: object

Class that servers as an interface to encrypt and decrypt text through the AES IGE mode.

static decrypt_ige(cipher_text, key, iv)

Decrypts the given text in 16-bytes blocks by using the given key and 32-bytes initialization vector.

static encrypt_ige(plain_text, key, iv)

Encrypts the given text in 16-bytes blocks by using the given key and 32-bytes initialization vector.

telethon.crypto.aes_ctr module

This module holds the AESModeCTR wrapper class.

class telethon.crypto.aes_ctr.AESModeCTR(key, iv)

Bases: object

Wrapper around pyaes.AESModeOfOperationCTR mode with custom IV


Decrypts the given cipher text through AES CTR

Parameters:data – the cipher text to be decrypted.
Returns:the decrypted plain text.

Encrypts the given plain text through AES CTR.

Parameters:data – the plain text to be encrypted.
Returns:the encrypted cipher text.

telethon.crypto.auth_key module

This module holds the AuthKey class.

class telethon.crypto.auth_key.AuthKey(data)

Bases: object

Represents an authorization key, used to encrypt and decrypt messages sent to Telegram’s data centers.

calc_new_nonce_hash(new_nonce, number)

Calculates the new nonce hash based on the current attributes.

  • new_nonce – the new nonce to be hashed.
  • number – number to prepend before the hash.

the hash for the given new nonce.

telethon.crypto.cdn_decrypter module

This module holds the CdnDecrypter utility class.

class telethon.crypto.cdn_decrypter.CdnDecrypter(cdn_client, file_token, cdn_aes, cdn_file_hashes)

Bases: object

Used when downloading a file results in a ‘FileCdnRedirect’ to both prepare the redirect, decrypt the file as it downloads, and ensure the file hasn’t been tampered.

static check(data, cdn_hash)

Checks the integrity of the given data. Raises CdnFileTamperedError if the integrity check fails.

  • data – the data to be hashed.
  • cdn_hash – the expected hash.

Calls GetCdnFileRequest and decrypts its bytes. Also ensures that the file hasn’t been tampered.

Returns:the CdnFile result.
static prepare_decrypter(client, cdn_client, cdn_redirect)

Prepares a new CDN decrypter.

  • client – a TelegramClient connected to the main servers.
  • cdn_client – a new client connected to the CDN.
  • cdn_redirect – the redirect file object that caused this call.

(CdnDecrypter, first chunk file data)

telethon.crypto.factorization module

This module holds a fast Factorization class.

class telethon.crypto.factorization.Factorization

Bases: object

Simple module to factorize large numbers really quickly.

classmethod factorize(pq)

Factorizes the given large integer.

Parameters:pq – the prime pair pq.
Returns:a tuple containing the two factors p and q.
static gcd(a, b)

Calculates the Greatest Common Divisor.

  • a – the first number.
  • b – the second number.

GCD(a, b)

telethon.crypto.rsa module

This module holds several utilities regarding RSA and server fingerprints.


Adds a new public key to be used when encrypting new data is needed

telethon.crypto.rsa.encrypt(fingerprint, data)

Encrypts the given data known the fingerprint to be used in the way Telegram requires us to do so (sha1(data) + data + padding)

  • fingerprint – the fingerprint of the RSA key.
  • data – the data to be encrypted.

the cipher text, or None if no key matching this fingerprint is found.


Return the variable length bytes corresponding to the given int