Professional Documents
Culture Documents
Aim: Write a Program for implementation of encryption and decryption of Chinese Remainder
Theorem.
Code:
#include <bits/stdc++.h>
bool check_coprime(vector<int> b)
int n = b.size();
if (__gcd(b[x], b[y]) != 1)
return true;
return false;
int n = b.size();
int product = 1;
M_inv[k] = i;
break;
int ans = 0;
int main()
int n;
cin >> n;
cout << "Enter a and b in x = a mod b for " << n << " equations: " << endl;
if (!check_coprime(b))
crt(a, b);
else
return 0;
}
Output:
Aim: Write a Program for implementation of encryption and decryption of RSA Cryptosystem.
Code:
#include <bits/stdc++.h>
// global variable
int p, q, phi, e, n, d;
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()
cin >> p;
while (!check_prime(p))
cin >> p;
cin >> q;
while (!check_prime(q))
cin >> q;
n = p * q;
phi = (p - 1) * (q - 1);
cout << "Enter value for e in range (1, " << phi << ") exclusive: ";
cin >> e;
cin >> q;
if ((d * e) % phi == 1)
break;
cout << "Public Key: {" << e << " , " << n << "}" << endl;
cout << "Private Key: {" << d << " , " << n << "}" << endl;
int main()
key_generation();
if (msg > n)
cout << "Message is greater than " << n << endl;
else
return 0;
Output: