0% found this document useful (0 votes)
5 views

IS ASSIGNMENT 02

res

Uploaded by

z0289521
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

IS ASSIGNMENT 02

res

Uploaded by

z0289521
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 5

ASSIGNMENT 02

RSA
#include <iostream>

#include <cmath>

#include <cstdlib>

#include <ctime>

using namespace std;

// Function to compute gcd of two numbers

long long gcd(long long a, long long b) {

if (b == 0)

return a;

return gcd(b, a % b);

// Function to compute (base^exp) % mod using binary exponentiation

long long modExp(long long base, long long exp, long long mod) {

long long result = 1;

base = base % mod;

while (exp > 0) {

if (exp % 2 == 1) {

result = (result * base) % mod;

exp = exp >> 1;

base = (base * base) % mod;


}

return result;

// Function to generate a random prime number in the range [min, max]

long long generatePrime(long long min, long long max) {

long long prime;

while (true) {

prime = rand() % (max - min + 1) + min;

bool isPrime = true;

for (long long i = 2; i <= sqrt(prime); i++) {

if (prime % i == 0) {

isPrime = false;

break;

if (isPrime) {

return prime;

// Function to generate RSA keys

void generateRSAKeys(long long& n, long long& e, long long& d) {

long long p = generatePrime(100, 200); // Generate random primes p and q

long long q = generatePrime(100, 200);

while (p == q) { // Ensure p != q

q = generatePrime(100, 200);
}

n = p * q; // n = p * q

long long phi = (p - 1) * (q - 1); // Euler's totient function

// Choose e such that 1 < e < phi and gcd(e, phi) = 1

e = 2;

while (gcd(e, phi) != 1) {

e++;

// Compute d such that d * e ≡ 1 (mod phi)

long long k = 1;

while ((k * phi + 1) % e != 0) {

k++;

d = (k * phi + 1) / e;

// Function to encrypt a message using public key (e, n)

long long encrypt(long long message, long long e, long long n) {

return modExp(message, e, n);

// Function to decrypt a message using private key (d, n)

long long decrypt(long long cipherText, long long d, long long n) {

return modExp(cipherText, d, n);

}
int main() {

srand(time(0)); // Seed for random number generation

long long n, e, d;

generateRSAKeys(n, e, d);

cout << "RSA Key Pair:" << endl;

cout << "Public Key (e, n): (" << e << ", " << n << ")" << endl;

cout << "Private Key (d, n): (" << d << ", " << n << ")" << endl;

// Encrypting a message

long long message;

cout << "Enter a message to encrypt (as a number): ";

cin >> message;

long long cipherText = encrypt(message, e, n);

cout << "Encrypted message: " << cipherText << endl;

// Decrypting the message

long long decryptedMessage = decrypt(cipherText, d, n);

cout << "Decrypted message: " << decryptedMessage << endl;

return 0;

You might also like