RSA module implements the RSA (Rivest-Shamir-Adleman) encryption algorithm, a public-key cryptosystem used for secure data transmission.
gen_keys
Generates a pair of RSA keys (public and private) for encryption and decryption.The bit length for the RSA keys. Larger values provide stronger security but slower performance. Common values: 32 (testing), 512, 1024, 2048 (recommended for production).
A tuple containing:
e(int): The public exponent (encryption key)d(int): The private exponent (decryption key)n(int): The modulus used by both keys
Key generation involves finding two prime numbers and calculating related values. The function validates the keys by performing a test encryption/decryption cycle before returning them.
Example
From application code
encrypt
Encrypts a text message using RSA public key encryption.The message to encrypt. Can contain any characters.
The public exponent from the key pair.
The modulus from the key pair.
A list of encrypted integers, where each integer represents an encrypted character from the original message.
How it works
The encryption process:- Converts each character to its ASCII value using
ord() - Encrypts each ASCII value using modular exponentiation:
ciphertext = (plaintext^e) mod n - Returns a list of encrypted values
Example
decrypt
Decrypts an RSA-encrypted message using the private key.A list of encrypted integers (typically from the
encrypt function).The private exponent from the key pair.
The modulus from the key pair (must match the one used for encryption).
The decrypted message as a string. Only includes valid ASCII characters in the range 32-126.
The function filters the decrypted characters to only include valid ASCII printable characters (32-126). This prevents garbage characters from appearing in the output.
How it works
The decryption process:- Takes each encrypted integer from the cipher list
- Decrypts each value using modular exponentiation:
plaintext = (ciphertext^d) mod n - Converts the decrypted value back to a character using
chr() - Filters out invalid ASCII characters
- Returns the reconstructed message
Example
encrypt_number
Encrypts a single number using RSA encryption.The number to encrypt.
The public exponent from the key pair.
The modulus from the key pair.
The encrypted number calculated as
(num^e) mod n.Example
decrypt_number
Decrypts a single RSA-encrypted number.The encrypted number to decrypt.
The private exponent from the key pair.
The modulus from the key pair.
The decrypted number calculated as
(cipher^d) mod n.Example
gcd
Calculates the greatest common divisor (GCD) of two numbers using the Euclidean algorithm.The first number.
The second number.
The greatest common divisor of a and b.
This function is used internally during key generation to ensure that the public exponent
e is coprime with phi(n).Example
mod_inverse
Calculates the modular multiplicative inverse using the Extended Euclidean algorithm.The number to find the inverse of.
The modulus.
The modular multiplicative inverse of
a modulo m, such that (a * result) mod m = 1.This function is used internally to calculate the private key exponent
d from the public exponent e and phi(n).Example
gen_prime
Generates a random prime number of the specified bit length.The bit length of the prime number to generate.
A random prime number with the specified bit length.
Example
is_prime
Checks whether a number is prime using trial division.The number to check for primality.
True if the number is prime, False otherwise.This implementation uses a simple trial division method. For larger numbers, more sophisticated primality tests (like Miller-Rabin) would be more efficient.
Example
Complete RSA workflow
Here’s a complete example showing the full RSA encryption/decryption process:Security considerations
Important security notes:
- Use at least 2048 bits for production systems
- Keep private keys (
d) secure and never share them - Public keys (
e,n) can be shared freely - The same key pair can encrypt multiple messages
- For the educational implementation, 32-bit keys are used for speed but are NOT secure