Python-RSA

repository·main·Indexed 19 days ago

https://github.com/sybrenstuvel/python-rsa

A pure-Python implementation of the RSA algorithm supporting PKCS#1 v1.5 for encryption, decryption, signing, and verification, as well as multi-prime encryption according to PKCS#1 v2.1. The library provides high-level functions for cryptographic operations, key generation, and a command-line interface. Note: This project has been archived by its maintainer and is not secure against timing attacks.

Tokens
12K
Snippets
52
Records
70
Agent score
67%

What's inside python-rsa

  1. Overview of Python-RSA capabilities

    main

    Python-RSA is a pure-Python implementation of the RSA algorithm. It provides the following core cryptographic functionalities:

    • Encryption and Decryption: Standard RSA operations.
    • Signing and Verification: Digital signatures.
    • Key Generation: Supports PKCS#1 version 1.5.
    • Multi-prime Encryption: Implements multi-prime encryption according to PKCS#1 version 2.1.
  2. How to handle large files with RSA

    main

    Because RSA cannot encrypt data larger than its key size, you should use Hybrid Encryption. This involves using a symmetric block cipher (like AES) for the actual data and RSA only to protect the symmetric key.

    Recommended Workflow:

    1. Generate a random symmetric key (e.g., using rsa.randnum.read_random_bits).
    2. Encrypt the large file using the symmetric key and a library like cryptography or PyCryptodome (Python-RSA does not provide AES/DES3 functionality).
    3. Encrypt the symmetric key using the recipient's RSA public key.
    4. Send both the encrypted file and the encrypted symmetric key to the recipient.

    Note: The VARBLOCK format is deprecated and has been removed in version 4.0 due to security vulnerabilities. Do not use it.

    import rsa.randnum
    
    # 1. Generate a random 128-bit key for AES
    aes_key = rsa.randnum.read_random_bits(128)
    
    # 2. (User must use an external library like AES to encrypt the file with aes_key)
    # ...
    
    # 3. Encrypt the AES key with RSA public key
    encrypted_aes_key = rsa.encrypt(aes_key, public_rsa_key)
    
    # 4. Send encrypted_file + encrypted_aes_key
  3. Security considerations for Python-RSA

    main

    While Python-RSA uses industry-standard random padding and has undergone security review, users should be aware of potential attack vectors:

    • Information Leakage: Displaying the stack trace of an rsa.pkcs1.CryptoError exception can leak information regarding why decryption or verification failed.
    • Input Processing: The implementation does not compress the input stream to remove repetitions.

    Users should exercise their own judgment to determine if this module meets the security requirements of their specific application.

  4. Compatibility with RSA standards

    main

    Python-RSA implements encryption and signatures according to PKCS#1 version 1.5. It also supports multiprime encryption according to PKCS#1 version 2.1.

    Key formats and capabilities:

    • Encryption: PKCS#1 v1.5 with at least 8 bytes of random padding.
    • Signatures: PKCS#1 v1.5 using MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA3-256, SHA3-384, or SHA3-512.
    • Private Keys: PKCS#1 v1.5 in PEM or DER format (ASN.1 type RSAPrivateKey). Compatible with OpenSSL.
    • Public Keys: PKCS#1 v1.5 in PEM or DER format (ASN.1 type RSAPublicKey). Note that OpenSSL's X.509 public keys are not supported.
    • VARBLOCK encryption: Deprecated in version 3.4 and removed in version 4.0.
  5. Warning: The VARBLOCK file format is deprecated and insecure

    main

    The VARBLOCK file format was used to encrypt files larger than the RSA key.

    CRITICAL: The VARBLOCK format is NOT recommended for general use. It has been deprecated since Python-RSA 3.4 and was removed in version 4.0 because it is vulnerable to several attacks. Do not use this format in new projects.

  6. Convert PKCS#8 keys to PKCS#1 for Python-RSA

    main

    Python-RSA supports PKCS#1 v1.5, but does not support the more complex PKCS#8 standard. If you have a private key in PKCS#8 format, you must use an external tool like OpenSSL to convert it to PKCS#1 before using it with Python-RSA.

    # Convert a PKCS#8 private key to PKCS#1 format
    openssl rsa -in privatekey-pkcs8.pem -out privatekey.pem
  7. Use the Python-RSA command line interface

    main
    Python-RSA provides several command-line scripts for common RSA operations. On Linux and Unix-like systems, these are executable Python scripts; on Windows, they are provided as .exe files. All scripts support the --help parameter to display usage instructions.
  8. Publish a new release to PyPI

    main

    Publishing to PyPI requires an API token rather than a username and password.

    1. Generate an API token at https://pypi.org/manage/account/token/.
    2. Configure your ~/.pypirc file using __token__ as the username and the full token (including the pypi- prefix) as the password.
    3. Build the package and upload using poetry and twine.

    Note: This project is archived. If you are a maintainer/contributor attempting to publish, ensure you follow the specific token requirements for the rsa repository.

    [rsa]
      repository = https://upload.pypi.org/legacy/
      username = __token__
      password = pypi-token
    . ./.venv/bin/activate
    
    poetry build
    twine check dist/rsa-4.10-dev0.tar.gz dist/rsa-4.10-dev0-*.whl
    twine upload -r rsa dist/rsa-4.10-dev0.tar.gz dist/rsa-4.10-dev0-*.whl