This code implements the RSA encryption and decryption algorithm. It generates random prime numbers, calculates the public and private keys, encrypts and decrypts messages using these keys.
The code performs the following steps:
-
Generates two random prime numbers,
p
andq
, of the specified number of bits. -
Calculates the modulus
n
as the product ofp
andq
. -
Calculates the Euler's totient function
phi
as(p-1) * (q-1)
. -
Finds the optimal value of
e
that is relatively prime tophi
. -
Displays the generated prime numbers (
p
andq
), modulus (n
), Euler's totient function (phi
), and the public key (e
,n
). -
Encrypts the input plain text message using the public key to obtain the ciphertext.
-
Displays the encrypted ciphertext.
-
Calculates the private key
d
as the modular multiplicative inverse ofe
modulophi
. -
Decrypts the ciphertext using the private key to obtain the original plain text message.
-
Displays the decrypted plain text message.
Run The main.go
file Using Command:
go run main.go
Run The RSA.cpp
file Using Command:
g++ RSA.cpp -o rsa
Once the compilation process is successful, you can execute the compiled program. To run the program, use the following command:
./rsa
- You can modify the number of bits for the generated prime numbers by changing the
numBits
variable in thesolve()
function.
- The code is based on the RSA algorithm for encryption and decryption.
- The Miller-Rabin primality test is used to generate random prime numbers.
- The modular exponentiation and extended Euclidean algorithm functions are adapted from standard algorithms.