You are on page 1of 8

Lab Assignment 9

Aim: Write a Program for implementation of encryption and decryption of Chinese Remainder
Theorem.

Code:

#include <bits/stdc++.h>

using namespace std;

bool check_coprime(vector<int> b)

int n = b.size();

for (int x = 0; x < n; x++)

for (int y = x + 1; y < n; y++)

if (__gcd(b[x], b[y]) != 1)

return true;

return false;

void crt(vector<int> a, vector<int> b)

int n = b.size();

vector<int> M(n), M_inv(n);

int product = 1;

for (int k = 0; k < n; k++)


product *= b[k];

cout << endl

<< "product = " << product << endl;

for (int k = 0; k < n; k++)

M[k] = product / b[k];

for (int i = 1; true; i++)

if ((i * M[k]) % b[k] == 1)

M_inv[k] = i;

break;

cout << "M values: ";

for (int k = 0; k < n; k++)

cout << M[k] << " ";

cout << endl;

cout << "M inverse values: ";

for (int k = 0; k < n; k++)

cout << M_inv[k] << " ";


cout << endl;

int ans = 0;

for (int k = 0; k < n; k++)

ans += a[k] * M[k] * M_inv[k];

cout << endl

<< "x = " << ans % product << endl;

int main()

int n;

cout << "Enter number of equations: ";

cin >> n;

cout << "Enter a and b in x = a mod b for " << n << " equations: " << endl;

vector<int> a(n), b(n);

for (int i = 0; i < n; i++)

cin >> a[i] >> b[i];

if (!check_coprime(b))

crt(a, b);

else

cout << "This has no solutions." << endl;

return 0;
}

Output:

Aim: Write a Program for implementation of encryption and decryption of RSA Cryptosystem.

Code:

#include <bits/stdc++.h>

using namespace std;

// global variable

int p, q, phi, e, n, d;

int power(long long x, int y, int p)

int res = 1;
x = x % p;

if (x == 0)

return 0;

while (y > 0)

if (y & 1)

res = (res * x) % p;

y = y >> 1;

x = (x * x) % p;

return res;

bool check_prime(int a)

if (a <= 1)

return false;

if (a <= 3)

return true;

if (a % 2 == 0 || a % 3 == 0)

return false;
for (int i = 5; i * i <= n; i = i + 6)

if (a % i == 0 || a % (i + 2) == 0)

return false;

return true;

void key_generation()

cout << "Enter value for p: ";

cin >> p;

while (!check_prime(p))

cout << "p is not prime. Enter Again: ";

cin >> p;

cout << "Enter value for q: ";

cin >> q;

while (!check_prime(q))

cout << "q is not prime. Enter Again: ";

cin >> q;

n = p * q;
phi = (p - 1) * (q - 1);

cout << "Enter value for e in range (1, " << phi << ") exclusive: ";

cin >> e;

while (e <= 1 && e >= phi && __gcd(e, phi) != 1)

cout << "not a valid e value. Enter Again: ";

cin >> q;

for (d = 1; true; d++)

if ((d * e) % phi == 1)

break;

cout << "Public Key: {" << e << " , " << n << "}" << endl;

cout << "Private Key: {" << d << " , " << n << "}" << endl;

int main()

long long msg, cipher_msg, decrypted_msg;

key_generation();

cout << "Enter message: " << endl;

cin >> msg;

if (msg > n)
cout << "Message is greater than " << n << endl;

else

cipher_msg = power(msg, e, n);

cout << "Encrypted message = " << cipher_msg << endl;

decrypted_msg = power(cipher_msg, d, n);

cout << "Decrypted message = " << decrypted_msg << endl;

return 0;

Output:

You might also like