This project is a simple implementation of the RSA encryption algorithm, written in Python. It demonstrates RSA key generation, encryption, and decryption by allowing you to encrypt a message with a public key and decrypt it back with a private key.
RSA is a widely-used asymmetric cryptographic algorithm that ensures secure communication. This program uses two large prime numbers to generate public and private keys, which can then be used to encrypt and decrypt messages.
-
Key Generation:
- Generate two distinct prime numbers
p
andq
. - Compute
n = p * q
(modulus) andphi = (p - 1) * (q - 1)
. - Choose an integer
e
(public exponent) that is coprime withphi
. - Calculate the modular inverse
d
ofe
with respect tophi
, resulting in the private exponent. - Public key:
(e, n)
and Private key:(d, n)
.
- Generate two distinct prime numbers
-
Encryption:
- Encrypt each character in the message using the formula:
ciphertext = (ord(char) ** e) % n
.
- Encrypt each character in the message using the formula:
-
Decryption:
- Decrypt each character using the formula:
plaintext = chr((char ** d) % n)
.
- Decrypt each character using the formula:
- Python 3.x
- sympy library for generating random prime numbers.
Install sympy
with:
pip install sympy