From 98bbcb6cd6bba6c254ab216ab65584dbc591bb34 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 24 Aug 2017 18:39:38 +0200 Subject: [PATCH] Favour rsa module over PyCrypto since the former is pure Python --- README-long.rst => README.rst | 0 setup.py | 2 +- telethon/crypto/rsa.py | 16 ++++++++++++---- 3 files changed, 13 insertions(+), 5 deletions(-) rename README-long.rst => README.rst (100%) diff --git a/README-long.rst b/README.rst similarity index 100% rename from README-long.rst rename to README.rst diff --git a/setup.py b/setup.py index 9bb8b490..6e11c079 100755 --- a/setup.py +++ b/setup.py @@ -94,5 +94,5 @@ if __name__ == '__main__': 'telethon_generator', 'telethon_tests', 'run_tests.py', 'try_telethon.py' ]), - install_requires=['pyaes', 'pycrypto'] + install_requires=['pyaes', 'rsa'] ) diff --git a/telethon/crypto/rsa.py b/telethon/crypto/rsa.py index b624dddb..43340b4a 100644 --- a/telethon/crypto/rsa.py +++ b/telethon/crypto/rsa.py @@ -1,9 +1,10 @@ import os from hashlib import sha1 try: - from Crypto.PublicKey import RSA + import rsa + import rsa.core except ImportError: - raise ImportError('Missing module "pycrypto", please install via pip.') + raise ImportError('Missing module "rsa", please install via pip.') from ..extensions import BinaryWriter @@ -42,7 +43,7 @@ def _compute_fingerprint(key): def add_key(pub): """Adds a new public key to be used when encrypting new data is needed""" global _server_keys - key = RSA.importKey(pub) + key = rsa.PublicKey.load_pkcs1(pub) _server_keys[_compute_fingerprint(key)] = key @@ -57,7 +58,14 @@ def encrypt(fingerprint, data): # len(sha1.digest) is always 20, so we're left with 255 - 20 - x padding to_encrypt = sha1(data).digest() + data + os.urandom(235 - len(data)) - return key.encrypt(to_encrypt, 0)[0] + + # rsa module rsa.encrypt adds 11 bits for padding which we don't want + # rsa module uses rsa.transform.bytes2int(to_encrypt), easier way: + payload = int.from_bytes(to_encrypt, 'big') + encrypted = rsa.core.encrypt_int(payload, key.e, key.n) + # rsa module uses transform.int2bytes(encrypted, keylength), easier: + block = encrypted.to_bytes(256, 'big') + return block # Add default keys