RSA Cryptosystem

RSA is a public-key cryptosystem used for both encryption and digital signatures. Security is based on the mathematical difficulty of factoring large composite numbers. RSA enables secure communication without prior key sharing and is widely used in secure messaging, certificates, and authentication.

class pycrypt.asymmetric.RSAKey(n, e, d=None, p=None, q=None)[source]

Bases: object

Represents an RSA key pair supporting encryption, decryption, signing, and verification.

This class provides both low-level RSA primitives and high-level operations using OAEP (Optimal Asymmetric Encryption Padding) for encryption and PSS (Probabilistic Signature Scheme) for signing, along with key generation and PEM serialization.

Example

>>> key = RSAKey.generate(2048)
>>> ciphertext = key.oaep_encrypt(b"hello")
>>> plaintext = key.oaep_decrypt(ciphertext)
>>> assert plaintext == b"hello"
Parameters:
  • n (int)

  • e (int)

  • d (int | None)

  • p (int | None)

  • q (int | None)

n

RSA modulus (product of two large primes, p and q).

Type:

int

e

RSA public exponent.

Type:

int

d

RSA private exponent (None for public-only keys).

Type:

int | None

p

First prime factor of n (None for public-only keys).

Type:

int | None

q

Second prime factor of n (None for public-only keys).

Type:

int | None

dP

d mod (p−1), used for CRT optimization.

Type:

int | None

dQ

d mod (q−1), used for CRT optimization.

Type:

int | None

qInv

Multiplicative inverse of q mod p.

Type:

int | None

k

Key length in bytes.

Type:

int

property PUBLIC_KEY: tuple[int, int]

Returns the public key (n, e).

Type:

tuple[int, int]

property PRIVATE_KEY: tuple[int, int, int | None, ...]

Returns the full private key components (n, e, d, p, q, dP, dQ, qInv).

Type:

tuple[int, int, int | None, …]

primitive_encrypt(message)[source]

Performs raw RSA encryption (modular exponentiation).

Parameters:

message (int) – The message as an integer.

Returns:

The ciphertext integer.

Return type:

int

primitive_decrypt(ciphertext)[source]

Performs raw RSA decryption, optionally using CRT optimization.

Parameters:

ciphertext (int) – The ciphertext as an integer.

Returns:

The decrypted message as an integer.

Return type:

int

Raises:

TypeError – If the private exponent is missing.

primitive_sign(message)[source]

Performs raw RSA signing (equivalent to decryption).

Parameters:

message (int) – The message as an integer.

Returns:

The signature as an integer.

Return type:

int

primitive_verify(signature)[source]

Performs raw RSA signature verification (equivalent to encryption).

Parameters:

signature (int) – The signature as an integer.

Returns:

The verified message integer.

Return type:

int

oaep_encrypt(message, label=b'', hash=<class 'pycrypt.hash.sha.variants.SHA256'>)[source]

Encrypts a message using RSA with OAEP padding.

Parameters:
  • message (bytes) – The plaintext message.

  • label (bytes, optional) – Optional label for OAEP encoding. Defaults to b””.

  • hash (type, optional) – Hash function class used in OAEP. Defaults to SHA256.

Returns:

The ciphertext.

Return type:

bytes

Raises:

ValueError – If message length exceeds maximum allowed size.

oaep_decrypt(ciphertext, label=b'', hash=<class 'pycrypt.hash.sha.variants.SHA256'>)[source]

Decrypts a message encrypted with RSA-OAEP.

Parameters:
  • ciphertext (bytes) – The ciphertext to decrypt.

  • label (bytes, optional) – Label used during encryption. Defaults to b””.

  • hash (type, optional) – Hash function used during encryption. Defaults to SHA256.

Returns:

The decrypted plaintext.

Return type:

bytes

Raises:
  • ValueError – If ciphertext length is invalid or decryption fails.

  • TypeError – If private key is missing.

pss_sign(message, slen=None, hash=<class 'pycrypt.hash.sha.variants.SHA256'>)[source]

Generates a digital signature using RSA-PSS.

Parameters:
  • message (bytes) – The message to sign.

  • slen (int | None, optional) – Salt length. Defaults to hash length.

  • hash (type, optional) – Hash function used for PSS. Defaults to SHA256.

Returns:

The signature.

Return type:

bytes

pss_verify(message, signature, slen=None, hash=<class 'pycrypt.hash.sha.variants.SHA256'>)[source]

Verifies a signature created with RSA-PSS.

Parameters:
  • message (bytes) – The original message.

  • signature (bytes) – The RSA-PSS signature.

  • slen (int | None, optional) – Salt length. Defaults to hash length.

  • hash (type, optional) – Hash function used for PSS. Defaults to SHA256.

Returns:

True if the signature is valid, False otherwise.

Return type:

bool

export_key(type='public')[source]

Exports the RSA key in PEM format.

Parameters:

type (Literal["public", "private"], optional) – Key type to export. Must be either “public” or “private”. Defaults to “public”.

Returns:

The PEM-encoded RSA key.

Return type:

str

Raises:
  • ValueError – If type is invalid.

  • TypeError – If private key components are missing for private export.

classmethod import_key(pem)[source]

Imports an RSA key from a PEM-formatted string.

Parameters:

pem (str) – The PEM-encoded RSA key.

Returns:

An RSAKey instance initialized with the imported key.

Return type:

RSAKey

Raises:

ValueError – If the PEM cannot be parsed as a valid RSA key.

classmethod generate(bits=2048, e=65537)[source]

Generates a new RSA key pair.

Parameters:
  • bits (int, optional) – Key size in bits. Defaults to 2048.

  • e (int, optional) – Public exponent. Defaults to 65537.

Returns:

A new RSA key pair.

Return type:

RSAKey