From d3442e78f7fc56aa7c9d788ff5b04183b109f51f Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 30 Dec 2020 14:57:55 +0530 Subject: make mods as requested --- q8/dh.c | 34 ++++++++++++++++++++++++---------- q8/rsa.cpp | 4 ++-- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/q8/dh.c b/q8/dh.c index 074dfc7..1c70c8a 100644 --- a/q8/dh.c +++ b/q8/dh.c @@ -2,7 +2,7 @@ #include #include -// Function to compute a^m mod n + int compute(int a, int m, int n) { int r; int y = 1; @@ -12,27 +12,41 @@ int compute(int a, int m, int n) { if (r == 1) y = (y * a) % n; a = a * a % n; - m = m / 2; } return y; } -// C program to demonstrate Diffie-Hellman algorithm +int rand_range(int l, int u) { + return rand() % (u - l + 1) + l; +} + + 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 + int p = 23; + int g = 5; + + printf("Modulus: %d, Base: %d\n\n", p, g); + int a, b; + int A, B; + + int l = 1, u = 1000; + printf("RNG range: %d - %d\n\n", l, u); + srand(time(0)); - a = rand(); // or use rand() + a = rand_range(l,u); A = compute(g, a, p); + srand(time(0)); - b = rand(); // or use rand() + b = rand_range(l,u); 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); + + printf("Alice's Secret is %d\nBob's Secret is %d\n\n", a, b); + printf("Alice's Public Key is %d\nBob's Public Key is %d\n\n", A, B); + printf("Alice'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 index c38e7d6..c33bf7e 100644 --- a/q8/rsa.cpp +++ b/q8/rsa.cpp @@ -47,10 +47,10 @@ int main() { cin.getline(text, sizeof(text)); len = strlen(text); do { - p = rand() % 30; + p = rand() % 800; } while (!isprime(p)); do { - q = rand() % 30; + q = rand() % 800; } while (!isprime(q)); n = p * q; phi = (p - 1) * (q - 1); -- cgit v1.2.3