RSA#
The RSA class implements the Rivest-Shamir-Adleman public-key encryption algorithm, whose security is based upon the Integer Factorization problem.
- class RSA#
Creates a new RSA instance.
Note
The implementation of RSA in this module follows the standards set by NIST in FIPS 186-5. Refer the Standards to see the full list of NIST Standards and Guidelines considered.
Introduction#
The Rivest-Shamir-Adleman (RSA) algorithm is a widely used public-key encryption algorithm that provides secure data transmission. It is based on the principles of asymmetric cryptography, where a pair of keys is used to encrypt and decrypt data. The security of RSA relies on the difficulty of factoring large composite numbers, which makes it computationally infeasible to determine the private key from the public key.
Mathematical Details#
RSA Key Generation#
The RSA key generation process involves the following steps:
Choose two large prime numbers: Two large prime numbers, \(p\) and \(q\), are chosen randomly.
Compute the modulus: The modulus, \(n\), is computed as:
Compute the totient: The Euler’s totient, \(\phi(n)\), is computed as:
Choose the public exponent: A small prime number, \(e\), is chosen as the public exponent. In the implementation, the standard value of 65537 is used.
Compute the private exponent: The private exponent, \(d\), is computed as:
The public key is then the pair \((n, e)\), and the private key is \((n, d)\).
RSA Encryption/Decryption Process#
Encryption: The plaintext message, \(m\), is encrypted using the public key \((n, e)\) as follows:
Decryption: The ciphertext message, \(c\), is decrypted using the private key \((n, d)\) as follows:
RSA Signature/Verification Process#
Signature: The hash of the plaintext message, \(m_h\), is signed using the private key \((n, d)\) as follows:
Verification: The signature, \(s\), for a message \(m\) with hash \(m_h\), is verified using the public key \((n, e)\) as follows:
The result of the verification is given by:
Usage#
# Example usage of RSA to encrypt, decrypt, sign and verify a message
from cryptosystems import RSA
cipher = RSA()
public_key, private_key = cipher.generate_keys() # Generate RSA keys
ciphertext = cipher.encrypt("Hello World", public_key)
print(ciphertext) # 123456
plaintext = cipher.decrypt(ciphertext, private_key, "str")
print(plaintext) # 'Hello World'
signature, message_hash = cipher.sign("Hello World", private_key)
print(signature, message_hash, sep=", ") # 123456, b'\x12\x34\x56\x78\x90'
verification = cipher.verify(signature, message_hash, public_key)
print(verification) # True
Methods#
- generate_keypair() tuple#
Generates a new RSA key pair, in the form \(((n, e), (n, d))\).
- Returns:
A tuple containing the public key and private key.
- Return type:
- encrypt(plaintext: int | str | bytes, public_key: tuple) int#
Encrypts the given plaintext using the RSA algorithm and returns the ciphertext.
- decrypt(ciphertext: int | str | bytes, private_key: tuple, return_type: str)#
Decrypts the given ciphertext using the RSA algorithm and returns the deciphered plaintext.
- Parameters:
- Returns:
The decrypted plaintext.
- Return type:
- sign(message: int | str | bytes, private_key: tuple) tuple#
Signs the given message using the RSA Algorithm and returns the signature and SHA256 hash.