From bcde52891e80500700d2a66e96fbdf504f771056 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 30 Dec 2020 14:31:44 +0530 Subject: add dh & rsa --- q8/dh.c | 38 +++++++++++++++++++++++++++++ q8/rsa.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 q8/dh.c create mode 100644 q8/rsa.cpp (limited to 'q8') diff --git a/q8/dh.c b/q8/dh.c new file mode 100644 index 0000000..074dfc7 --- /dev/null +++ b/q8/dh.c @@ -0,0 +1,38 @@ +#include +#include +#include + +// Function to compute a^m mod n +int compute(int a, int m, int n) { + int r; + int y = 1; + + while (m > 0) { + r = m % 2; + if (r == 1) + y = (y * a) % n; + a = a * a % n; + + m = m / 2; + } + + return y; +} + +// C program to demonstrate Diffie-Hellman algorithm +int main() { + int p = 23; // modulus + int g = 5; // base + int a, b; // a - Alice's Secret Key, b - Bob's Secret Key. + int A, B; // A - Alice's Public Key, B - Bob's Public Key + srand(time(0)); + a = rand(); // or use rand() + A = compute(g, a, p); + srand(time(0)); + b = rand(); // or use rand() + B = compute(g, b, p); + int keyA = compute(B, a, p); + int keyB = compute(A, b, p); + printf("\nAlice's Secret Key is %d\nBob's Secret Key is %d\n\n", keyA, keyB); + return 0; +} diff --git a/q8/rsa.cpp b/q8/rsa.cpp new file mode 100644 index 0000000..c38e7d6 --- /dev/null +++ b/q8/rsa.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include + +using namespace std; + +long int gcd(long int a, long int b) { + if (a == 0) + return b; + if (b == 0) + return a; + return gcd(b, a % b); +} + +long int isprime(long int a) { + int i; + for (i = 2; i < a; i++) { + if ((a % i) == 0) + return 0; + } + return 1; +} + +long int encrypt(char ch, long int n, long int e) { + int i; + long int temp = ch; + for (i = 1; i < e; i++) + temp = (temp * ch) % n; + return temp; +} + +char decrypt(long int ch, long int n, long int d) { + int i; + long int temp = ch; + for (i = 1; i < d; i++) + ch = (temp * ch) % n; + return ch; +} + +int main() { + long int i, len; + long int p, q, n, phi, e, d, cipher[50]; + char text[50]; + + cout << "Enter the text to be encrypted: "; + cin.getline(text, sizeof(text)); + len = strlen(text); + do { + p = rand() % 30; + } while (!isprime(p)); + do { + q = rand() % 30; + } while (!isprime(q)); + n = p * q; + phi = (p - 1) * (q - 1); + do { + e = rand() % phi; + } while (gcd(phi, e) != 1); + do { + d = rand() % phi; + } while (((d * e) % phi) != 1); + cout << "Two prime numbers (p and q) are: " << p << " and " << q << endl; + cout << "n(p * q) = " << p << " * " << q << " = " << p * q << endl; + cout << "(p - 1) * (q - 1) = " << phi << endl; + cout << "Public key (n, e): (" << n << ", " << e << ")\n"; + cout << "Private key (n, d): (" << n << ", " << d << ")\n"; + for (i = 0; i < len; i++) + cipher[i] = encrypt(text[i], n, e); + cout << "Encrypted message: "; + for (i = 0; i < len; i++) + cout << cipher[i]; + for (i = 0; i < len; i++) + text[i] = decrypt(cipher[i], n, d); + cout << endl; + cout << "Decrypted message: "; + for (i = 0; i < len; i++) + cout << text[i]; + cout << endl; + return 0; +} -- cgit v1.2.3