### Installing RSA Library from Source (Shell) Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/installation.rst After downloading the source code, navigate to the source directory and run this command. It executes the standard Python setup script to build and install the library on your system. ```Shell python setup.py install ``` -------------------------------- ### Setting Up RSA Development Environment with Pipenv (Shell) Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/installation.rst After cloning the repository, navigate into the project directory and use Pipenv to install the required development dependencies. This command creates a virtual environment and installs packages listed in the project's Pipfile. ```Shell cd python-rsa pipenv install --dev ``` -------------------------------- ### Installing RSA Library via Pip (Shell) Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/installation.rst This command installs the python-rsa library package globally or locally using the pip package manager. Use 'sudo' for system-wide installation or '--user' for installation in the user's home directory. ```Shell pip install rsa ``` -------------------------------- ### Setting up Development Environment Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/README.md These commands outline the steps to set up a development environment for the Python-RSA project. It involves creating a virtual environment using `venv`, activating it, installing the `poetry` dependency manager, and then installing project dependencies via poetry. ```Shell python3 -m venv .venv . ./.venv/bin/activate pip install poetry poetry install ``` -------------------------------- ### Installing RSA Library Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/README.md This command demonstrates how to install the `rsa` Python library from the Python Package Index (PyPI) using the pip package manager. This is the standard method for users to add the library to their Python environment. ```Shell pip install rsa ``` -------------------------------- ### Cloning RSA Git Repository for Development (Shell) Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/installation.rst This Git command clones the official python-rsa source code repository from GitHub to your local machine. It is the first step to obtain the source code for development contributions. ```Shell git clone https://github.com/sybrenstuvel/python-rsa.git ``` -------------------------------- ### Configuring PyPI API Token ~/.pypirc Source: https://github.com/sybrenstuvel/python-rsa/blob/main/README.md This snippet provides an example configuration for the `~/.pypirc` file, used by tools like `twine` for uploading packages to PyPI. It configures an upload server named `rsa` and specifies the use of an API token (`__token__` as username and the token itself as password) for authentication. ```INI [distutils] index-servers = rsa # Use `twine upload -r rsa` to upload with this token. [rsa] repository = https://upload.pypi.org/legacy/ username = __token__ password = pypi-token ``` -------------------------------- ### Publishing New Release to PyPI Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/README.md These commands detail the process for publishing a new release of the Python-RSA library to PyPI. It requires activating the development virtual environment, building the distribution packages using poetry, verifying the built files with twine, and finally uploading them to PyPI using twine and the configured API token. ```Shell . ./.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 ``` -------------------------------- ### Generating OpenSSL RSA Private Key (Shell) Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/compatibility.rst Generates a 512-bit RSA private key using the OpenSSL command-line tool and saves it to a specified file in PEM format. This key can serve as the basis for interoperability examples or be converted for direct use with python-rsa. ```Shell openssl genrsa -out myprivatekey.pem 512 ``` -------------------------------- ### Generating RSA Keys with Multiple Primes Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/usage.rst Generates an RSA key pair with a specified bit size using a custom number of primes. This example creates keys using 3 primes instead of the default 2. ```python import rsa (pubkey, privkey) = rsa.newkeys(512, nprimes=3) ``` -------------------------------- ### Importing Python-RSA 1.3.3 for Migration (Python) Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/upgrading.rst This snippet demonstrates how to import the older Python-RSA version 1.3.3 using a private module name. It is provided specifically to allow users to decrypt data or keys created with this version during the upgrade process to a newer release. Using this version in production is strongly discouraged due to security vulnerabilities. ```Python import rsa._version133 as rsa ``` -------------------------------- ### Converting Old Python-RSA Key Dictionaries to PKCS#1 Objects (Python) Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/upgrading.rst This snippet demonstrates how to migrate from the older dictionary-based key format to the newer PKCS#1 standard used by Python-RSA version 3.0 and later. It shows two methods: explicitly passing parameters to the PublicKey and PrivateKey constructors and using dictionary unpacking (**) for a more concise approach. The required parameters (n, e for public; n, e, d, p, q for private) must be extracted from the old dictionary. ```Python import rsa # Load the old key somehow. old_pub_key = { 'e': 65537, 'n': 31698122414741849421263704398157795847591L } old_priv_key = { 'd': 7506520894712811128876594754922157377793L, 'p': 4169414332984308880603L, 'q': 7602535963858869797L } # Create new key objects like this: pub_key = rsa.PublicKey(n=old_pub_key['n'], e=old_pub_key['e']) priv_key = rsa.PrivateKey(n=old_pub_key['n'], e=old_pub_key['e'], d=old_priv_key['d'], p=old_priv_key['p'], q=old_priv_key['q']) # Or use this shorter notation: pub_key = rsa.PublicKey(**old_pub_key) old_priv_key.update(old_pub_key) priv_key = rsa.PrivateKey(**old_priv_key) ``` -------------------------------- ### Decrypting with Old, Re-encrypting with New (Python) Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/upgrading.rst This code demonstrates the process of migrating encrypted data. It imports Python-RSA version 2.0 under an alias (rsa200) to decrypt data encrypted with that version, then imports the current version (3.0+) to re-encrypt the plaintext with a new key compatible with the current version's security features like random padding. This is a standard pattern for upgrading. ```Python import rsa._version200 as rsa200 import rsa # this imports version 3.0 decrypted = rsa200.decrypt(old_crypto, version_200_private_key) new_crypto = rsa.encrypt(decrypted, version_3_public_key) ``` -------------------------------- ### Importing Python-RSA 2.0 for Migration (Python) Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/upgrading.rst This snippet shows how to import the older Python-RSA version 2.0 via its private module name. It's intended solely for migration, enabling decryption or processing of data generated with this version before migrating to a more secure current release. Production use of this version is not recommended due to security risks. ```Python import rsa._version200 as rsa ``` -------------------------------- ### Signing Message with RSA Private Key Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/usage.rst Creates a detached digital signature for a message using the sender's private key and a specified hash algorithm (SHA-1 in this example). The signature allows verification of message integrity and authenticity. ```python (pubkey, privkey) = rsa.newkeys(512) message = 'Go left at the blue tree'.encode() signature = rsa.sign(message, privkey, 'SHA-1') ``` -------------------------------- ### Interoperable Encryption and Decryption Workflow (Shell) Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/compatibility.rst Illustrates an end-to-end workflow demonstrating interoperability by encrypting a plain text file using the `pyrsa-encrypt` utility with a public key, and then decrypting the resulting encrypted file using the corresponding private key with the OpenSSL `rsautl` command. This confirms basic encryption/decryption compatibility. ```Shell $ echo hello there > testfile.txt $ pyrsa-encrypt -i testfile.txt -o testfile.rsa publickey.pem $ openssl rsautl -in testfile.rsa -inkey privatekey.pem -decrypt hello there ``` -------------------------------- ### Extracting python-rsa Compatible Public Key from Private Key (Shell) Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/compatibility.rst Uses the `pyrsa-priv2pub` command-line utility, provided with python-rsa, to extract the public key component from an existing private key file. The output public key is saved in a format compatible with the python-rsa library. ```Shell pyrsa-priv2pub -i myprivatekey.pem -o mypublickey.pem ``` -------------------------------- ### Converting PKCS#8 Private Key to PKCS#1 using OpenSSL (Shell) Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/compatibility.rst Shows how to use the OpenSSL `rsa` command-line tool to convert a private key stored in the PKCS#8 format into the PKCS#1 format. This conversion is necessary because python-rsa directly supports PKCS#1 but not the more complex PKCS#8 format. ```Shell openssl rsa -in privatekey-pkcs8.pem -out privatekey.pem ``` -------------------------------- ### Loading Private Key from PKCS1 File Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/usage.rst Loads an RSA private key from a file formatted according to PKCS#1. The file content should be read in binary mode. ```python import rsa with open('private.pem', mode='rb') as privatefile: keydata = privatefile.read() privkey = rsa.PrivateKey.load_pkcs1(keydata) ``` -------------------------------- ### Signing File Content with RSA Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/usage.rst Creates a digital signature for the content of a file-like object. The file is read and hashed in blocks (1024 bytes by default) before signing. ```python with open('somefile', 'rb') as msgfile: signature = rsa.sign(msgfile, privkey, 'SHA-1') ``` -------------------------------- ### Verifying File Content Signature with RSA Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/usage.rst Verifies a digital signature against the content of a file-like object and the sender's public key. The file content is hashed in blocks during verification. ```python with open('somefile', 'rb') as msgfile: rsa.verify(msgfile, signature, pubkey) ``` -------------------------------- ### Generating RSA Keys Using Process Pool Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/usage.rst Generates an RSA key pair faster by utilizing multiple processes in parallel. The 'poolsize' parameter specifies the number of processes to use. ```python (pubkey, privkey) = rsa.newkeys(512, poolsize=8) ``` -------------------------------- ### Demonstrating RSA Verification Error Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/usage.rst Shows that attempting to verify a signature for a message that has been altered after signing will cause a VerificationError. ```python message = 'Go right at the blue tree'.encode() rsa.verify(message, signature, pubkey) ``` -------------------------------- ### Signing Pre-Calculated Hash with RSA Private Key Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/usage.rst Allows signing a message's hash separately from the message itself. First, compute the hash using `rsa.compute_hash`, then sign the resulting hash bytes with the private key using `rsa.sign_hash`. ```python message = 'Go left at the blue tree'.encode() hash = rsa.compute_hash(message, 'SHA-1') signature = rsa.sign_hash(hash, privkey, 'SHA-1') ``` -------------------------------- ### Generating RSA Key Pair Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/usage.rst Generates a new RSA public and private key pair with a specified bit size. The function returns a tuple containing the public key and the private key. ```python import rsa (pubkey, privkey) = rsa.newkeys(512) ``` -------------------------------- ### Demonstrating RSA Decryption Error Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/usage.rst Illustrates that altering an encrypted message (ciphertext) after encryption will likely cause a DecryptionError when attempting to decrypt it. ```python crypto = rsa.encrypt(b'hello', bob_pub) crypto = crypto[:-1] + b'X' # change the last byte rsa.decrypt(crypto, bob_priv) ``` -------------------------------- ### Encoding String Message to Bytes Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/usage.rst Converts a standard Python string into a byte sequence using a specified encoding, typically UTF-8, as the RSA module operates on bytes. ```python message = 'hello Bob!'.encode('utf8') ``` -------------------------------- ### Verifying RSA Signature with Public Key Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/usage.rst Verifies a digital signature against the original message and the sender's public key. If the signature is valid, the function returns the name of the hash algorithm used. ```python message = 'Go left at the blue tree'.encode() rsa.verify(message, signature, pubkey) ``` -------------------------------- ### Generating Random Key for Symmetric Encryption Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/usage.rst Generates a cryptographically secure random byte sequence of a specified length, typically used as a key for symmetric encryption algorithms like AES. ```python import rsa.randnum aes_key = rsa.randnum.read_random_bits(128) ``` -------------------------------- ### Encrypting Symmetric Key with RSA Public Key Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/usage.rst Encrypts a symmetric encryption key (like an AES key generated randomly) using a recipient's RSA public key. This is a common method for securely distributing the symmetric key. ```python encrypted_aes_key = rsa.encrypt(aes_key, public_rsa_key) ``` -------------------------------- ### Encrypting Message with RSA Public Key Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/usage.rst Encrypts a byte message using the recipient's RSA public key. The output is the encrypted message as bytes. ```python import rsa crypto = rsa.encrypt(message, bob_pub) ``` -------------------------------- ### Decrypting Message with RSA Private Key Python Source: https://github.com/sybrenstuvel/python-rsa/blob/main/doc/usage.rst Decrypts an encrypted byte message using the corresponding RSA private key. The output is the original byte message, which can then be decoded back to a string. ```python message = rsa.decrypt(crypto, bob_priv) print(message.decode('utf8')) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.