or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cli-tools.mdcrypto-operations.mdindex.mdkey-management.md
tile.json

tessl/pypi-rsa

Pure-Python RSA implementation for encryption, decryption, signing, and verification

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/rsa@4.9.x

To install, run

npx @tessl/cli install tessl/pypi-rsa@4.9.0

index.mddocs/

RSA

A pure-Python implementation of the RSA cryptographic algorithm supporting encryption, decryption, digital signing, and signature verification according to PKCS#1 version 1.5 standards. This library provides both a Python API for programmatic use and command-line tools for direct RSA operations.

Package Information

  • Package Name: rsa
  • Language: Python
  • Installation: pip install rsa
  • Dependencies: pyasn1 (>=0.1.3)

Core Imports

import rsa

Import specific functions:

from rsa import newkeys, encrypt, decrypt, sign, verify
from rsa import PublicKey, PrivateKey

Basic Usage

import rsa

# Generate a key pair
(public_key, private_key) = rsa.newkeys(512)

# Encrypt a message
message = b'hello world'
encrypted = rsa.encrypt(message, public_key)

# Decrypt the message
decrypted = rsa.decrypt(encrypted, private_key)
print(decrypted.decode())  # outputs: hello world

# Sign a message
signature = rsa.sign(message, private_key, 'SHA-256')

# Verify the signature
hash_method = rsa.verify(message, signature, public_key)
print(hash_method)  # outputs: SHA-256

Architecture

The RSA library is organized around three main components:

  • Key Management: Key pair generation, loading/saving keys in various formats (PEM, DER)
  • Cryptographic Operations: PKCS#1 v1.5 encryption/decryption and signing/verification
  • CLI Tools: Command-line utilities for key generation and cryptographic operations

The library implements pure-Python RSA operations without external cryptographic dependencies, making it suitable for educational purposes and environments requiring minimal dependencies.

Capabilities

Key Management

RSA key pair generation and key object management including loading/saving keys in PKCS#1 PEM/DER formats and OpenSSL compatibility.

def newkeys(nbits: int, accurate: bool = True, poolsize: int = 1, exponent: int = 65537) -> Tuple[PublicKey, PrivateKey]: ...

class PublicKey:
    def __init__(self, n: int, e: int): ...
    def save_pkcs1_pem(self) -> bytes: ...
    def save_pkcs1_der(self) -> bytes: ...

class PrivateKey:
    def __init__(self, n: int, e: int, d: int, p: int, q: int): ...
    def save_pkcs1_pem(self) -> bytes: ...
    def save_pkcs1_der(self) -> bytes: ...

Key Management

Cryptographic Operations

PKCS#1 v1.5 encryption, decryption, signing, and signature verification with support for multiple hash algorithms.

def encrypt(message: bytes, pub_key: PublicKey) -> bytes: ...
def decrypt(crypto: bytes, priv_key: PrivateKey) -> bytes: ...
def sign(message: bytes, priv_key: PrivateKey, hash_method: str) -> bytes: ...
def verify(message: bytes, signature: bytes, pub_key: PublicKey) -> str: ...
def sign_hash(hash_value: bytes, priv_key: PrivateKey, hash_method: str) -> bytes: ...
def find_signature_hash(signature: bytes, pub_key: PublicKey) -> str: ...
def compute_hash(message: Union[bytes, BinaryIO], method_name: str) -> bytes: ...

Cryptographic Operations

CLI Tools

Command-line utilities for key generation, encryption, decryption, signing, and signature verification.

# CLI entry points available as shell commands:
# pyrsa-keygen - Generate RSA key pairs
# pyrsa-encrypt - Encrypt data with public key
# pyrsa-decrypt - Decrypt data with private key  
# pyrsa-sign - Sign data with private key
# pyrsa-verify - Verify signature with public key
# pyrsa-priv2pub - Convert private key to public key

CLI Tools

Exception Types

class CryptoError(Exception):
    """Base class for all cryptographic exceptions"""

class DecryptionError(CryptoError):
    """Raised when decryption fails"""

class VerificationError(CryptoError):
    """Raised when signature verification fails"""

Type References

from typing import Tuple, Union, BinaryIO, Iterator, Callable

Supported Hash Methods

  • SHA-1 - Secure Hash Algorithm 1
  • SHA-224 - SHA-2 224-bit
  • SHA-256 - SHA-2 256-bit (recommended)
  • SHA-384 - SHA-2 384-bit
  • SHA-512 - SHA-2 512-bit
  • MD5 - Message Digest 5 (deprecated, not recommended)

Key Formats

  • PKCS#1 PEM - ASCII-armored format with BEGIN/END RSA KEY headers
  • PKCS#1 DER - Binary ASN.1 DER encoding
  • OpenSSL PEM - OpenSSL-compatible PEM format
  • Pickle - Python pickle format (with security warnings)